[zz]怎么进行Flex动画编程
[zz]如何进行Flex动画编程?
如何进行Flex的动画编程呢?跟着我来,一步一步从基础开始把!
首先,我们做个最基本的动画,让一个小球在屏幕上实现滚动。
我们在我们的FlexBuilder或者FlashBuilder中,新建一个 ActionScript项目 ,其名为 animate ,然后,我们看以下代码,你可以直接将其拷贝到新建工程的as类文件中:
package {
import flash.display.Sprite;
import flash.events.Event;
public class animate extends Sprite {
private var ball:Sprite;
public function animate() {
init();
}
private function init():void {
ball = new Sprite();
addChild(ball);
ball.graphics.beginFill(0xff0000);
ball.graphics.drawCircle(0, 0, 40);
ball.graphics.endFill();
ball.x = 20;
ball.y = stage.stageHeight / 2;
ball.addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
private function onEnterFrame(event:Event):void {
ball.x++;
}
}
}
我们可以看到类animate实际上继承了Sprite类,并且在最后加入了一个EnterFrame得动作,这是个什么动作呢?这其实就是帧在刷新时会执行的动作,在这个事件里我们可以实现称之为动作的代码,即我们想要实现的动作的代码,这里我们加入的是一个最简单的让横坐标加5的代码,这样,我们可以编译运行这个类所产生的swf文件,我们可以看到一个红色的小球从左向右缓慢移动。
ok,这就是一个最简答的动画教程。
但是,如果让动画运行之后,小球会一直滚动到屏幕外边,如果我们想要小球滚动到右边界的某处,就向回滚动的话,如何实现呢?
OK,我们可以看下面的代码:
package {
import flash.display.Sprite;
import flash.events.Event;
public class animate extends Sprite {
private var ball:Sprite;
public function animate() {
init();
}
private function init():void {
ball = new Sprite();
addChild(ball);
ball.graphics.beginFill(0xff0000);
ball.graphics.drawCircle(100, 100, 40);
ball.graphics.endFill();
ball.x = 20;
ball.y = stage.stageHeight / 2;
ball.addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
private function onEnterFrame(event:Event):void {
if(ball.x>220)
{
ball.removeEventListener(Event.ENTER_FRAME,onEnterFrame);
ball.addEventListener(Event.ENTER_FRAME, left);
}
else
ball.x+=5;
}
private function left(event:Event):void {
if(ball.x<20)
{
ball.removeEventListener(Event.ENTER_FRAME,left);
ball.addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
else
ball.x-=5;
}
}
}
相信大家已经想到了,在这里我们定义了一个新的动作left,当小球的横坐标超出某个范围的时候,将小球先前绑定的动作remove掉,然后绑定一个这个新的动作left,这个动作就是小球向回滚动的动作,然后当小球滚动到原点的时候,我们再让其绑定到初始化时的动作,这样小球就会来回滚动了^_^。
http://www.5gme.com/space-73350-do-blog-id-67597.html
package { import flash.display.Sprite; import flash.events.Event; public class animate extends Sprite { private var ball:Sprite; public function animate() { init(); } private function init():void { ball = new Sprite(); addChild(ball); ball.graphics.beginFill(0xff0000); ball.graphics.drawCircle(0, 0, 40); ball.graphics.endFill(); ball.x = 20; ball.y = stage.stageHeight / 2; ball.addEventListener(Event.ENTER_FRAME, onEnterFrame); } private function onEnterFrame(event:Event):void { ball.x++; } } }
package { import flash.display.Sprite; import flash.events.Event; public class animate extends Sprite { private var ball:Sprite; public function animate() { init(); } private function init():void { ball = new Sprite(); addChild(ball); ball.graphics.beginFill(0xff0000); ball.graphics.drawCircle(100, 100, 40); ball.graphics.endFill(); ball.x = 20; ball.y = stage.stageHeight / 2; ball.addEventListener(Event.ENTER_FRAME, onEnterFrame); } private function onEnterFrame(event:Event):void { if(ball.x>220) { ball.removeEventListener(Event.ENTER_FRAME,onEnterFrame); ball.addEventListener(Event.ENTER_FRAME, left); } else ball.x+=5; } private function left(event:Event):void { if(ball.x<20) { ball.removeEventListener(Event.ENTER_FRAME,left); ball.addEventListener(Event.ENTER_FRAME, onEnterFrame); } else ball.x-=5; } } }