Flash AS3教程:创建好看的遮罩动画效果来源:中国教程网论坛 发布者:chinaitedu 发布时间:2010-02-01
这是一个创建遮罩效果的教程,将学习如何在一个图像上创建多个大小不同的运动遮罩。
1、新建Flash文件,导入所需的图片到舞台,设置舞台属性的宽、高同图片相同大小。
package {
import flash.display.MovieClip; public class MyMask extends MovieClip { //Mask’s x and y speed public var speedX:Number; public var speedY:Number; //Set the given scale for this mask, when we create a new //mask object public function MyMask(scale:Number) { this.scaleX = scale; this.scaleY = scale; } } }
这是一个名为MyMask.as的遮罩类,保存在fla文件的同一目录下。 //We use an array to hold all our masks. //(Except the mask that follows our cursor) var masks:Array = new Array(); //We add all of the masks to a container var maskContainer:Sprite = new Sprite(); //Set the maskContainer to be the image’s mask backgroundImage.mask = maskContainer; //Add the container on the stage addChild(maskContainer); //Create the mask which follows cursor movement (master mask) var masterMask:MyMask = new MyMask(1); //Set the master masks’s coordinates to match cursor’s coordinates masterMask.x = mouseX; masterMask.y = mouseY; //Add the master mask to a container maskContainer.addChild(masterMask); //Cache the image and container as bitmap, so we //can animate the alpha of the masks maskContainer.cacheAsBitmap=true; backgroundImage.cacheAsBitmap=true; //Create a timer that is called every 0.2 seconds var timer:Timer = new Timer(200,0); timer.addEventListener(TimerEvent.TIMER, timerEvent); timer.start(); //This function is called every 0.2 seconds. //We create a new mask in this function. function timerEvent(e:TimerEvent):void { //Calculate a random scale for the new mask (0 to 1.5) var scale:Number = Math.random() * 1.5 + 0.5; //Create a new mask with random scale var newMask:MyMask = new MyMask(scale); //Set the position for the new mask newMask.x = mouseX; newMask.y = mouseY; //Assign a random x and y speed for the mask newMask.speedX = Math.random() * 20 - 10; newMask.speedY = Math.random() * 20 - 10; //Add the mask to the container maskContainer.addChild(newMask); //Add the mask to the array masks.push(newMask); } //We need ENTER_FRAME to animate the masks addEventListener(Event.ENTER_FRAME, enterFrameHandler); //This function is called in each frame function enterFrameHandler(e:Event):void { //Loop through the mask array for (var i:uint = 0; i < masks.length; i++) { //Save a mask to a local variable var myMask:MyMask = (MyMask)(masks[i]); //Update the x and y position myMask.x += myMask.speedX; myMask.y += myMask.speedY; //Increase the scale myMask.scaleX += 0.1; myMask.scaleY += 0.1; //Reduce the alpha myMask.alpha -= 0.01; //If the alpha is below 0, remove the mask //from the container and from the array if (myMask.alpha < 0) { masks.splice(i,1); maskContainer.removeChild(myMask); } } //Update the master mask position masterMask.x = mouseX; masterMask.y = mouseY; }
9、好了,工作全部完成,测试你的影片。 |
热门咨讯
|