A Flash Developer Resource Site

Results 1 to 6 of 6

Thread: Arrow Keys Code by Jovan Game Studios

  1. #1
    TheFlashMaster
    Join Date
    Aug 2009
    Posts
    28

    Thumbs up Arrow Keys Code by Jovan Game Studios

    Hello.
    Today I will show and explain the arrow keys code. This code can help you if you want to make game with moving object with the arrow keys.


    First you go to Insert>Symbol. Now you draw your object. Then you go to Scence 1.

    On the Action Script box of your object you write this code:

    onClipEvent (enterFrame) {
    if (Key.isDown(Key.UP)) {
    this._y -= 5;
    }
    if (Key.isDown(Key.DOWN)) {
    this._y += 5;
    }
    if (Key.isDown(Key.RIGHT)) {
    this._x += 5;
    }
    if (Key.isDown(Key.LEFT)) {
    this._x -= 5;
    }
    }

    So let's explain this code.
    With the "if" function you make condition when some arrow key will be pressed. When you press arrow key the object is moving with adding or subtracting the scaleY (up and down) or scaleX (left and right). That the code.

    Thank you!
    Jovan Game Studios

  2. #2
    FK'n_dog a_modified_dog's Avatar
    Join Date
    Apr 2003
    Location
    "aaarf"
    Posts
    9,176
    hmmm.....soooo Flash 5

  3. #3
    5+5=55 Schfifty Five's Avatar
    Join Date
    Jun 2006
    Posts
    698
    Quote Originally Posted by The FlashMaster View Post
    When you press arrow key the object is moving with adding or subtracting the scaleY (up and down) or scaleX (left and right).
    You're adding or subtracting 5 to the x and y coordiantes (_x and _y properties). scaleX and scaleY have nothing to do with it (just a typo perhaps?)

  4. #4
    Dignitary rynoe's Avatar
    Join Date
    Jan 2003
    Location
    Earth
    Posts
    760
    Quote Originally Posted by a_modified_dog View Post
    hmmm.....soooo Flash 5
    lol

    Stop teaching newbies AS1 Flashmaster.

    [SIGPIC][/SIGPIC]

  5. #5
    Dignitary rynoe's Avatar
    Join Date
    Jan 2003
    Location
    Earth
    Posts
    760
    Here it is in AS3

    PHP Code:
    import flash.events.KeyboardEvent;
    stage.addEventListener(KeyboardEvent.KEY_DOWNmoveIt)
    function 
    moveIt(evt:KeyboardEvent):void{
        if (
    evt.keyCode == 37){
            
    myMC.-=5;
        }
        if (
    evt.keyCode == 39){
            
    myMC.+=5;
        }
        if (
    evt.keyCode == 38){
            
    myMC.-=5;
        }
        if (
    evt.keyCode == 40){
            
    myMC.+=5;
        }


    The differences here are

    1. This function only fires when a key is down, saving processing speed.
    2. Any key on the keyboard can be added in this function.
    Last edited by rynoe; 08-05-2009 at 11:09 AM.
    [SIGPIC][/SIGPIC]

  6. #6
    Senior Member Robb@exo's Avatar
    Join Date
    Jun 2003
    Location
    Canada
    Posts
    1,540
    Quote Originally Posted by rynoe View Post
    lol

    Stop teaching newbies AS1 Flashmaster.

    Definitely agree with rynoe, Flashmaster's energy should be directed at teaching newbies how to use Google, or for that matter the seach feature of this forum.
    Wile E. Coyote - "Clear as mud?"

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