//**************VARIABLES**************/
Var gamestate;String (this creates a string variable with no value)
//*********BEGINNING STUFF**************/
endScreen.visible = false; (this line makes a movie clip called endScreen invisible)
//**************INTRO SCREEN**************/
introScreen.play_btn.addEventListener (MouseEvent.CLICK, clickAway);
function clickAway (event:MouseEvent):void {
moveScreenOff (introScreen);
}
function moveScreenOff (screen:MovieClip):void {
screen.x = (screen.width)*-1;
gameState = "INITGAME";
addEventListener (Event.ENTER_FRAME, gameLoop);
}
//**************STATE_INIT_GAME**************/
function initGame ():void {
trace("add stuff to initGame")
gameState = "STARTPLAYER";
}
//**************STATE_START_PLAYER**************/
function startPlayer ():void {
trace("add player stuff")
gameState = "PLAYGAME";
}
(This code is the same as STATE INIT GAME code)
//**************STATE_PLAY_GAME**************/
function playGame ():void {
makeEnemies ();
testForEnd ();
}
function makeEnemies ():void {
}
function testForEnd ():void {
gameState = "ENDGAME";
}
//**************STATE ENDGAME**************/
function endGame ():void {
removeGame ();
endScreen.visible = true;
removeEventListener (Event.ENTER_FRAME, gameLoop);
showResults ();
}
function showResults ():void {
trace ("Show Results");
endScreen.play_btn.addEventListener (MouseEvent.CLICK, clickFinalAway);
function clickFinalAway (event:MouseEvent):void {
trace ("clickFinalAway");
moveScreenOff (endScreen);
}
}
//**************REMOVE GAME**************/
function removeGame ():void {
function moveScreenOff (screen:MovieClip):void (make the movie clip move off screen)
Screen.x (Is the axis which it is on X,Y
= (screen.width)*-1; the whole value moves it off screen by the width of the screen)
gameState = "INITGAME"; (is a variable and if gameState remembers "INGAME" it will do something.)
addEventListener (Event.ENTER_FRAME, gameLoop); (This created an enterframe listener, once activated it becomes a gameloop)
Function initGame ():void{ (It is the function)
Trace("add stuff to initGame") ( )
gameState = "STARTPLAYER"; ( )
function makeEnemies ():void{ (this calls the function make enemies)
makeEnemies ();
testForEnd ();
}
function showResults ():void {
trace ("Show Results");
endScreen.play_btn.addEventListener (MouseEvent.CLICK, clickFinalAway);
function clickFinalAway (event:MouseEvent):void {
trace ("clickFinalAway");
moveScreenOff (endScreen);
}
(This creates a function called showResults,it also creates a function called click final away)
//**************GAME STATES**************/
function gameLoop (e:Event):void {
switch (gameState) {
case "INITGAME":
initGame();
break;
case "STARTPLAYER":
startPlayer ();
break;
case "PLAYGAME" :
playGame ();
break;
case "ENDGAME" :
endGame ();
break;
}
}
We will now add collision detection and explosions. Nest this within the “for” loop in function moveEnemies
var tempLaser:MovieClip;
//Loop through the Lasers array
for (var j:int=Lasers.length-1; j>=0; j--) {
tempLaser = Lasers[j];
if (tempEnemy.hitTestObject(tempLaser)) {
//Remove enemy from enemies array
removeEnemy(i);
//Remove laser from Lasers array
removeLaser(j);
//Add to the score
}
}
When explosions are generated they need to be listed in an array. So declare this variable putting the following in the variables section:
var explosions:Array;
Put the following in the initGame function:
explosions = new Array();
function removeExplosion(idx:int) {
explosions = new Array();
create a function to make explosions, grabbing an instance from the movieClip symbol in the library.
function makeExplosion(ex:Number, ey:Number):void {
var tempExplosion:MovieClip;
tempExplosion = new Explosion();
tempExplosion.x = ex;
tempExplosion.y = ey;
addChild(tempExplosion);
explosions.push(tempExplosion);
}
Now to see when to get rid of the explosion. Put the following inside the “movelazer” function JUST BEFORE THE LAST CLOSING CURLY BRACE ( Whoo-hoo ---- another “for” loop, this time testing all the explosions
remembered in the explosions array. If they have finished their animation
then a new function “removeExplosion” is called
//Remove any tempExplosion from the explosions array that has already played
var tempExplosion:MovieClip;
for (i=explosions.length-1; i>=0; i--) {
tempExplosion = explosions[i];
if (tempExplosion.currentFrame >= tempExplosion.totalFrames){
removeExplosion(i);
}
}
This creates the function, removes the explosion from the stage, and
splices it out of the explosions array
removeChild(explosions[idx]);
explosions.splice(idx,1);
}
You need these new variables. Paste into variables section at the top: Create 3 DYNAMIC text boxes on the stage (mobile device – best text is Sans point size 24). Give instance names of: score_txt lives_txt level_txt
var score:Number;
var lives:Number;
Now you need to add script to display information into the text boxes you just made. This will happen in the initGame function (and elsewhere – explained later) When you script in a number into a text box, you have to turn the number into a string, so
you type the word “String” before the number which is in brackets
level_txt.text = ("LEVEL: " + String(level));
score = 0;
score_txt.text = ("SCORE: " + String(score));
lives = 2;
lives_txt.text = ("LIVES: " + String(lives));
We have made an explosion happen if the enemy gets to the bottom of the stage OR if it hits the player. Now, if either of these things happens, then the player LOSES A LIFE. So add the following script inside the function moveEnemies, inside the IF bit that tests getting getting to the bottom of the stage OR hitting the player:
//Subtract a life
lives--;
lives_txt.text = ("LIVES: " + String(lives));
Now to add to the score WHEN THE PLAYER SUCCEEDS. Paste the following inside the function moveEnemies, inside the IF bit that tests if the enemy hits the laser:
score++;
score_txt.text = ("SCORE: " + String(score));
Next we need to see if the player is successful and the level needs to increase or see if the player is bad and has lost all lives. So the processor needs to get to the testForEnd function. We had to comment out a line in this function. Paste the following into the testForEnd function, REPLACING THE SINGLE LINE THAT IS THERE ALREADY and commented The testForEnd function is called from the playGame function.
By changing the variable gameState to ENDGAME, the enterFrame script (gameLoop) will now call the
endgamefunction, not playGame. In this way the processer deals with all the stuff needed to action what
happens when the game is over.
score++;
score_txt.text = ("SCORE: " + String(score));
if (score > level * 10) {
level++;
level_txt.text = ("LEVEL: " + String(level));
}
//Check to see how many lives the user has
if (lives == 0) {
gameState = "ENDGAME"
trace(gameState);
}
TEST YOUR GAME. YOU WILL SEE THAT STUFF STAYS ON THE STAGE THAT SHOULDN’T BE THERE.
The endgame function calls the removeGame function then when that has been processed, calls theshowResults function. Paste the following into the removeGame function:
//Remove enemies, the lasers, the explosions (from stage and array) and the player
for (var i:int = enemies.length-1; i >=0; i--) {
removeEnemy(i);
enemies.splice(i);
}
for (var j:int = Lasers.length-1; j >=0; j--) {
removeLaser(j);
Lasers.splice(j);
}
for (var k:int = explosions.length-1; k >=0; k--) {
removeExplosion(k);
explosions.splice(k)
}
removeChild(player);
laserTimer.stop();
If you test the game now, all should be working – except on gameover of a REPEAT play, the end screen does not appear, that’s because it was moved left of stage in the function moveScreenOff
which we did ages ago, so it needs to be bought back to centre stage when it is made visible. Add the following to the endGame function
endScreen.x= stage.stageWidth/
We now have a very basic working game. So time to make it playable on a mobile device.
The player will move using the accelerometer feature of the device, so create an Accelerometer type variable, pastinfg this at the top of the script:
var accel:Accelerometer;
Now creat the accelerometer paste this in the function startPlayer before “If the phone has accelerometer support...”
accel = new Accelerometer();
Then, inside the if bit that the processor accesses IF the device has an accelrometer facility paste the next:
The above script creates a listener for the device movement, and on receiving this trigger calls a new function. So now create the function. Paste this anywhere in column 1
function accelMove(event:AccelerometerEvent):void{
//accelerationX returns a number between 0,1. Properties: Y, Z.
player.x -= event.accelerationX * 80;
if (player.x < 0) {
player.x = 0;
}
else if (player.x > (stage.stageWidth - player.width) ) {
player.x = stage.stageWidth - player.width;
}
}
Basically the code in this function moves the player in the x axis relative to the movement
of the phone, but with the constraint that if the player gets to the left or the right of the
stage, it is held there and cannot go outside the stage area.
If the app user answers a phone call, or deactivates the app in any other way, it is good practice to pause everything, so here follows 2 listeners, listening for de-activation and activation. Paste these into column 1:
If these listeners are triggered they call functions (pauseGame() OR resumeGame
//When the user gets a phone call, or launches another app...
stage.addEventListener(Event.DEACTIVATE, Deactivate);
function Deactivate(event:Event):void{
pauseGame();
}
//When the user comes back to the game...
stage.addEventListener(Event.ACTIVATE, Activate);
function Activate(event:Event):void{
resumeGame();
}
Write functions for the two listeners above and paste into column 1:
If the enterFrame listener is not there, the processor cannot call any function and the game will pause
If the timer is stopped the lsers will not be generated
If the accelerometer listener is not there, the processor cannot call the function to move the player.
function pauseGame():void{
//Remove any listener events, timers etc.
if (gameState == "PLAY_GAME") {
removeEventListener(Event.ENTER_FRAME, gameLoop);
laserTimer.stop();
accel.removeEventListener(AccelerometerEvent.UPDATE, accelMove);
}
}
No comments:
Post a Comment