<< Chapter < Page | Chapter >> Page > |
/*Project Animation01
Extremely simple animation project.This is an AS3 project and not a Flex project.
Uses the ENTER_FRAME event for timing.Causes a filled blue circle that is drawn on a
transparent Sprite object to move diagonallyfrom left to right across the Flash window.
The sprite with the circle moves out of theFlash window at the bottom right.
*********************************************************/pacpackage {
import flash.display.Sprite;import flash.events.Event;public class Main extends Sprite{
private var sprite:Sprite;private var dx:Number = 3;//x-movement distance
private var dy:Number = 2;//y-movement distanceprivate var radius:Number = 24;//radius of circlepublic function Main():void {
sprite = new Sprite();//Enable the following statement to cause the// sprite background to be visible.
//sprite.opaqueBackground = true;//Draw a filled circle on the Sprite object;
sprite.graphics.beginFill(0x0000ff, 1.0);sprite.graphics.drawCircle(radius,radius,radius);
sprite.graphics.endFill();addChild(sprite);//default location is 0,0addEventListener(Event.ENTER_FRAME, onEnterFrame);
}//end constructor//--------------------------------------------------////Event handler.
public function onEnterFrame(event:Event):void {sprite.x += dx;
sprite.y += dy;}//end onEnterFrame}//end class}//end package
/*Project Animation07
Classical bouncing ball project written as an AS3 project.However, in this case the ball is a Sprite object with
an embedded image of Dick Baldwin's caricature.Causes the image to bounce around inside of a 450 x 500
rectangle.*********************************************************/
package {import flash.display.Sprite;
import flash.events.Event;public class Main extends Sprite{private var moveableImage:MoveableImage
private var rectWidth:uint = 450;private var rectHeight:uint = 500;public function Main() {//Draw a black rectangle with a yellow background.
this.graphics.beginFill(0xFFFF00,1.0);this.graphics.lineStyle(3, 0x000000);
this.graphics.drawRect(0, 0, rectWidth, rectHeight);this.graphics.endFill();
moveableImage =new MoveableImage(rectWidth,rectHeight);
addChild(moveableImage);//default location is 0,0addEventListener(Event.ENTER_FRAME, onEnterFrame);}//end constructor
//--------------------------------------------------////Event handler.public function onEnterFrame(event:Event):void {
//Ask the image to move.moveableImage.moveIt();
}//end onEnterFrame}//end class}//end package
/*Class MoveableImage
Constructs a Sprite with an embedded image that can bemoved around inside of a rectangle for which the
dimensions are passed to the constructor.*********************************************************/
package {import flash.display.Sprite;
import flash.events.Event;import flash.display.Bitmap;public class MoveableImage extends Sprite{
private var dx:Number = 4;//x-movement distanceprivate var dy:Number = 2;//y-movement distance
private var rectWidth:uint;private var rectHeight:uint;
private var imageWidth:uint;private var imageHeight:uint;[Embed(source='/baldwin.jpg')]private var imgClass:Class;
private var headImage:Bitmap = new imgClass ();public function MoveableImage(rectWidth:uint,rectHeight:uint) {
//Save the dimensions of the rectangle.this.rectWidth = rectWidth;
this.rectHeight = rectHeight;//Get and save the dimensions of the image.imageWidth = headImage.width;
imageHeight = headImage.height;//Add the image to the display list.addChild(headImage);
}//end constructor//--------------------------------------------------////Cause the Sprite object to move and bounce off of
// the edges of the rectangle.public function moveIt():void {
//Test for a collision with the left or right edge// of the rectangle.
if (((this.x + dx)>(rectWidth - imageWidth)
|| (this.x<0))) {
dx *= -1;//Reverse horizontal direction}//end if//Test for a collision with the top or the bottom
// of the rectangle.if (((this.y + dy)>(rectHeight - imageHeight)
|| (this.y<0))) {
dy *= -1;//Reverse vertical direction.}//end if//Make the move.
this.x += dx;this.y += dy;
}//end onEnterFrame}//end class}//end package
/*Project Animation01A
Extremely simple animation program.This is an AS3 project and not a Flex project.
This program is essentially the same as Animation01except that this program creates a Timer object and
uses TIMER events in place of ENTER_FRAME events fortiming.
Causes a filled blue circle that is drawn on atransparent Sprite object to move diagonally
from left to right across the Flash window.The sprite with the circle moves out of the
Flash window at the bottom right.*********************************************************/
package {import flash.display.Sprite;
This section contains a variety of miscellaneous materials.
-end-
Notification Switch
Would you like to follow the 'Object-oriented programming (oop) with actionscript' conversation and receive update notifications?