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.