Hi,
After much help from people on this forum I’ve nearly completed building my first game in flash. However I’m having some problems restarting the game.
When either my avatar health bar runs out or the secondcounter gets to zero it releases an dispatchEvent saying avatar is dead which then runs onAvatarDeath function and adds the game over screen.
public function onAvatarDeath( avatarEvent:AvatarEvent ):void
{
var finalScore:Number = playScreen.getFinalScore();
removeChild(playScreen);
removeEventListener(Event.ENTER_FRAME, onEnterFrame);
flash.media.SoundMixer.stopAll()
gameOverScreen = new GameOverScreen();
gameOverScreen.addEventListener( NavigationEvent.RESTART, onRequestRestart );
gameOverScreen.x = 0;
gameOverScreen.y = 0;
gameOverScreen.setFinalScore( finalScore );
addChild( gameOverScreen );
playScreen = null;
}
When the game over screen is added you can then restart the game which runs these functions.
public function onRequestRestart( navigationEvent:NavigationEvent ):void
{
restartGame();
}
public function restartGame():void
{
currentIndex = 0;
questionsAsked = 0;
questionsAnswered.splice(0,questionsAnswered.length);
removeEventListener(Event.ENTER_FRAME, convertScore);
time=0;
overalltime = 0;
overallTime = 10;
score = 0;
health.meter.width=89;
quizQuestions = new Array();
addMainBackgrounds();
addBubbles();
addFish();
addYellowFish();
addBlueFish();
addQuizBackgrounds();
createQuestions();
createButtons();
createStatusBox();
addAllQuestions();
hideAllQuestions();
firstQuestion();
gameOverScreen = null;
}
The game restarts and all the arrays are cleared. Time is set back to zero however if the game gets this far it starts to lag and crashed. I believe I need to remove some of the child’s and event listeners from the game when it’s over but unsure how to do this in a different class. The class this the game in is here.
public function Game() {
currentLevelData=new LevelData(1);
startSecondCounter=new SecondCounter ;
backgroundMusic=new BackgroundMusic ;
bgmSoundChannel=backgroundMusic.play();
bgmSoundChannel.addEventListener(Event.SOUND_COMPLETE,onBackgroundMusicFinished);
downKeyIsBeingPressed=false;
upKeyIsBeingPressed=false;
leftKeyIsBeingPressed=false;
rightKeyIsBeingPressed=false;
useMouseControl=false;
Mouse.hide();
predator=new Array ;
var newEnemy=new Enemy(700,300);
predator.push(newEnemy);
addChild(newEnemy);
prey=new Array ;
var newFood=new Food(640,200);
prey.push(newFood);
addChild(newFood);
avatar=new Avatar ;
addChild(avatar);
if (useMouseControl) {
avatar.x=mouseX;
avatar.y=mouseY;
} else {
avatar.x=200;
avatar.y=250;
}
addEventListener(Event.ENTER_FRAME, onTick);
addEventListener(Event.ADDED_TO_STAGE,onAddToStage);
}
public function onBackgroundMusicFinished(event:Event):void {
bgmSoundChannel=backgroundMusic.play();
bgmSoundChannel.addEventListener(Event.SOUND_COMPLETE,onBackgroundMusicFinished);
}
public function onAddToStage(event:Event):void {
stage.addEventListener(KeyboardEvent.KEY_DOWN,onKeyPress);
stage.addEventListener(KeyboardEvent.KEY_UP,onKeyRelease);
}
public function onKeyPress(keyboardEvent:KeyboardEvent):void {
if (keyboardEvent.keyCode==Keyboard.DOWN) {
downKeyIsBeingPressed=true;
} else if (keyboardEvent.keyCode==Keyboard.UP) {
upKeyIsBeingPressed=true;
} else if (keyboardEvent.keyCode==Keyboard.LEFT) {
leftKeyIsBeingPressed=true;
} else if (keyboardEvent.keyCode==Keyboard.RIGHT) {
rightKeyIsBeingPressed=true;
}
}
public function onKeyRelease(keyboardEvent:KeyboardEvent):void {
if (keyboardEvent.keyCode==Keyboard.DOWN) {
downKeyIsBeingPressed=false;
} else if (keyboardEvent.keyCode==Keyboard.UP) {
upKeyIsBeingPressed=false;
} else if (keyboardEvent.keyCode==Keyboard.LEFT) {
leftKeyIsBeingPressed=false;
} else if (keyboardEvent.keyCode==Keyboard.RIGHT) {
rightKeyIsBeingPressed=false;
}
}
public function onTick(event:Event):void {
if (Math.random()<currentLevelData.enemySpawnRate){
var randomyEnemy:Number=Math.random()*425;
var randomyFood:Number=Math.random()*425;
var newEnemy:Enemy=new Enemy(650,randomyEnemy);
predator.push(newEnemy);
addChild(newEnemy);
var newFood:Food=new Food(650,randomyFood);
prey.push(newFood);
addChild(newFood);
}
if (useMouseControl) {
avatar.x=mouseX;
avatar.y=mouseY;
} else {
if (downKeyIsBeingPressed) {
avatar.moveABit(0,1);
} else if (upKeyIsBeingPressed) {
avatar.moveABit(0,-1);
} else if (leftKeyIsBeingPressed) {
avatar.moveABit(-1,0);
} else if (rightKeyIsBeingPressed) {
avatar.moveABit(1,0);
}
}
if (avatar.x<avatar.width/2) {
avatar.x=avatar.width/2;
}
if (avatar.x>600-avatar.width/2) {
avatar.x=600-avatar.width/2;
}
if (avatar.y<avatar.height/2) {
avatar.y=avatar.height/2;
}
if (avatar.y>425-avatar.height/2) {
avatar.y=425-avatar.height/2;
}
var enemyHasBeenHit:Boolean=false;
var isSoundPlaying:Boolean=false;
var numberPlaying:Number=0;
for each (var enemy:Enemy in predator) {
enemy.moveABit();
if (PixelPerfectCollisionDetection.isColliding(avatar,enemy,this,true)) {
health.meter.width--;
avatar.gotoAndPlay(2);
}
}
for(var i:int = 0; i<prey.length; i++) {;
prey*.moveABit();
if (PixelPerfectCollisionDetection.isColliding(avatar,prey*,this,true)) {
gameScore.addToValue(20);
/*getPoint=new GetPoint ;
getPoint.volume = 0.01;
pointChannel=getPoint.play();*/
prey*.parent.removeChild(prey*);
prey.splice(i,1);
break;
}
if(prey*.x < - 20){
prey*.parent.removeChild(prey*);
prey.splice(i,1);
}
for(var j:int = 0; j<predator.length; j++) {;
if(predator[j].x < - 20){
predator[j].parent.removeChild(predator[j]);
predator.splice(j,1);
}
}
}
if (gameScore.currentValue>=currentLevelData.pointsToReachNextLevel) {
currentLevelData=new LevelData(currentLevelData.levelNum+1);
//setBackgroundImage();
}
/*if(health.meter.width<15){
if(health.meter.width>14){
heartbeatSound=new HeartbeatSound;
heartbeatChannel=heartbeatSound.play();
trace("going to die");
}
}*/
if (health.meter.width<1) {
//gameTimer.stop();
bgmSoundChannel.stop();
/*heartbeatChannel.stop();
pointChannel.stop();*/
removeGameListeners();
dispatchEvent(new AvatarEvent(AvatarEvent.DEAD));
}
}
public function getFinalScore():Number {
return gameScore.currentValue;
}
I hope I’ve explained this well enough and included all the code needed
Anyone any ideas?
Thanks in advance