Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

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

Community Beginner ,
Oct 11, 2018 Oct 11, 2018

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");

  }

267
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Oct 11, 2018 Oct 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

Translate
Community Beginner ,
Oct 11, 2018 Oct 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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 11, 2018 Oct 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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Oct 12, 2018 Oct 12, 2018

thanks my dude

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 15, 2018 Oct 15, 2018
LATEST

You're welcome.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines