dcsimg
A Flash Developer Resource Site

Results 1 to 5 of 5
  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
    Senior Member Alloy Bacon's Avatar
    Join Date
    Apr 2011
    Posts
    920
    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]
    My 3D browser based flash demo, you can draw on the wall and it saves forever: https://spark.adobe.com/page/oXRKzNxeb9p1t/

  3. #3
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,771
    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
    Senior Member Alloy Bacon's Avatar
    Join Date
    Apr 2011
    Posts
    920
    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
    My 3D browser based flash demo, you can draw on the wall and it saves forever: https://spark.adobe.com/page/oXRKzNxeb9p1t/

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

×
We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.