# Cannot control character after replay

**URL:** https://forum.kirupa.com/t/cannot-control-character-after-replay/332330
**Category:** flash
**Created:** [April 27, 2014, 9:23am UTC](https://forum.kirupa.com/t/cannot-control-character-after-replay/332330 "2014-04-27T09:23:29Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![Ayasha29](https://avatars.discourse-cdn.com/v4/letter/a/3ec8ea/32.png) [@Ayasha29](https://forum.kirupa.com/u/Ayasha29)
#### Post date: [April 27, 2014, 9:23am UTC](https://forum.kirupa.com/t/cannot-control-character-after-replay/332330/1 "2014-04-27T09:23:29Z")

</div>

Hi guys.

I made a platform game. The code that I made works fine but when when the player is game over and I press the button to replay I cannot control my character any more. Any help would be appreciated.

Here the code:

stop();

import flash.events.KeyboardEvent;  
import flash.events.Event;  
import flash.display.MovieClip;  
import flash.utils.Timer;  
import flash.display.Sprite;

//vars (define events and functions)  
var KeyThatIsPressed:uint;  
var rightKeyIsDown:Boolean=false;  
var leftKeyIsDown:Boolean=false;  
var upKeyIsDown:Boolean=false;  
var downKeyIsDown:Boolean=false;  
var spaceKeyIsDown:Boolean=false;

var hitObstacle:Boolean=false; // keeps track if obstacle is hit  
var health=6;  
health\_txt.text=health.toString();

var score=0;  
coinCount\_txt.text=score.toString();  
var hitCoin:Boolean=true;

var timeleft=15;  
timetxt.text=String(timeleft);  
var gameTimer = new Timer(1500);

//propreties for player  
stage.addEventListener(KeyboardEvent.KEY\_DOWN, PressAKey);  
stage.addEventListener(KeyboardEvent.KEY\_UP, ReleaseAKey);  
stage.addEventListener(KeyboardEvent.KEY\_DOWN, moveThePlayer);

function PressAKey(event:KeyboardEvent):void  
{  
if(event.keyCode==Keyboard.RIGHT)  
{  
rightKeyIsDown=true;  
}  
if(event.keyCode==Keyboard.LEFT)  
{  
leftKeyIsDown=true;  
}  
if(event.keyCode==Keyboard.UP)  
{  
upKeyIsDown=true;  
}  
if(event.keyCode==Keyboard.DOWN)  
{  
downKeyIsDown=true;  
}  
if(event.keyCode==Keyboard.SPACE)  
{  
spaceKeyIsDown=true;  
}  
}

function ReleaseAKey(event:KeyboardEvent):void  
{  
if(event.keyCode==Keyboard.RIGHT)  
{  
rightKeyIsDown=false;  
}  
if(event.keyCode==Keyboard.LEFT)  
{  
leftKeyIsDown=false;  
}  
if(event.keyCode==Keyboard.UP)  
{  
upKeyIsDown=false;  
}  
if(event.keyCode==Keyboard.DOWN)  
{  
downKeyIsDown=false;  
}  
if(event.keyCode==Keyboard.SPACE)  
{  
spaceKeyIsDown=false;  
}  
}

function moveThePlayer(event:KeyboardEvent):void {

```
if(rightKeyIsDown)

```

{  
player\_mc.gotoAndStop(2);

}  
if(leftKeyIsDown)  
{  
player\_mc.gotoAndStop(2);

}  
if(downKeyIsDown)  
{  
player\_mc.gotoAndStop(2)

}  
if(upKeyIsDown)  
{  
player\_mc.gotoAndStop (2);

}

```
if(spaceKeyIsDown)

```

{  
player\_mc.gotoAndStop(2);

}  
}

//defines player  
stage.addEventListener(KeyboardEvent.KEY\_UP, run);  
function run(event:KeyboardEvent):void  
{  
player\_mc.gotoAndStop(1);  
}  
stage.addEventListener(KeyboardEvent.KEY\_UP, checkkeysup);  
var speed=10;  
var moveright=false;  
function checkkeysdown(mykey:KeyboardEvent)  
{  
if (mykey.keyCode==Keyboard.RIGHT)  
{  
moveright=true;  
}  
}  
function checkkeysup(mykey:KeyboardEvent)  
{  
if (mykey.keyCode==Keyboard.RIGHT)  
{  
moveright=false;  
}  
}

// collects coins  
stage.addEventListener(Event.ENTER\_FRAME, collectCoins);  
function collectCoins(e:Event):void{  
if (player\_mc.hitTestObject(coin\_mc)){  
if (hitCoin==true)  
score++;  
coin\_mc.parent.removeChild(coin\_mc);  
}  
if (player\_mc.hitTestObject(coin\_2)){  
if (hitCoin==true)  
score++;  
coin\_2.parent.removeChild(coin\_2);  
}  
if (player\_mc.hitTestObject(coin\_3)){  
if (hitCoin==true)  
score++;  
coin\_3.parent.removeChild(coin\_3);  
}  
if (player\_mc.hitTestObject(coin\_4)){  
if (hitCoin==true)  
score++;  
coin\_4.parent.removeChild(coin\_4);  
}  
if (player\_mc.hitTestObject(coin\_5)){  
if (hitCoin==true)  
score++;  
coin\_5.parent.removeChild(coin\_5);  
}  
if (player\_mc.hitTestObject(coin\_6)){  
if (hitCoin==true)  
score++;  
coin\_6.parent.removeChild(coin\_6);  
}  
if (player\_mc.hitTestObject(coin\_7)){  
if (hitCoin==true)  
score++;  
coin\_7.parent.removeChild(coin\_7);  
}  
if (player\_mc.hitTestObject(coin\_8)){  
if (hitCoin==true)  
score++;  
coin\_8.parent.removeChild(coin\_8);  
}  
if (player\_mc.hitTestObject(coin\_9)){  
if (hitCoin==true)  
score++;  
coin\_9.parent.removeChild(coin\_9);  
}  
if (player\_mc.hitTestObject(coin\_bc)){  
if (hitCoin==true)  
score++;  
coin\_bc.parent.removeChild(coin\_bc);  
}  
if (player\_mc.hitTestObject(coin\_11)){  
if (hitCoin==true)  
score++;  
coin\_11.parent.removeChild(coin\_11);  
}  
if (player\_mc.hitTestObject(coin\_12)){  
if (hitCoin==true)  
score++;  
coin\_12.parent.removeChild(coin\_12);  
}  
if (player\_mc.hitTestObject(coin\_13)){  
if (hitCoin==true)  
score++;  
coin\_13.parent.removeChild(coin\_13);  
}  
if (player\_mc.hitTestObject(coin\_14)){  
if (hitCoin==true)  
score++;  
coin\_14.parent.removeChild(coin\_14);  
}  
if (player\_mc.hitTestObject(coin\_15)){  
if (hitCoin==true)  
score++;  
coin\_15.parent.removeChild(coin\_15);  
}  
if (player\_mc.hitTestObject(coin\_fe)){  
if (hitCoin==true)  
score++;  
coin\_fe.parent.removeChild(coin\_fe);  
}  
if (player\_mc.hitTestObject(coin\_17)){  
if (hitCoin==true)  
score++;  
coin\_17.parent.removeChild(coin\_17);  
}  
if (player\_mc.hitTestObject(coin\_18)){  
if (hitCoin==true)  
score++;  
coin\_18.parent.removeChild(coin\_18);  
}  
if (player\_mc.hitTestObject(coin\_19)){  
if (hitCoin==true)  
score++;  
coin\_19.parent.removeChild(coin\_19);  
}  
if (player\_mc.hitTestObject(coin\_20)){  
if (hitCoin==true)  
score++;  
coin\_20.parent.removeChild(coin\_20);  
}  
if (player\_mc.hitTestObject(coin\_21)){  
if (hitCoin==true)  
score++;  
coin\_21.parent.removeChild(coin\_21);  
}  
if (player\_mc.hitTestObject(coin\_22)){  
if (hitCoin==true)  
score++;  
coin\_22.parent.removeChild(coin\_22);  
}  
if (player\_mc.hitTestObject(coin\_23)){  
if (hitCoin==true)  
score++;  
coin\_23.parent.removeChild(coin\_23);  
}  
if (player\_mc.hitTestObject(coin\_24)){  
if (hitCoin==true)  
score++;  
coin\_24.parent.removeChild(coin\_24);  
}  
if (player\_mc.hitTestObject(coin\_cd)){  
if (hitCoin==true)  
score++;  
coin\_cd.parent.removeChild(coin\_cd);  
}  
if (player\_mc.hitTestObject(coin\_df)){  
if (hitCoin==true)  
score++;  
coin\_df.parent.removeChild(coin\_df);  
}  
if (player\_mc.hitTestObject(coin\_27)){  
if (hitCoin==true)  
score++;  
coin\_27.parent.removeChild(coin\_27);  
}  
if (player\_mc.hitTestObject(coin\_28)){  
if (hitCoin==true)  
score++;  
coin\_28.parent.removeChild(coin\_28);  
}  
hitCoin=true;  
coinCount\_txt.text=score.toString();  
}

//timer that end  
gameTimer.addEventListener(TimerEvent.TIMER, countDown);

function countDown(e:TimerEvent):void{  
timeleft–;  
timetxt.text=String(timeleft);  
if (timeleft\<0){  
gameTimer.stop();  
stage.removeEventListener(KeyboardEvent.KEY\_DOWN, PressAKey);  
stage.removeEventListener(KeyboardEvent.KEY\_UP, ReleaseAKey);  
stage.removeEventListener(KeyboardEvent.KEY\_DOWN, moveThePlayer);  
stage.removeEventListener(KeyboardEvent.KEY\_UP, run);  
stage.removeEventListener(KeyboardEvent.KEY\_UP, checkkeysup);  
stage.removeEventListener(Event.ENTER\_FRAME, collectCoins);  
gameTimer.removeEventListener(TimerEvent.TIMER, countDown);  
stage.removeEventListener(Event.ENTER\_FRAME, gameloop);  
gotoAndStop(1, “Scene 3”);  
}  
}  
gameTimer.start();

stage.addEventListener(Event.ENTER\_FRAME, gameloop);  
function gameloop(e:Event):void{  
obstacle\_mc.x-=20;  
if (obstacle\_mc.x\<-100){  
obstacle\_mc.x=650;  
hitObstacle=false;  
}  
if (player\_mc.hitTestObject(obstacle\_mc)) {  
if (hitObstacle==false){ // only subtract health if hitObstacle is false  
health–;  
}  
hitObstacle=true;  
health\_txt.text=health.toString();  
if (health\<=0){  
stage.removeEventListener(KeyboardEvent.KEY\_DOWN, PressAKey);  
stage.removeEventListener(KeyboardEvent.KEY\_UP, ReleaseAKey);  
stage.removeEventListener(KeyboardEvent.KEY\_DOWN, moveThePlayer);  
stage.removeEventListener(KeyboardEvent.KEY\_UP, run);  
stage.removeEventListener(KeyboardEvent.KEY\_UP, checkkeysup);  
stage.removeEventListener(Event.ENTER\_FRAME, collectCoins);  
gameTimer.removeEventListener(TimerEvent.TIMER, countDown);  
stage.removeEventListener(Event.ENTER\_FRAME, gameloop);  
gotoAndStop(1, “Scene 3”);  
}  
}  
}

The code for the game over screen:

stop();  
replay\_btn.addEventListener(MouseEvent.CLICK, clickFunction);  
function clickFunction(evt:MouseEvent):void {  
gotoAndPlay(1,“Scene 1”);  
}  
coinCount\_txt.text=score.toString();

Many thanks in advance.
