dcsimg
A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: Accessing the stage from a class

  1. #1
    Developer
    Join Date
    Apr 2007
    Location
    UK
    Posts
    324

    Accessing the stage from a class

    Dudes!

    I want to be able to dynamically add and remove an event listener for the keyboard to the stage from a class but Im not sure how to do it. I've used code like this :

    Code:
    package
    {
    	import flash.events.*
    	import flash.display.*
    	public class Main {
    		public function test() : void
    		{
    			stage.addEventListener (KeyboardEvent.KEY_DOWN, keyPressed);
    			function keyPressed (event : KeyboardEvent) : void {
    				trace("hello?")
    			}
    		}
    	}
    }
    But when I come to compile it says stage is not defined. Does anyone know how I can access the stage from a class function or cant it be done?

  2. #2
    Actionscript 3, postproduction georgecalgary's Avatar
    Join Date
    Jun 2005
    Location
    Toronto
    Posts
    229
    You can add a reference of a display object in display list to your class, usually come with the constructor function.

  3. #3
    half as fun, double the price senocular's Avatar
    Join Date
    Feb 2002
    Location
    San Francisco, CA (USA)
    Posts
    4,361
    the stage property is only accessible to display objects. This property is also null until the object has been added to an active (visible) display list. Note: the only display object that has access to the stage in its constructor is the document class instance. For more information, see:
    http://www.kirupa.com/forum/showthre...13#post1952513
    http://www.kirupa.com/forum/showthre...94#post1948094

  4. #4
    Developer
    Join Date
    Apr 2007
    Location
    UK
    Posts
    324
    Hi

    georgecalgary's answer was enough to help me. Taking my posted code as an example, I simply changed this line

    public function test() : void

    to

    public function test(stageisplayObject) : void

    and callled it like this :

    var tester:Main = new Main()
    tester.test(stage)

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