Flash_and_Flex_03_2009

Create your first ActionScript game in minutes

Properties panel and set the width to 640 and the height to 480. Align the rectangle to the stage by using the options on the Modify/Align menu. From this point remember that any non visible panels can be shown by selecting them on the Windowmenu. With that said, go to the color panel and select the paint bucket icon to bring up the fill options. Set the type to linear and create a gradient that roughly resembles the glow from a sunset or sunrise. Go ahead and experiment with the various colors and options available. Before

moving on, make sure that the alpha amount for the leftmost part of the gradient on the panel is zero. Select the rectangle on the stage and also select the Paint Bucket Tool from the leftmost panel. Hold the shift key and drag the mouse down across the rectangle. This will create a glow effect that starts at the bottom and gradually reveals more stars towards the top. You should now lock the SkyFlare layer as well, as we are going to add some static (non- moving) foreground objects to the stage. Create

a new layer on top of the existing layers called Texts . Use the Text Tool from the leftmost panel to create a text object at the top of the screen with the following text: Click to shoot down the aliens before they cross the sky! Select the new text object and go to the Properties panel. Set the text type to Static Text. Feel free to play around with the properties of the text object to make it look the way you want. Create a second text object at the bottom of the screen with the following text: Highscore:

Listing 1. ActionScript code for frame 1

//Create comments by adding "//" before text on a line

//the below function that stops application from going to the next frame //";" denotes the end of the statement //Various parameters can be passed to functions as you'll see further down stop(); //Create score and high score variables and set their initial amounts var score:Number = 0; var highScore:Number = 0; //Adds a listener to the stage for when frame is refreshed (according to framerate) addEventListener(Event.ENTER_FRAME, mainEnterFrame);

//This is the function called when the current frame is refreshed function mainEnterFrame(event:Event):void { // "{}" or brackets are used to mark the beginning and end of code to be executed, usually within a function

//Creates a number object with a decimal number between 0 and 1 var alienSpawn:Number = Math.random();

//if the number is above 0.95 (should happen 5% of the time) then execute the code below in the brackets if (alienSpawn > 0.95) { //execute the makeObject function and pass the name of the object to create along with the starting x and y positions makeObject("Scripts.Alien",-40,Math.random() * 440); } }

//function used to create and add a particular object to the stage function makeObject(className:String, startX:Number, startY:Number) { //create new object using the dynamically obtained class name var myClass:Class = getDefinitionByName(className) as Class; //declare new object with initial variables being passed to constructor //the refferer (this frame) is also passed to the object var clipInstance:Object = new myClass(startX,startY, this ); //create new reference to newly created object //(as container will not accept original "clipInstance" reference) var instance = clipInstance; //add the object as a child to the stage (display object container) this .addChild(instance); }

03/2009 (5)

27

Made with FlippingBook HTML5