Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I hope you can help me with the following problem:

I made a character in Illustrator. The player sees the character from above. When you press the left arrow key character.gotoAndStop('left'); the character moves to the left. (Same goes for the right direction with label 'right' When you release the left or right arrow key (upKey) the character goes back to the label 'up'.

I'm trying to accomplish the following: When the character is facing down ('down'). the left key should go to the ("facingdownleft") label and on release back to the label ('down'). (Same for the right direction)

enter image description here

stage.addEventListener(KeyboardEvent.KEY_DOWN, downKey);
function downKey(event:KeyboardEvent)
{
 if(event.keyCode==39){
 isRight=true;
 character.gotoAndStop('right');}
 if(event.keyCode==37){
 isLeft=true;
 character.gotoAndStop('left');}
 if(event.keyCode==38){
 isUp=true;
 character.gotoAndStop('up');
 }
 if(event.keyCode==40){
 isDown=true;
 facingdown=true;
 character.gotoAndPlay('down');}
}

if( facingdown == true && isLeft == true)
{ 
 character.gotoAndPlay('downleft');
}


stage.addEventListener(KeyboardEvent.KEY_UP, upKey);
function upKey(event:KeyboardEvent){
 if(event.keyCode==39){
 isRight=false;
 character.gotoAndPlay('up')}
 if(event.keyCode==37){
 isLeft=false
 character.gotoAndPlay('up')}
 if(event.keyCode==38){
 isUp=false;}
 if(event.keyCode==40){
 isDown=false}
 }

I made a boolean for facingdown and an if statement for downleft:

  if( facingdown == true && isLeft == true)
    { 
     character.gotoAndPlay('downleft');
    }

But before I make numerous amounts of if statements again, I would like to know if I'm on the right track and if you can help me accomplish this? Thank you so much in advance and feel free to ask questions.

enter image description here

share|improve this question
    
Humm I need some informations in order to help you. Could you give us exactly what are the available frame labels (because I can see, "up", "down", "left", "right", "downleft" and that's all, but I guess it should exists "downright" too, right?). Further more "(up)left", "(up)right", "downleft", "downright" are sequences whereas "up" and "down" are one frame position I am right ? –  Valkea Apr 27 '12 at 12:03
    
I can't understand the problem, that code you made doesn't work? –  Dyego Apr 27 '12 at 13:19
    
@dyegodejesussilva Thank you for your question, the if statement doesn't give any errors, but doesn't work properly either. if facedown == true and you press left it still goes to the (up)left label. –  Opoe Apr 27 '12 at 15:30
    
@Valkea Thank you for your question. The reason i left out downright is because I thought it would be best to first test it with one direction and facedown. So downright isn't in the code yet. My bad! I added a image of the labels. –  Opoe Apr 27 '12 at 15:32

1 Answer 1

up vote 2 down vote accepted

You could try something like the following code:

stage.addEventListener(KeyboardEvent.KEY_DOWN, downKey);
function downKey(event:KeyboardEvent)
{
    if( event.keyCode == Keyboard.UP )
    {
        isUp=true;
        character.gotoAndStop('up'); // up (animated?)
    }
    else if( event.keyCode == Keyboard.DOWN )
    {
        isUp=false;
        character.gotoAndStop('down'); // down (animated?)
    }

    else if( event.keyCode == Keyboard.LEFT )
    {
        if( isUp )
            character.gotoAndStop('left'); // (up)left
        else
            character.gotoAndStop('downleft'); // downleft      
    }
    else if( event.keyCode == Keyboard.RIGHT )
    {
        if( isUp )
            character.gotoAndStop('right'); // (up)right
        else
            character.gotoAndStop('downright'); // downright
    }
}

stage.addEventListener(KeyboardEvent.KEY_UP, upKey);
function upKey(event:KeyboardEvent)
{
    if( event.keyCode == Keyboard.RIGHT || event.keyCode == Keyboard.LEFT )
    {
        if( isUp )
            character.gotoAndStop('up'); // up (still?)
        else
            character.gotoAndStop('down'); // down (still?)
    }
}

I tried to do something as close as possible to your original code.

But it's yet a very simple solution that doesn't handle multi-key pressed at the same time etc... And obviously you should consider different keyframes for immobile and animated "up" and "down". I supposed that your bools were globals so I reduced it to one because it doesn't seems useful in this portion of code, but indeed you can re-introduce the other bools if needed elsewhere.

share|improve this answer
    
thank you so much! It's exactly what I asked for. Thanks again :) –  Opoe Apr 27 '12 at 18:07
    
just a small question, shall I add a seperate function for movement or shall I put it in your code like this: character.x += speed; –  Opoe Apr 27 '12 at 18:11
1  
Well it depends on the movement type you need. If you add this code directly into this function, you will get ONE move each time you press the direction key. But if you want to repeatedly move until you release the key, you will need to manage it outside this function (though you can start the move from the KEY_DOWN event function and stop it from the KEY_UP event function). –  Valkea Apr 27 '12 at 18:28

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.