ActionScript Development
Listing 2. ActionScript code for Alien.as
//Updates the score variable on stage referrer.score = referrer.score + 1;
//package defines the folder where this external code is located package Scripts { //imports the base SimpleButton class import flash.display.SimpleButton; //import Event native class (for refresh frame event handling) import flash.events.Event; //import mouse events native class (for mouse click handling) import flash.events.MouseEvent; //this class extends a native class SimpleButton public class Alien extends SimpleButton { //create empty reference variable for later use var referrer; //Constructor function for this particle class //This function is executed when the object is first created //"startX" and "startY" are the starting coordinates //The "refer" parameter is used to refer back to the stage public function Alien(startX:Number,startY:Number,refer) { //set the initial position of the object //create variable for the speed var speed:Number;
//Sets the score text on the stage to display the new score referrer.scoreText.text = "Highscore: " + referrer.highScore + " Score: " + referrer.score;
//execute this object's remove function remove(); }
//This is the function called when the frame is refreshed (as per fps) function mainEnterFrame(event:Event):void { //move the alien to the right x = x + speed; //test to see if the alien has moved accross the screen completely (no longer visible) if (x > 680) { //test to see if the score is higher than the high score if (referrer.score > referrer.highScore) { //set the high score to the current score referrer.highScore = referrer.score; }
//resets the score to 0
referrer.score = 0;
x = startX; y = startY;
//update score display referrer.scoreText.text = "Highscore: " + referrer.highScore + " Score: " + referrer.score;
//set a random speed for the alien to move speed = 2 + (Math.random() * 6);
//set the size of the alien to be random (between 0 and 1)
//execute the remove function remove();
scaleX = 0.3 + Math.random(); scaleY = 0.3 + Math.random();
}
}
//The empty reference will now refer to the stage referrer = refer;
//function used to remove object completely public function remove() { //remove this object and its listeners this .removeEventListener(Event.ENTER_FRAME, mainEnterFrame); this .removeEventListener(MouseEvent.CLICK, clickHandler); //remove this object from the stage this .parent.removeChild( this ); //remove object from memory delete this ; } } }
//Add listener for when frame is refreshed this .addEventListener(Event.ENTER_FRAME, mainEnterFrame);
//Add listener for when the alien is clicked by the mouse this .addEventListener(MouseEvent.CLICK, clickHandler);
}
//This is the function called when the user clicks on the alien function clickHandler(event:MouseEvent):void {
03/2009 (5)
28
Made with FlippingBook HTML5