how to create a simple animation in pure actionscript (flex sdk only)
I really don't want something sophisticated, in fact I want the simplest animation for learning purpose:
开发者_运维百科I just want to move a shape on a straight line.
Is it possible to do so in pure actionscript (with flex sdk only) by creating a timeline programmatically or without creating timeline ?
If you are using Flash Builder (or Flex Builder) you can accomplish this by creating an ActionScript project. I am a bit unclear when you say you want to use the Flex SDK but in pure ActionScript since Flex is a framework for ActionScript. So if you are talking about mx Effects and Spark Effects please let us know.
The following is for a pure ActionScript 3.0 project.
Ok so you will need a class that extends MovieClip
say SimpleFlash
. You can extend Sprite
instead if you do not need to use a timeline.
import flash.display.MovieClip;
public class SimpleFlash extends MovieClip {
public function SimpleFlash() {
}
}
We will need a simple shape to be placed in our SimpleFlash
constructor,
var simpleShape:Shape = new Shape();
simpleShape.graphics.beginFill(0xFF0000);
simpleShape.graphics.drawRect(0, 0, 100, 100);
simpleShape.graphics.endFill();
simpleShape.x = 0;
And we will have to add an event listener to the shape so that when we enter the first frame, we can execute some animation.
simpleShape.addEventListener(Event.ENTER_FRAME,simpleAnimation);
Do not forget to add it to the display list
addChild(simpleShape);
In our simpleAnimation function (called two lines up) we want to move the shape in a straight line along the x axis.
public function simpleAnimation(event:Event):void {
event.target.x += 1;
}
The shape after will move 1 unit to the right every time the Event of ENTER_FRAME is fired. There you go a simple animation without the timeline.
What you can learn from this ?
- Change
1
to another number see what happens. Does the shape move faster or slower ? - Change
event.target.x
toevent.target.y
which way does it go ? - Can you make it go diagonally ? How ?
精彩评论