How to access the stage from an AS3 class in Adobe Flash
Asked Answered
M

4

5

The problem I've encountered is that I am using a keyboardEventListener to make a movieclip run around. As I'm a college student, I'm creating this for an assignment but we are forced to use as3 classes.

When I run the code in the maintimeline, there is no problem. But when I try to access it from another class (with an 'Export for ActionScript' on the movieclip in question) I get an error he can't address the stage.

this.stage.addEventListener(KeyboardEvent.KEY_DOWN, dostuff);

Mucous answered 21/4, 2010 at 20:36 Comment(0)
M
9

A class in AS3 is not on the stage until you actually place it there. As a result, "this.stage" will be null at compile time. You can get around this problem by using the ADDED_TO_STAGE event to delay binding your listeners until the time is right.

public function MyClass(){
    this.addEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);
}

private function addedToStageHandler(e:Event):void{
    this.stage.addEventListener(KeyboardEvent.KEY_DOWN, dostuff);
}
Merrythought answered 21/4, 2010 at 21:0 Comment(2)
Alrgiht! I've solved the stage problem. I still get a few errors though. I'm getting the error that he can't find the keyboard and every error is located on the following kind of code "if (evt.keyCode == Keyboard.RIGHT){ //stuff }" the error is the following: "1120: Access of undefined property Keyboard." already thanks for solving the stage thing! Searched Google down for it but couldn't find anything relevant!Mucous
@Mucous if you have a different question, ask a new question on stackoverflow instead of asking it in a comment.Sparid
W
0

"1120: Access of undefined property Keyboard. There's your answer. You haven't defined the keyboard properties. Which means you haven't imported to the package.

should look something like this:

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

Advice: have a deeper look into importing. try using flash builder, its much better for beginners and auto import classes so u dont need to memorize everything.

Whizbang answered 25/4, 2010 at 2:32 Comment(0)
C
0

this answer has helped me a million times, but i dont yet have enough points to pop it up one, or i would.

This happens generically when you try to access anything on the stage before it is added to stage. I was, for a while, using an init() in the constructor of all my projects main classes, but because of this issue, I no longer do. Now, instead I have replaced it with this (where Main is the class constructor/name):

public function Main():void {
    this.addEventListener(Event.ADDED_TO_STAGE, init);
    super();    
}
...
private function init(e:Event):void {
...

I hope this helps anyone else who read any of the books I did on flash, that offer the init() idea.

And..thanks Greg W.

Corinthian answered 27/4, 2011 at 18:57 Comment(0)
R
0

When you create class you have to refer the stage from inside of your class coz its not accessible globally you have to pass it into the class , and here is a example for use stage event listener inside a class.

package  {
    import flash.events.KeyboardEvent;

    public class Eventhndl{

        private var obj:Object; //create local variable to refarance stage


        public function Eventhndl(objStage:Object):void{
            obj = objStage; //make local refarance for stage inside the class

            obj.addEventListener(KeyboardEvent.KEY_DOWN,runit); //add the event listener
        }

        private function runit(Event:KeyboardEvent):void{
            trace("keyDownHandler: " + Event.keyCode);
            trace("ctrlKey: " + Event.ctrlKey);
            trace("keyLocation: " + Event.keyLocation);
            trace("shiftKey: " + Event.shiftKey);
            trace("altKey: " + Event.altKey);
        }
    }

}

save the file as Eventhndl.as and now you can just add the instance of this class and pass whatever the object that you need to listen its event, here is how to do that.

import Eventhndl;

var EH:Eventhndl = new Eventhndl(stage); 
Repeater answered 19/11, 2016 at 10:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.