Hi guys! I’m new to AS3 and they say one of the best ways to learn anything is to push yourself. So I’ve given myself this assignment based on the book I’m reading(Foundation ActionScript 3.0 with Flash and Flex). I will need some help though.
The assignment is based on the following example in Chapter 1 of the book: a flash movie with balls bouncing all over the screen. The example uses the external classes below:
The Ball class:
package {
// Import necessary classes
import flash.display.MovieClip;
import flash.events.Event;
import flash.geom.ColorTransform;
import flash.geom.Rectangle;
public class Ball extends MovieClip {
// Horizontal speed and direction
public var speedX:int = 10;
// Vertical speed and direction
public var speedY:int = -10;
// Constructor
public function Ball() {
addEventListener(Event.ENTER_FRAME, onEnterFrame);
// colors the ball a random color
var colorTransform:ColorTransform = new ColorTransform();
colorTransform.color = Math.random()*0xFFFFFF;
transform.colorTransform = colorTransform;
}
// Called every frame
private function onEnterFrame(event:Event):void {
// Move ball by appropriate amount
x += speedX;
y += speedY;
// Get boundary rectangle for ball
var bounds:Rectangle = getBounds(parent);
// Reverse horizontal direction if collided with left or right
// of stage.
if (bounds.left < 0 || bounds.right > stage.stageWidth) {
speedX *= -1;
}
// Reverse vertical direction if collided with top or bottom
// of stage.
if (bounds.top < 0 || bounds.bottom > stage.stageHeight) {
speedY *= -1;
}
}
}
}
The main class:
package {
import flash.display.MovieClip;
import flash.events.MouseEvent;
public class MultiBounce extends MovieClip {
// Number of balls to create
private static const NUM_BALLS:uint = 50;
// Constructor
public function MultiBounce() {
stage.addEventListener(MouseEvent.MOUSE_DOWN, onStageClick);
}
// handler for when stage is clicked, creates balls
private function onStageClick(pEvent:MouseEvent):void {
stage.removeEventListener(MouseEvent.MOUSE_DOWN, onStageClick);
// For each ball to be created
for (var i:uint = 0; i < NUM_BALLS; i++) {
// Create new Ball instance
var ball:Ball = new Ball();
// Places ball at mouse click
ball.x = pEvent.stageX;
ball.y = pEvent.stageY;
// Specify random speed and direction
ball.speedX = (Math.random() * 30) - 15;
ball.speedY = (Math.random() * 30) - 15;
// Add new Ball to stage
addChild(ball);
}
}
}
}
My assignment for myself has actually two parts:
- How do I achieve the same result by using external classes without library symbols.
- How do I achieve the same result with pure timeline coding.
(1) With regard to assignment 1(external classes) I managed to create a Ball class (sprite) with the following code:
package {
import flash.display.Sprite;
import flash.events.Event;
import flash.geom.Rectangle;
public class Ball extends Sprite
{
private var ball:Sprite = new Sprite ;
private var speedX:int = +10;
private var speedY:int = -10;
public function Ball()
{
ball.graphics.beginFill(Math.random() * 0xffffff);
ball.graphics.drawCircle(0,0,20);
ball.graphics.endFill();
ball.x = stage.stageWidth / 2;
ball.y = stage.stageHeight - ball2.height;
addChild(ball);
addEventListener(Event.ENTER_FRAME, enterFrameHandler);
}
private function enterFrameHandler(event:Event):void
{
ball.x += speedX;
ball.y += speedY;
var bounds:Rectangle = ball.getBounds(stage);
if (bounds.left < 0 || bounds.right > stage.stageWidth)
{
speedX *= -1;
}
if (bounds.top < 0 || bounds.bottom > stage.stageHeight)
{
speedY *= -1;
}
}
}
}
How do I construct the main class based on this Ball class?
(2) As to assignment 2 (timeline coding) how do I modify the following code in the timeline (courtesy of cbeech and simranzenov) that will include mouse interactivity and producing exactly the same result as the book example?
var balls:Array = [];function createBalls( num:int ):void {
for( var i:int=0; i<num; i++ ) {
var ball:MovieClip = new MovieClip ;
ball.graphics.beginFill(Math.random()*0xff0000);
ball.graphics.drawCircle(0,0,10);
ball.graphics.endFill();
ball.x = 5+ (Math.random() * (stage.stageWidth-10));
ball.y = 5+ (Math.random() * (stage.stageHeight-10));
ball.speedX = 5+ (Math.random() * 5);
ball.speedY = 5+ (Math.random() * 5);
addChild(ball);
balls.push(ball);
}
}
function process( e:Event ):void {
for( var b in balls ) {
balls**.x += balls**.speedX;
balls**.y += balls**.speedY;
if( balls**.x-5 <= 0 || balls**.x+5 >= stage.stageWidth )
{balls**.speedX *=-1;
}
if( balls**.y-5 <= 0 || balls**.y+5 >= stage.stageHeight ) {balls**.speedY *=-1};
}
}
addEventListener( Event.ENTER_FRAME, process );
createBalls(50);