Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • EspaƱol
      • FranƧais
      • PortuguĆŖs
  • ę—„ęœ¬čŖžć‚³ćƒŸćƒ„ćƒ‹ćƒ†ć‚£
  • ķ•œźµ­ ģ»¤ė®¤ė‹ˆķ‹°
0

Event Listener not working within a class

Guest
Jan 30, 2011 Jan 30, 2011

Hi!

I have a problem, I have 2 files, one that is the main .fla that is called game.fla and another file called PacMan.as.

I have just started with as3 and would like to know why I can“t create an KeyboardEvent on the .as file.

I have created an movieclip that would be the player and linked it to the .as. I have checked and it works when i use the trace command within the .as file.


But when i add an function and an eventListener the trace command dosen“t work...

Here is what i have:

*.AS File(PacMan.as):

// PacMan Class
package gamecontent.classes
{
import flash.display.MovieClip;
import flash.ui.Keyboard;
import flash.events.*;

public class PacMan extends MovieClip
{
   
    public function PacMan()
    {
        var newPacMan:PacMan = new PacMan;
    newPacMan.addEventListener(KeyboardEvent.KEY_DOWN, testFunction);

trace("Class Loaded");
    }
        function testFunction(e:KeyboardEvent):void
    {
        trace("Key is pressed...");
    }
}


}

*.fla file (game.fla) I just "spawn" the player from the library:

var player:PacMan = new PacMan();


addChild(player);

// Position Objects...
player.x = 39;
player.y  = 559;

Any help is highly appriciated...

TOPICS
ActionScript
1.5K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Advocate , Jan 30, 2011 Jan 30, 2011

hi

try adding the listener to the stage instead:

stage.addEventListener(KeyboardEvent.KEY_DOWN, testFunction);

this will pick up all keyboard events - if you add a keyboard listener to a specific object, the event only fires if that object has focus

Translate
Advocate ,
Jan 30, 2011 Jan 30, 2011

hi

try adding the listener to the stage instead:

stage.addEventListener(KeyboardEvent.KEY_DOWN, testFunction);

this will pick up all keyboard events - if you add a keyboard listener to a specific object, the event only fires if that object has focus

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Jan 30, 2011 Jan 30, 2011

I have done that, i get an error:

TypeError: Error #1009: Cannot access a property or method of a null object reference.
    at gamecontent.classes::PacMan()
    at game_fla::MainTimeline/frame1()

My code:

// PacMan Class
package gamecontent.classes
{
import flash.display.MovieClip;
import flash.ui.Keyboard;
import flash.events.*;

public class PacMan extends MovieClip
{
   
    public function PacMan()
    {
    stage.addEventListener(KeyboardEvent.KEY_DOWN, testFunction);
    }
        function testFunction(e:KeyboardEvent):void
    {
        trace("Test");
    }
}


}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Mentor ,
Jan 30, 2011 Jan 30, 2011

Since you are in authoring mode Keyboard presses will affect the Flash User Interface, as can be witnessed when you switch between keys such as A, S, D, V, F, etc.  These are hotkeys in Flash and steal any interactivity from when you do Test Movie.  Try using "Debug Movie" instead and then press the keys on the keyboard.  Create conditional statements in your trace event such as trace(e.charCode) and trace(e.keyCode) so you can see which key is pressed.  You can combine them into a dynamic text field which reads myTextField.text="Key is pressed..."+e.charCode+"    "+"e.keyCode; for example.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Mentor ,
Jan 30, 2011 Jan 30, 2011

but still use Lee's post:  stage.addEventListener(....);

Message was edited by: markerline

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Jan 30, 2011 Jan 30, 2011
LATEST

hi

i assume you are getting an error because you are referencing stage before PacMan has been added to the stage (so the stage property is still null)

to get round it, dont add the keyboard listener until PacMan has been added to stage:

public function PacMan()
{
     addEventListener(Event.ADDED_TO_STAGE, added);

}

private function added(e:Event):void

{

     removeEventListener(Event.ADDED_TO_STAGE, added);

     stage.addEventListener(KeyboardEvent.KEY_DOWN, testFunction);

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines