Step 3: Game Programming – Actionscript
Now add the program functions and logic to frame 1 of the actions layer. Open the actions panel and type the following script, or copy/paste the code - you'll learn more if you type it though. The game script is explained below the sample code. Variables Functions Student Challenge
/* create and initialise variables */ var clicks:Number = 0; var score:Number = 0; var miss:Number = 0; var maxClicks:Number = 25; var tx:Number = 600; /* add a function to the intro movie clip that will call the initialise function when intro is clicked */ intro.onRelease = function() { init(); }; /* init function does the following: hide the intro movie clip initialise the scoreboard reset game variables position the target call play the game function. */ init = function () { intro._visible = false; scoreboard.clicks.text = 0; scoreboard.score.text = 0; scoreboard.miss.text = 0; clicks = 0; score = 0; miss = 0; target1._x = tx; target1._y = Math.random()*250+50; target1.speed = Math.random()*12+2; target1.clicked = false; playGame(); }; /* function to control game play. this is a simple game engine */ playGame = function () { // hide the mouse Mouse.hide(); /* show the cursor set boundaries for the cursor so that it stays within the game */ startDrag("cursor", true, 30, 30, 520, 370); /* attach function to the target to: move the target test to see if it has crossed the screen and if so, reset itself */ target1.onEnterFrame = function() { this._x -= this.speed; if (this._x<-30) { miss++; scoreboard.miss.text = miss; this._x = tx; this._y = Math.random()*280+50; this.speed = Math.random()*10+2; } }; /* test if target has been hit, if so, add to score and pop the balloon !! NOTE !! see actions inside the target clip last frame to reset the target. */ target1.onPress = function() { if (!this.clicked) { this.clicked = true; clicks++; score++; scoreboard.clicks.text = clicks; scoreboard.score.text = score; this.gotoAndPlay("pop"); } }; // add to clicks if mouse clicked and target missed bg.onPress = function() { clicks++; scoreboard.clicks.text = clicks; }; // test to see if game over this.onEnterFrame = function() { if (clicks>=maxClicks) { gameOver(); } }; }; /* Game Over show mouse, hide cursor and delete all game functions Show intro screen again */ gameOver = function () { Mouse.show(); stopDrag(); cursor._x = tx; delete target1.onEnterFrame; delete target1.onPress; delete bg.onPress; delete this.onEnterFrame; intro._visible = true; };
Actionscript Explained:
NOTE: For more information on any code highlighted in blue, search in Flash Help - Actionscript Reference.
Variables:
var clicks:Number = 0; - Counter to track the player's mouse clicks. var score:Number = 0; - Counter to keep track of the number of balloons popped. var miss:Number = 0; - Counter to track how many balloons were not hit. var maxClicks:Number = 25; - Maximum number of clicks available to the player var tx:Number = 600; - Value to set the target's X property when it is reset.
Top of page
Functions:
1. Function to begin the game. When the player clicks the play game (intro) movie clip the init function will be called. intro.onRelease = function() { init(); }; 2. Function to initialise the game variables and call the start game function. The init function does the following:
- hides the introduction screen
- resets the scoreboard and counter variables
- sets the initial position, status and speed of the target.
- call the playGame function.
init = function () { intro._visible = false; scoreboard.clicks.text = 0; scoreboard.score.text = 0; scoreboard.miss.text = 0; clicks = 0; score. = 0; miss = 0; target1._x = tx; target1._y = Math.random()*250+50; target1.clicked = false; target1.speed = Math.random()*10+2; playGame(); }; Top of page 3. Function to start and maintain control of the game. The playGame function provides the following:
- hide the mouse
- attach the cursor movie clip to follow mouse movement.
- attach a function to the target movie clip to control movement and increment misses
- attach a function to the target movie clip to respond to mouse clicks that will:
- record a click
- increment the score
- tell the target to pop, explode, etc. (reset the target – see target clip actions)
- reset the speed of the target.
- attach a function to the background movie clip (bg) to respond to mouse clicks
- attach a function to the game to check if the game is over and call the game over function.
playGame = function () { // hide the mouse and show the cursor Mouse.hide(); startDrag("cursor", true, 30, 30, 520, 370); // move the target target1.onEnterFrame = function() { this._x -= this.speed; if (this._x<-30) { miss++; scoreboard.miss.text= miss; this._x = tx; this._y = Math.random()*280+50; this.speed = Math.random()*10+2; } }; // test if target hit, add to score and number of clicks and reset target // ** NOTE ** see actions in target clip last frame for reset! target1.onPress = function() { if (!this.clicked) { this.clicked = true; clicks ++; score++; scoreboard.clicks.text = clicks; scoreboard.score.text = score; this.gotoAndPlay("pop"); } }; // add to clicks if target missed bg.onPress = function() { clicks ++; scoreboard.clicks.text = clicks; }; // test for game over scoreboard.onEnterFrame = function() { if (this.clicks>=numClicks) { gameOver(); } }; }; Top of page 4. Function to end the game and prompt the user to play again. This function performs the following:
- show the mouse
- disconnect the cursor clip from the mouse
- move (hide) the cursor movie clip
- delete the functions created in the startGame function
- show the introduction screen to prompt the player to play the game.
gameOver = function () { Mouse.show(); stopDrag(); cursor._x = tx; delete target1.onEnterFrame; delete target1.onPress; delete bg.onPress; delete this.onEnterFrame; intro._visible = true; };
Student Challenge:
1. Create a “Game Over” movie clip to use instead of the intro movie clip when the game ends. 2. Add another 2 – 3 targets to the game. (can be instances of the same target) 3. Add a surprise target that will add to, or subtract from, the number of clicks remaining. 4. Add the concept of levels to the game. Each level could for example:
- increase the speed of the targets
- increase or decrease the number of targets
- make the targets smaller
- make the targets move in a different direction (up, down, diagonal, wobble, etc)
- anything else you can think of
Have fun
|