PDA

View Full Version : How does the ADDED_TO_STAGE work?



fusionPT
December 1st, 2009, 11:06 AM
How does the ADDED_TO_STAGE work?

I have a class but it doesn`t work outside the document class. How do i create a new instance of the class i have? It throws me an Error #1009...

this is the class... it works if i put it in the document class field...



package {

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

public class bgTile extends Sprite {

public var _pat:Pattern = new Pattern(0,0);
public var _bgSprite:Sprite = new Sprite();

public function bgTile() {//Constructor
//stage.scaleMode = StageScaleMode.NO_SCALE;
//stage.align= StageAlign.TOP_LEFT;
tile();
addEventListener(Event.ADDED_TO_STAGE, onStageHandler, false, 0, true );
//
}
public function tile() {
_bgSprite.graphics.beginBitmapFill(_pat);
_bgSprite.graphics.drawRect(0, 0, stage.stageWidth, stage.stageHeight);
_bgSprite.graphics.endFill();
addChild(_bgSprite);
_bgSprite.x=0;
_bgSprite.y=0;
}
public function reTile(evt:Event) {
tile();
}
private function onStageHandler(e:Event):void {
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align= StageAlign.TOP_LEFT;
stage.addEventListener(Event.RESIZE, reTile);

}
}
}//Close package


Thanks a lot!

IQAndreas
December 1st, 2009, 11:26 AM
My favorite error. :) Here is my handy dandy copy-paste list which describes EXACTLY why it happens.


TypeError: Error #1009: Cannot access a property or method of a null object reference.
Hands down, with no doubt possible in anyone's mind the absolute #1 error. There are 2 possible causes:


Error #1009 when preloading another SWF:
http://www.kirupa.com/forum/showpost...10&postcount=2 (http://www.kirupa.com/forum/showpost.php?p=2495410&postcount=2)
http://www.kirupa.com/forum/showthread.php?t=330677
http://www.kirupa.com/forum/showthread.php?p=2500293
http://www.kirupa.com/forum/showthread.php?t=333092
Error #1009 in general:
http://www.kirupa.com/forum/showpost...21&postcount=2 (http://www.kirupa.com/forum/showpost.php?p=2500521&postcount=2)




As for your problem specifically, you are running the "tile()" function BEFORE the class "bgTile" has been added to the stage. This is normally no problem, but notice that you are still trying to access the stage in the "tile()" function:
_bgSprite.graphics.drawRect(0, 0, stage.stageWidth, stage.stageHeight);

You can fix this problem by moving the "tile()" call to inside of "onStageHandler()".

I would still recommend checking out the links, though. Any more questions? Should I clarify anything better?

fusionPT
December 1st, 2009, 12:14 PM
Hi, thanks.

i cant get it to work. I have a movieclip called "Pattern" and i tile it with the:


_bgSprite.graphics.beginBitmapFill(new Pattern(0,0));

i believe this is the problem. Can't get the bitmap fill correctly...

Maybe the problem is in the line:
public class bgTile extends MovieClip {

...

Can you help me?
Thanks.

dr_tchock
December 1st, 2009, 12:24 PM
IqAndreas is correct - you are attempting to access the stage before the object is added to it. A solution would be to change your class' constructor name to 'init' and then call this once you have added the instance of that class to the stage. The reason for this is that the constructor function will run as soon as the class is instantiated - before it's added to the stage.

fusionPT
December 1st, 2009, 02:20 PM
Thanks guys. I'm just starting on classes...