I am trying to use a variable in my switch statement as it makes the player movement smooth. This is my smooth player movement code.
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressedDown);
stage.addEventListener(KeyboardEvent.KEY_UP, keyPressedUp);
stage.addEventListener(Event.ENTER_FRAME,movehero);
function keyPressedDown(event:KeyboardEvent){
if(event.keyCode==37){leftArrow = true;}
else if (event.keyCode == 39){rightArrow=true;}
else if(event.keyCode ==38){upArrow = true;}
else if(event.keyCode ==40){downArrow = true;}
}
function keyPressedUp(event:KeyboardEvent){
if(event.keyCode==37){leftArrow = false;}
else if (event.keyCode == 39){rightArrow=false;}
else if(event.keyCode ==38){upArrow = false;}
else if(event.keyCode ==40){downArrow = false;}
}
function movehero(event:Event){
if(leftArrow){hero.x -= 10;}
if(rightArrow){hero.x += 10;}
if(upArrow){hero.y -= 10;}
if(downArrow){hero.y += 10;}
}
I am trying to implement it into my existing code. i need the switch statements for collision.
This code works fine but player movement is very slugish.
Thanks in advance
I am trying to acheive something like this, but this does not work
var leftArrow:Boolean= false;
var rightArrow:Boolean= false;
var upArrow:Boolean= false;
var downArrow:Boolean=false;
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressedDown);
stage.addEventListener(KeyboardEvent.KEY_UP, keyPressedUp);
stage.addEventListener(Event.ENTER_FRAME,movehero);
function keyPressedDown(event:KeyboardEvent){
if(event.keyCode==37){leftArrow = true;}
else if (event.keyCode == 39){rightArrow=true;}
else if(event.keyCode ==38){upArrow = true;}
else if(event.keyCode ==40){downArrow = true;}
}
function keyPressedUp(event:KeyboardEvent){
if(event.keyCode==37){leftArrow = false;}
else if (event.keyCode == 39){rightArrow=false;}
else if(event.keyCode ==38){upArrow = false;}
else if(event.keyCode ==40){downArrow = false;}
}
function movehero(event:Event){
switch(event){
case leftArrow:
hero.x -=10;
break;
case rightArrow:
hero.x +=10;
break;
case upArrow:
hero.y -=10;
break;
case downArrow:
hero.y += 10;
break;
default :
break;
}
}