A Flash Developer Resource Site

Results 1 to 8 of 8

Thread: AS3 sending input to a method

  1. #1
    Junior Member
    Join Date
    Feb 2010
    Posts
    6

    AS3 sending input to a method

    hi I am new with ActionScript 3, trying to apply a few things I have done in C++.
    Does anyone know where I can find examples of event handling inside of a class method?
    Specifically how to call that method and send the event parameter to the method..

    heres my package / class (BTW I am just trying to make a circle move around when you press the arrow keys)

    package {

    import flash.display.*;
    import flash.events.*;

    public class Hero extends MovieClip {

    var xV = 0; // x Velocity
    var yV = 0; // y Velocity

    public function Hero(){}

    public function MoveIt()
    {
    this.x += xV;
    this.y += yV;

    }

    public function handleKeyDown(event:KeyboardEvent)
    {
    switch(event.keyCode)
    {
    case 38: --yV; break;
    case 40: ++yV; break;
    case 37: --xV; break;
    case 39: ++xV; break;
    default: break;
    }
    }


    public function handleKeyUp(event:KeyboardEvent)
    {
    switch(event.keyCode)
    {
    case 38: ++yV; break;
    case 40: --yV; break;
    case 37: ++xV; break;
    case 39: --xV; break;
    default: break;
    }
    }


    }

    }




    and here is where I am stuck / getting errors.. not knowing how to use this stuff inside of the timeline:

    var newHero:Hero = new Hero();
    this.addChild(newHero);
    newHero.x = 300;
    newHero.y = 200;

    stage.addEventListener(KeyboardEvent.KEY_DOWN, newHero.handleKeyDown());





    I can't figure out how to get the event passed into HandleKeyDown()
    Last edited by As3272; 02-08-2010 at 03:06 PM.

  2. #2
    Junior Member
    Join Date
    Feb 2010
    Posts
    6
    how do I edit the post? I made the code a really bad color. -got the color changed..
    Last edited by As3272; 02-08-2010 at 03:07 PM.

  3. #3
    :
    Join Date
    Dec 2002
    Posts
    3,518
    Code:
    var newHero:Hero = new Hero();
    this.addChild(newHero);
    newHero.x=300;
    newHero.y=200;
    
    stage.addEventListener(KeyboardEvent.KEY_DOWN, MovieClip(newHero).handleKeyDown);

  4. #4
    Junior Member
    Join Date
    Feb 2010
    Posts
    6
    Thank you dawsonk

  5. #5
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    You don't need to cast to MovieClip to get handleKeyDown. Since newHero is of type Hero which declares handleKeyDown as a public function, the compiler will know that that function is there without casting.

    Code:
    var newHero:Hero = new Hero();
    this.addChild(newHero);
    newHero.x=300;
    newHero.y=200;
    
    stage.addEventListener(KeyboardEvent.KEY_DOWN, newHero.handleKeyDown);
    The problem with the original code was that you were attempting to invoke handleKeyDown rather than pass a reference to the function.

  6. #6
    Junior Member
    Join Date
    Feb 2010
    Posts
    6
    Quote Originally Posted by 5TonsOfFlax View Post
    You don't need to cast to MovieClip to get handleKeyDown. Since newHero is of type Hero which declares handleKeyDown as a public function, the compiler will know that that function is there without casting.

    Code:
    var newHero:Hero = new Hero();
    this.addChild(newHero);
    newHero.x=300;
    newHero.y=200;
    
    stage.addEventListener(KeyboardEvent.KEY_DOWN, newHero.handleKeyDown);
    The problem with the original code was that you were attempting to invoke handleKeyDown rather than pass a reference to the function.

    right after I made that post I actually tried this way and had no success??

  7. #7
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    I don't know why that wouldn't work. What error message(s) did you get?

  8. #8
    Junior Member
    Join Date
    Feb 2010
    Posts
    6
    Can't remember, I will post it later. I have a ton of windows open at the moment.

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