A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: Controlling an object with the arrows

  1. #1
    Junior Member
    Join Date
    May 2012
    Posts
    17

    Controlling an object with the arrows

    Hello,

    I'm trying to use a tutorial to teach myself how to make a basic flash game:
    http://www.webzo.org/tutorials/flash...d-movement.php

    Unfortunately this tutorial uses AS2 and I would like to teach myself AS3, since it's more recent. I try to do the spaceship movement code but when the program runs it tells me: "Access of undefined property Key."

    Here is the code:
    this.onEnterFrame = function() {
    if (Key.isDown(Key.RIGHT)) {
    Ship._x += 10;
    } else if (Key.isDown(Key.LEFT)) {
    Ship._x -= 10;
    } else if (Key.isDown(Key.UP)) {
    Ship._y -= 10;
    } else if (Key.isDown(Key.DOWN)) {
    Ship._y += 10; }
    }

    I understand it is a coding difference between AS2 and AS3. What do I need to do to convert it to AS3?

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Don't use that tutorial if you want to use AS3, it'll just confuse you.

    In AS3, event listeners are added with addEventListener rather than setting an onEventName property. Event listeners are passed an Event object as a parameter.

    All the _something properties lose the _. So _x becomes x and _y becomes y.

    There is no Key class in AS3, but the constants are in the Keyboard class. There is no direct equivalent of Key.isDown, but there is a package out there that Senocular made which implements an AS2 like interface. But I'd just use KeyboardEvent.KEY_DOWN and KeyboardEvent.KEY_UP listeners.

    Variables and instancenames should start with lowercase letters. Use "ship" rather than "Ship". Only classes should start with uppercase letters.

  3. #3
    Dignitary rynoe's Avatar
    Join Date
    Jan 2003
    Location
    Earth
    Posts
    760
    Code:
    stage.addEventListener(KeyboardEvent.KEY_DOWN, keyHandler);
    
    function keyHandler(evt:KeyboardEvent){
        if(evt.keyCode==37){
              trace("left");
        }
        if(evt.keyCode==38){
              trace("up");
        }
        if(evt.keyCode==39){
              trace("right");
        }
        if(evt.keyCode==40){
              trace("down");
        }
    }
    [SIGPIC][/SIGPIC]

  4. #4
    Junior Member
    Join Date
    May 2012
    Posts
    17
    Thanks, and you are right, I see that AS2 and AS3 are quite different so I will use other tutorials.

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