论坛交流
首页办公自动化| 网页制作| 平面设计| 动画制作| 数据库开发| 程序设计| 全部视频教程
应用视频: Windows | Word2007 | Excel2007 | PowerPoint2007 | Dreamweaver 8 | Fireworks 8 | Flash 8 | Photoshop cs | CorelDraw 12
编程视频: C语言视频教程 | HTML | Div+Css布局 | Javascript | Access数据库 | Asp | Sql Server数据库Asp.net  | Flash AS
当前位置 > 文字教程 > Flash AS教程
Tag:2.0,3.0菜鸟,游戏,,cs,技巧,源码,,文本,文字,函数,音乐,随机,拖拽,asp,access,xml,mc,视频教程

遮罩动画的制作实例(一)

文章类别:Flash AS | 发表日期:2010-3-2 9:23:52

演示:


1、新建Flash文件,导入所需的图片到舞台,设置舞台属性的宽、高同图片相同大小。

2、将图片设置为左对齐、上对齐。右键单击图片转换成影片剪辑,命名为“Background”,设置注册点为居中。图1:
sshot-1.png
3、将图层1改名为背景,在属性面板中输入实例名称:“backgroundImage” 锁定。图2:
sshot-2.png
4、新建一个图层,用椭圆工具画一个禁止笔触的50*50的圆,填充色任意。

5、把圆转换成影片剪辑,设置如下。图3:
sshot-3.png
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;

}
9、好了,工作全部完成,测试你的影片。

 

 
上一篇:遮罩动画的制作实例(二) 人气:2159
下一篇:FLASH AS3制作数字下落的效果 人气:2903
视频教程列表
文章教程搜索
 
Flash AS推荐教程
Flash AS热门教程
看全部视频教程
购买方式/价格
购买视频教程: 咨询客服
tel:15972130058