Flash动画教程:创建好看的遮罩动画效果(3)
来源:互联网 作者:未知 发布时间:2011-03-26 09:12:47 网友评论 0 条
6、删除舞台上的圆,图层改名为as。至此fla的美工已全部完成。
7、新建ActionScript文件,编写一个外部的MyMask.as文件。在编译器中输入代码:
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文件的同一目录下。
8、切换到fla,在as层输入代码:
//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;
}
- 2009-10-11
- 2009-10-10
- 2009-10-11
- 2009-10-11
- 2011-02-02
- 2009-10-10
- 2009-10-10
- 2009-10-11
- 2010-05-16
- 2009-10-11
- 2010-05-16
- 2009-10-10
- 2009-10-11
- 2010-05-16
- 2009-10-11
关于我们 | 联系方式 | 广告服务 | 免责条款 | 内容合作 | 图书投稿 | 招聘职位 | About CG Time
Copyright © 2008 CGTime.org Inc. All Rights Reserved. CG时代 版权所有