A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: AS2 - player movement problem

  1. #1
    Junior Member
    Join Date
    Feb 2018
    Posts
    2

    AS2 - player movement problem

    Hello.
    I need help in improving the code to move the character in my game (AS2).
    The problem is that I would like move the player character only in four directions (left, right, up, down)
    For movement I'm using a script:

    onClipEvent(enterFrame){
    if(Key.isDown(Key.RIGHT)){
    _root.player._x += 5;
    }

    if(Key.isDown(Key.LEFT)){
    _root.player._x -= 5;
    }

    if(Key.isDown(Key.UP)){
    _root.player._y -= 5;
    }

    if(Key.isDown(Key.DOWN)){
    _root.player._y += 5;
    }

    }

    If I press two buttons (up and left or up and right or down and left or down and right) at the same time it moves diagonally.
    I would like to block the possibility of pressing two buttons. Let nothing happen when two buttons are pressed.
    Thx for help

  2. #2
    Client Software Programmer AS3.0's Avatar
    Join Date
    Apr 2011
    Posts
    1,404
    I would put else before the if statement so it picks closest one to the top pressed every frame per second
    if(Key.isDown(Key.RIGHT)){
    _root.player._x += 5;
    }else if(Key.isDown(Key.LEFT)){
    _root.player._x -= 5;
    }else if(Key.isDown(Key.UP)){
    _root.player._y -= 5;
    }else if(Key.isDown(Key.DOWN)){
    _root.player._y += 5;
    }
    }
    [/PHP]

  3. #3
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,780
    Hi
    Use a switch

    Look up as2 switch

  4. #4
    Junior Member
    Join Date
    Feb 2018
    Posts
    2
    Quote Originally Posted by Alloy Bacon View Post
    I would put else before the if statement so it picks closest one to the top pressed every frame per second
    if(Key.isDown(Key.RIGHT)){
    _root.player._x += 5;
    }else if(Key.isDown(Key.LEFT)){
    _root.player._x -= 5;
    }else if(Key.isDown(Key.UP)){
    _root.player._y -= 5;
    }else if(Key.isDown(Key.DOWN)){
    _root.player._y += 5;
    }
    }
    [/PHP]
    Thank you very much. It helped. I'm owed you a large beer :-)

  5. #5
    Client Software Programmer AS3.0's Avatar
    Join Date
    Apr 2011
    Posts
    1,404
    I hope it worked if anything send it to fruitbeard he taught me the scripts: https://www.paypal.com/donate/?token...GB&locale.x=GB

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