Skip to main content
Inspiring
October 5, 2007
Answered

Can't get AS3 KeyboardEvent to work

  • October 5, 2007
  • 4 replies
  • 852 views
When I use the code below to capture a KeyBoard event I get the following error:

TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Racer$iinit()
at MyMain$iinit()

Can anyone tell me why?

-----Code------

package {
import flash.ui.Keyboard;
import flash.display.*;
import flash.events.*;
import RaceCar;



public class Racer extends Sprite {
//
public function Racer():void {

stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownListener);

}
private function keyDownListener(e:KeyboardEvent):void {
trace("keyDownHandler");

}
}

}
This topic has been closed for replies.
Correct answer caseymanx
yes, if myCar is on-stage, it has access to the stage:


THANK YOU! That's what I was looking for. It works now. I really appreciate your help.

Casey

4 replies

Inspiring
October 5, 2007
nothing can reference the stage class unless it is first part of the display list. when i said create an empty clip i meant

var kh:Sprite = new Sprite();
addChild(kh);

It is also necessary to add your Racer object in the main timeline to the display list as well.

var myRCR:Racer = new Racer();
addChild(myRCR);

This is the only way that you will have access to the stage property.
kglad
Community Expert
Community Expert
October 5, 2007
ok, now does RaceCar have reference to the stage? eg, is it your document class? is myCar added to the stage when addChild(myCar) executes?
caseymanxAuthor
Inspiring
October 5, 2007
RaceCar is a Symbol in the library that's defined as a sprite. As it stands right now I've been letting Flash create an empty RaceCar class. In other words, I haven't written a RaceCar class. The RaceCar symbol shows up on the stage so I guess that means its been added to the display list, right? I mean, I added it to the display list and its showing up.

The class Racer is instantiated by the document class named MyMain. Racer is also added to the display list.

I'm not committed to using the stage class. Basically, I've tried a lot of things and can't get any of them to work.
kglad
Community Expert
Community Expert
October 5, 2007
yes, if myCar is on-stage, it has access to the stage:

Inspiring
October 5, 2007
Create an empty clip to add the event listener to.

stage (lowercase) is a property of the DisplayObjectContainer classes (MovieClip, Sprite, etc) and unless referenced from a valid object of their type, will usually return a null object reference.
kglad
Community Expert
Community Expert
October 5, 2007
i don't think that will help, sym, unless that object has access to the stage.

cm, is a Racer object created on the main timeline? if so, pass a timeline reference to your Racer class.
kglad
Community Expert
Community Expert
October 5, 2007
how does Racer obtain a reference to stage? eg, is Racer your document class?
caseymanxAuthor
Inspiring
October 5, 2007
No Racer is not the document class. I guess I thought that by importing flash.display.* I could refer to the stage. Can you tell me how I should implement a KeyboardEvent in the Racer class?