Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 02-13-2008, 04:09 PM   #1
Rocketboy
Senior Member
 
Join Date: Apr 2006
Location: Brooklyn
Posts: 109
Unhappy Stage Scale Strangness Concerns

Hello,
I am trying to set up a simple stage scale listener to center objects on the stage. Though I seem to be doing what all the examples say to do, I am having this error come up when I run this class:

TypeError: Error #1009: Cannot access a property or method of a null object reference.
at classes.Interface::Resizer()
at classes.Interface::InterfaceManager()
at classes::CourseManager/init()
at classes::CourseManager()

Seems like the "stage" object is not being recognized correctly. What I don't get is why are all the code samples I look at use "stage" to refer to the Stage class rather than the capitalized "Stage" as the class is actually called? And why is "stage" not being recognized? THANKS!!!

Code:
package classes.Interface { 
	/**
	 * @author jkalish
	 */
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.EventDispatcher;
	import flash.events.Event;
	public class Resizer extends Sprite {
		//vars
		private var interface_mc:MovieClip;
		//constructor
		public function Resizer(interfaceRef:MovieClip){
			trace("***Resizer CONSTRUCT");
			//setup stage scale properties
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
			//create stage size listening object	
			stage.addEventListener(Event.RESIZE, stageScaled);
			stageScaled();
		}
                privatefunction stageScaled(e:Event){
                   //do things
}
}}
__________________
-jack
www.jackkalish.com
Rocketboy is offline   Reply With Quote
Old 02-13-2008, 05:27 PM   #2
dr_zeus
Feathers
 
dr_zeus's Avatar
 
Join Date: Mar 2005
Location: Silicon Valley
Posts: 1,999
Default

Unless you're working with a document class, the stage is never accessible in the constructor. Listen for the Event.ADDED_TO_STAGE event.
__________________
Josh Tynjala | Feathers
dr_zeus is offline   Reply With Quote
Old 02-13-2008, 06:15 PM   #3
panel
AS3
 
panel's Avatar
 
Join Date: Mar 2007
Location: Warsaw
Posts: 1,991
Send a message via Skype™ to panel
Default

Access stage
Quote:
Originally Posted by Senocular;
1. natively, only display objects have inherent access to stage
2. display objects only have a valid reference to stage if they're within the stage's display list (on the screen, after added to it with addChild)
3. The only display objects which immediately have access to the stage before the use of addChild (in their constructor) are the document/application class instance (root) and any instance from the library placed on the timeline (not created with AS).
4. Non-display object classes have to be passed or assigned a reference to the stage from another object which already has access to it - they cannot access it otherwise.
panel is offline   Reply With Quote
Old 02-13-2008, 06:43 PM   #4
Rocketboy
Senior Member
 
Join Date: Apr 2006
Location: Brooklyn
Posts: 109
Default

dr_zeus,

Sorry, I don't quite understand. I tried moving the code out of the constructor into a new init() function, but I'm still having the same problem. When you say "Listen for the Event.ADDED_TO_STAGE event" what should I be listening for to be added to the stage? Thanks.

Here is my code:

Code:
import flash.display.StageScaleMode;
	import flash.events.EventDispatcher;
	import flash.events.Event;
	public class Resizer extends Sprite {
		//vars
		private var interface_mc:MovieClip;
		//constructor
		public function Resizer(interfaceRef:MovieClip) {
			init();
		}
		private function init() {
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
			stage.addEventListener(Event.RESIZE, stageScaled);
			stageScaled();
			
		}
		protected function stageScaled(e:Event){
			//do things
		}
	}
}
__________________
-jack
www.jackkalish.com
Rocketboy is offline   Reply With Quote
Old 02-13-2008, 07:31 PM   #5
panel
AS3
 
panel's Avatar
 
Join Date: Mar 2007
Location: Warsaw
Posts: 1,991
Send a message via Skype™ to panel
Default

Like This
ActionScript Code:
import flash.display.StageScaleMode;     import flash.events.EventDispatcher;     import flash.events.Event;     public class Resizer extends Sprite {         //vars         private var interface_mc:MovieClip;         //constructor         public function Resizer(interfaceRef:MovieClip) {             addEventListener(Event.Event.ADDED_TO_STAGE, onAddedToStage)                     }         private function init():void {             stage.scaleMode = StageScaleMode.NO_SCALE;             stage.align = StageAlign.TOP_LEFT;             stage.addEventListener(Event.RESIZE, stageScaled);             stageScaled();         }         protected function onAddedToStage(e:Event):void {             //here stage property isn't null             init();         }         protected function stageScaled(e:Event){             //do things         }     } }

Last edited by dr_zeus; 02-13-2008 at 08:13 PM. Reason: fixed typo
panel is offline   Reply With Quote
Old 02-15-2008, 08:48 PM   #6
Rocketboy
Senior Member
 
Join Date: Apr 2006
Location: Brooklyn
Posts: 109
Wink

I ended up using the stage property of a reference to an object on the stage that I was passing into the constructor of the class, as panel suggested, and that seems to have worked. Thanks!

Code:
public function Resizer(interfaceRef:MovieClip){
			trace("***Resizer CONSTRUCT");
			//setup stage scale properties
			interfaceRef.stage.scaleMode = StageScaleMode.NO_SCALE;
			interfaceRef.stage.align = StageAlign.TOP_LEFT;
			//create stage size listening object	
			interfaceRef.stage.addEventListener(Event.RESIZE, stageScaled);
			stageScaled();
		}
__________________
-jack
www.jackkalish.com
Rocketboy is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Creation-time Application Event: Stage Object Available astgtciv Flex 2, 3 & 4 6 07-29-2006 04:01 PM
dynamically resizing the stage for a .flv player? ykol20 ActionScript 2.0 2 01-03-2005 02:38 PM
loadmovie and SWF stage dimensions ahayes1ic ActionScript 1.0 (and below) 0 02-11-2004 08:08 AM
I want to deactivate Main stage buttons while movie clip runs kunafish Simple Stuff (Newbies) 3 07-29-2002 11:34 PM
_x, _y, scale and centering on the stage spoonyg ActionScript 1.0 (and below) 3 10-11-2001 01:59 PM


All times are GMT. The time now is 02:06 PM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.