Skip to main content
New Participant
October 11, 2018
Answered

can anyone figure out why my code won't work?

  • October 11, 2018
  • 2 replies
  • 301 views

Ive been using a tutorial on youtube to make a 2d game of my own and so far its worked perfectly but I've come into a road block, the girl in the tutorial shows how to get your character to stop doing his walking animation when you stop moving mine though for some reason will not stop the character from doing a movement animation even though hers does, I've checked it over many times and no error pop ups are coming so I need some help, the video is called Coding User Interaction and Animating Movement of A Game Character Adobe Flash and the code I have so far is this:

import flash.events.Event;

import flash.events.KeyboardEvent;

stop();

stage.focus=stage;

//initialize the Boolean variables for each arrow key

var leftKeyDown: Boolean=false;

var upKeyDown: Boolean=false;

var rightKeyDown: Boolean=false;

var downKeyDown: Boolean=false;

var lastKey: Number=0;

stage.addEventListener(KeyboardEvent.KEY_DOWN, downKey);

stage.addEventListener(KeyboardEvent.KEY_UP, upKey);

stage.addEventListener(Event.ENTER_FRAME, moveNinja);

  function downKey(event: KeyboardEvent) {

  if (event.keyCode == 37) {

  leftKeyDown = true;

  rightKeyDown=false;

  upKeyDown=false;

  downKeyDown=false;

  } else if (event.keyCode == 38) {

  leftKeyDown = false;

  rightKeyDown=false;

  upKeyDown=true;

  downKeyDown=false;

  } else if (event.keyCode == 39) {

  leftKeyDown = false;

  rightKeyDown=true;

  upKeyDown=false;

  downKeyDown=false;

  } else if (event.keyCode == 40) {

  leftKeyDown = false;

  rightKeyDown=false;

  upKeyDown=false;

  downKeyDown=true;

  }

  }

  function upKey(event: KeyboardEvent) {

  if (event.keyCode == 37) {

  leftKeyDown = false;

  } else if (event.keyCode == 38) {

  upKeyDown = false;

  } else if (event.keyCode == 39) {

  rightKeyDown = false;

  } else if (event.keyCode == 40) {

  downKeyDown = false;

  }

  }

  //check for last key pressed

  stage.addEventListener(KeyboardEvent.KEY_DOWN,lastKeyPressed);

  function lastKeyPressed(event:KeyboardEvent) {

  if(downKeyDown==true) {lastKey=1;}

  if(upKeyDown==true) {lastKey=2;}

  if (leftKeyDown==true) {lastKey=3;}

  if(rightKeyDown==true) {lastKey=4;} }

  function moveNinja(event:Event) {

  if (upKeyDown==true) {

  Ninja.y-=3;

  Ninja.gotoAndStop("walkup");

  }

  if (downKeyDown==true) {

  Ninja.y+=3;

  Ninja.gotoAndStop("walkdown");

  }

  if (leftKeyDown==true) {

  Ninja.x-=3;

  Ninja.gotoAndStop("walkleft");

  }

  if (rightKeyDown==true) {

  Ninja.x+=3;

  Ninja.gotoAndStop("walkright");

  }

  }

  //stand still animations

  if(lastKey==0) {

  Ninja.gotoAndStop("stilldown");

  }

  if(lastKey==1 && downKeyDown==false) {Ninja.gotoAndStop("stilldown");

  }

  if(lastKey==2 && upKeyDown==false) {Ninja.gotoAndStop("stillup");

  }

  if(lastKey==3 && leftKeyDown==false) {Ninja.gotoAndStop("stillleft");

  }

  if(lastKey==4 && rightKeyDown==false) {Ninja.gotoAndStop("stillright");

  }

This topic has been closed for replies.
Correct answer JoãoCésar17023019

Hi.

You should move the last five if statements to the moveNinja function because the player state must be checked at every frame.

Regards,

JC

2 replies

JoãoCésar17023019
JoãoCésar17023019Correct answer
Community Expert
October 11, 2018

Hi.

You should move the last five if statements to the moveNinja function because the player state must be checked at every frame.

Regards,

JC

aightyAuthor
New Participant
October 12, 2018

thanks my dude

JoãoCésar17023019
Community Expert
October 15, 2018

You're welcome.

aightyAuthor
New Participant
October 11, 2018

also Ninja and the other names in it are correct I have checked them compared the names I'm trying to find with the code and they good