This page is optimized for mobile devices, if you would prefer the desktop version just
click here
The main class for the project named animation01.
/*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
The main class for the project named animation07.
/*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
The moveableimage class for the project named animation07.
/*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
The main class for the project named animation01a.
/*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;
Miscellaneous
This section contains a variety of miscellaneous materials.
Housekeeping material
- Module name: Animation Fundamentals
- Files:
- ActionScript0150\ActionScript0150.htm
- ActionScript0150\Connexions\ActionScriptXhtml0150.htm
PDF disclaimer: Although the Connexions
site makes it possible for you to download a PDF file for thismodule at no charge, and also makes it possible for you to
purchase a pre-printed version of the PDF file, you should beaware that some of the HTML elements in this module may not translate well into
PDF.
Deployment of FlashDevelop projects on the Connexions website: As of the initial publication of this module, the Connexions websiterequires that all resources be located in the same folder as the file named
index.cnxml . However, the output from a
release build with FlashDevelop places a JavaScript file named
swfobject.js in a folder named
js that is a child of the folder that
contains all of the other files, including the file named
index.html .
You can flatten this structure for deployment on the Connexions website bymoving the file named
swfobject.js into the folder that
contains
index.html , and then modifying one line of html
code in the file named
index.html to cause it to refer to
the file named
swfobject.js in the same folder as itself.
-end-
Read also:
OpenStax, Object-oriented programming (oop) with actionscript. OpenStax CNX. Jun 04, 2010 Download for free at http://cnx.org/content/col11202/1.19
Google Play and the Google Play logo are trademarks of Google Inc.