A Flash Developer Resource Site

Results 1 to 14 of 14

Thread: Move Link in 4 Directions

  1. #1
    Senior Member
    Join Date
    Jan 2008
    Posts
    107

    Wink Move Link in 4 Directions

    Greetings all. For educational purposes, I'm trying to clone The Legend of Zelda (NES) Link's movement.

    This what I have:

    PHP Code:
    stop();

    var 
    leftKeyDown:Boolean false;
    var 
    upKeyDown:Boolean false;
    var 
    rightKeyDown:Boolean false;
    var 
    downKeyDownBoolean false;

    var 
    linkSpeed:Number7;

    // Link Movement

    Link.addEventListener(Event.ENTER_FRAMEmoveLink);

    function 
    moveLink(event:Event):void
    {
        if(
    leftKeyDown)
        {
            
    Link.-= linkSpeed;
        }
        
        if(
    rightKeyDown)
        {
            
    Link.+= linkSpeed;
        }
        
        if(
    upKeyDown)
        {
            
    Link.-= linkSpeed;
        }
        
        if(
    downKeyDown)
        {
            
    Link.+= linkSpeed;
        }
    }

    // Keys are pressed

    stage.addEventListener(KeyboardEvent.KEY_DOWNcheckKeysDown);

    function 
    checkKeysDown(event:KeyboardEvent):void
    {
        if(
    event.keyCode == 37)
        {
            
    leftKeyDown true;
        }
        
        if(
    event.keyCode == 38)
        {
            
    upKeyDown true;
        }
        
        if(
    event.keyCode == 39)
        {
            
    rightKeyDown true;
        }
        
        if(
    event.keyCode == 40)
        {
            
    downKeyDown true;
        }
    }

    // Keys are loose

    stage.addEventListener(KeyboardEvent.KEY_UPcheckKeysUp);

    function 
    checkKeysUp(event:KeyboardEvent):void
    {
        if(
    event.keyCode == 37)
        {
            
    leftKeyDown false;
        }
        
        if(
    event.keyCode == 38)
        {
            
    upKeyDown false;
        }
        
        if(
    event.keyCode == 39)
        {
            
    rightKeyDown false;
        }
        
        if(
    event.keyCode == 40)
        {
            
    downKeyDown false;
        }

    Link can move in eight directions, what do I need to remove his diagonal movements?

    Thanks.
    Last edited by Hurdarr; 06-04-2009 at 09:52 AM.

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Respond only to the last pressed direction key. So in checkKeysDown, set all your flags to false before your conditionals. By the way, you should be able to use a switch statement to clear up some of those ugly if statements.

  3. #3
    Senior Member
    Join Date
    Jan 2008
    Posts
    107
    Yap, that did the trick. Thanks.
    Last edited by Hurdarr; 06-04-2009 at 11:47 AM.

  4. #4
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Code:
    function checkKeysDown(event:KeyboardEvent):void
    {
      leftKeyDown = false;
      rightKeyDown = false;
      upKeyDown = false;
      downKeyDown = false;
      switch (event.keyCode) {
        case 37:
            leftKeyDown = true;
            break;
        
        case 38:
            upKeyDown = true;
            break;
    
        case 39:
            rightKeyDown = true;
            break;
        
        case 40:
            downKeyDown = true;
            break;
    }
    Be sure to put the "break"s in there, otherwise the execution will "fall-through" from one case to the next.

  5. #5
    Senior Member
    Join Date
    Jan 2008
    Posts
    107
    Hmm..I didn't do that.

    PHP Code:
    function checkKeysDown(event:KeyboardEvent):void
    {
        
    // I added the following lines
        
    leftKeyDown false;
        
    upKeyDown false;
        
    rightKeyDown false;
        
    downKeyDown false;
        
        if(
    event.keyCode == 37)
        {
            
    leftKeyDown true;
        }
        
        if(
    event.keyCode == 38)
        {
            
    upKeyDown true;
        }
        
        if(
    event.keyCode == 39)
        {
            
    rightKeyDown true;
        }
        
        if(
    event.keyCode == 40)
        {
            
    downKeyDown true;
        }

    Everything seems working fine. But thanks anyway. I'll learn more about switches later.

    Also, how can I animate the sprite? I already have the Link animated sprites.

    I created one movieclip, and inside of said movieclip, I created four frames. Each one has a movement animation (up,left,down and right). The frames are properly labeled.

    However, when I add "Link.gotoAndPlay("left");" to the code and publish the file, it just shows the first frame of the animation when I press the left key.

    Do I need to place all the individual non-animated sprites in the movieclip timeline?

    Thanks.

  6. #6
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Try gotoAndStop rather than gotoAndPlay.

  7. #7
    Senior Member
    Join Date
    Jan 2008
    Posts
    107
    Tried but didn't work.

    It might be easier to just upload the .fla.

    Thanks for the help.
    Attached Files Attached Files

  8. #8
    Senior Member Alluvian's Avatar
    Join Date
    Jun 2006
    Posts
    967
    I think your problem is that you are checking your movement logic every frame. So EVERY FRAME you are telling link to go to the first frame of his movement animation.

    You need to add 1 boolean variable for each direction link can animate in. Then you do something like the following when your moveright button is pressed:
    if (link.movingRight == false) {
    link.gotoAndStop("moveRight");
    link.movingRight = true;
    }

    then you need also set that variable to false when you release the button (as well as going to link's stationary frame/animation.

    :edit:
    actually, you can probably get by with just one variable link.moving depending on how you want to handle concurrent button presses. Pressing right, holding it down and pressing left.
    Last edited by Alluvian; 06-04-2009 at 04:02 PM.

  9. #9
    Senior Member
    Join Date
    Jan 2008
    Posts
    107
    Thanks for the help. That's a little bit too advanced to me, for now.

  10. #10
    Senior Member
    Join Date
    Jan 2008
    Posts
    107
    Hmm...but still didn't solve the animation thingie.

    How do you animate a sprite, that has 2 frames per animation? I created 4 movieclips, each doing one of Link's running movement (up, down, left right).

    Then I created an empty movieclip with 4 frames. Each frame has one movieclip with one movement direction. They are all labeled.

    But I don't know where is the error lies.

    I posted the .fla a few posts above.

    Thanks.

  11. #11
    Senior Member
    Join Date
    Jan 2008
    Posts
    107
    Still haven't solved the problem.

    Help.

  12. #12
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    I may be misunderstanding your structure, but I think you have two levels of movieclips. The first goes to a frame which corresponds to a direction. The second is a clip on each directional frame which has an animation. You need to use gotoAndStop on the first level and play on the second level. You'll also want to loop the second level clips.

  13. #13
    Senior Member
    Join Date
    Jan 2008
    Posts
    107
    Hmm...and how exactly can I do that? I add the code in the movieclip?

  14. #14
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    I think you can add the play() command for the inner clip to the frame code of the outer clip.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center