PDA

View Full Version : how to run a function only once


wall28
04-28-2011, 08:00 PM
Hi, I just created a game through a tutorial and I have created a death and main menu sequence (they take you to the main menu frame). The problem is flash calls a new update() function each time the game is played thus causing a weird error (the game speed increases). is there a way i can have this function only called on once?

Also I had to add a return; command to the function with a if statement so it wouldnt keep running while in the main menu.

package
{
import flash.display.*;
import flash.events.*;
import flash.geom.*;
import flash.text.*;
import flash.utils.*;
import flash.ui.*;
import flash.sensors.Accelerometer;
import flash.events.AccelerometerEvent;


import Game.*;





public class GameController extends MovieClip
{
private var playerScore:Number;
private var bricks:Array;
private var scrollSoFar:Number;
private var player:Player;
private var moveX:Number;
private var jump:Boolean;
private var lemon:Boolean=true;
var accel:Accelerometer;

public function GameController()
{

}

public function startGame()
{
playerScore = C.PLAYER_START_SCORE;

bricks = new Array();

scrollSoFar = 0;
moveX = 0;


//Add first 3 bricks for player to step on


//Add player on to stage

player = new Player(C.PLAYER_START_X, C.PLAYER_START_Y);
mcGameStage.addChild(player);









mcGameStage.addEventListener(Event.ENTER_FRAME,upd ate);

//Handle event when this game is being preloaded
addEventListener(Event.ADDED_TO_STAGE, gameAddedToStage );

//Handle situations when this game is being run directly

//*****************accelerometer******************** ******
function accelMove(event:AccelerometerEvent):void
{
player.x -= event.accelerationX * 20;
if (player.x < 0)
{
player.x = 0;
}
else if (player.x > (stage.stageWidth) )
{
player.x = stage.stageWidth;
}
}
accel = new Accelerometer();
accel.setRequestedUpdateInterval(.1);
if (Accelerometer.isSupported)
{
accel.addEventListener(AccelerometerEvent.UPDATE, accelMove);
}
else
{
//If there is no accelerometer support...
}



if (stage != null)
{

}
}

private function gameAddedToStage(evt: Event):void
{

}



public function update(evt:Event)
{

if(lemon ==false)
{

if(this.currentFrame!=1)
{
gotoAndPlay(1);


}



return;

}//******************
//Handle User Input
//******************
if (moveX > 0)
player.moveRight();
else if (moveX < 0)
player.moveLeft();
else if (moveX == 0)
player.stopMoving();


//******************
//Handle Game Logic
//******************
//Check to spawn bricks
scrollSoFar += C.BRICK_SPEED;
if (scrollSoFar % C.SPAWN_BRICK_FACTOR == 0)
{
var randomXPos = Math.floor(Math.random() * C.GAME_WIDTH/1.6) +
C.GAME_WIDTH/480;

var newBrick = new Brick(randomXPos, C.BRICK_SPAWN_Y);
bricks.push(newBrick);

mcGameStage.addChildAt(newBrick,0);
}

//Update Bricks
for (var i=bricks.length - 1; i >= 0; i--)
{
bricks[i].update();


if (bricks[i].notInScreen())
{
mcGameStage.removeChild(bricks[i]);

bricks.splice(i,1);

//Add score
}
}

player.update()






//******************This is what controls if the player hits the bottom of the screen******************

if (player.hitTestObject(bottom))
{

player.hitFloorb(bottom);

}

if (lemon==true)
{
if (player.hitTestObject(top))
{
removeChild(benter);
lemon=false;



}
}


//Check for collisions
if (player.isInAir() && player.isFalling())
{
for (var i=bricks.length - 1; i >= 0; i--)
{
if (player.hitAreaFloor.hitTestObject(bricks[i]))
{
playerScore += C.SCORE_PER_BRICK;
player.hitFloor(bricks[i]);
}

}
}


//******************
//Handle Display
//******************
//Display new Score
txtScorePlayer.text = String(playerScore);
}



//public functions for children to invoke

}
}

kretzu
04-28-2011, 08:32 PM
you can use a variable:
boolean=false before the function and change it to true inside your function. when you call the function check if the boolean is false-call function or true-don't call the function