Main.as takes a movieclip from the library and adds it to the stage (tile bg tutorials only as 3 version).
When index is compiled it works fine. Once I complie preloader, I get “TypeError: Error #1009: Cannot access a property or method of a null object reference.”.
Once I comment out the addChild line everything is fine. So I’m assuming it had to do with scope.
Here’s preloader code
stop ();
import flash.display.Loader;
import flash.display.LoaderInfo;
import flash.net.URLRequest;
import flash.text.TextField;
import flash.events.Event;
import flash.events.ProgressEvent;
var myLoader:Loader = new Loader();
var myRequest:URLRequest = new URLRequest("index.swf");
var loadProgress_txt:TextField = new TextField();
myLoader.load (myRequest);
myLoader.contentLoaderInfo.addEventListener (Event.OPEN,showPreloader);
myLoader.contentLoaderInfo.addEventListener (ProgressEvent.PROGRESS,showProgress);
myLoader.contentLoaderInfo.addEventListener (Event.COMPLETE,showLoadResult);
function showPreloader (evt:Event):void {
addChild (loadProgress_txt);
}
function showProgress (evt:ProgressEvent):void {
//loadProgress_txt.text = "loaded:”+evt.bytesLoaded+" from "+evt.bytesTotal;
}
function showLoadResult (evt:Event):void {
removeChild (loadProgress_txt);
addChild (myLoader);
}
Main.as
package {
import flash.display.*;
import flash.events.*;
import flash.utils.*;
public class Main extends Sprite {
//
//
//
//
private var tileWidth:Number;
private var tileHeight:Number;
private var xMax:Number;
private var yMax:Number;
private var sHeight:Number;
private var sWidth:Number;
private var bg:Sprite;
//
//
//
//
public function Main () {
tileWidth = 36;
tileHeight = 36;
sHeight = stage.stageHeight;
sWidth = stage.stageWidth;
xMax = Math.round(sHeight/tileWidth);
yMax = Math.round(sWidth/tileHeight);
for (var i:int = 0; i<= xMax + 1; i++) {
for (var j:int = 0; j <= yMax + 1; j++) {
var myPattern:Pattern = new Pattern();
addChild (myPattern); // this is the line
myPattern.x = tileHeight * j;
myPattern.y = tileWidth * i;
}
}
}
}
}
I tried simple putting a this.addChild(myPattern) but that doesn’t work. I’m hoping someone can explain this to me.
The did clear up some things so thank you for that. Although that did not fix my problem. I’m not sure if I am just looking at this too long or what :lol:
I’ll give it a go tomorrow morning, and I thank you for your help.
this.parent trace to [object Stage], which is correct, right?
Because I want to add the movieclip to the stage scope not the event scope.
So when I test it alone of course it works just fine. But once I compile my preloader swf I get
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Main()
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Main()
Which isn’t making much sense to me. It appears that preloader.swf needs and object reference to the movieclip being attached inside it’s child swf. Why? main.swf is it’s own scope, correct?
I’m getting used to as3 myself… the issue is the stage references, and more so, I believe that they’re run in the constructor of your class. if you trace(stage) when its loaded into the other swf, you’ll notice it traces null.
I ran a test and this did NOT work when loading index.swf thought I’d bring it up: There is an addedToStage event - so when loading, instead of using Event.COMPLETE, I tried:
ActionScript Code:
[LEFT]myLoader.[COLOR=#000080]contentLoaderInfo[/COLOR].[COLOR=#000080]addEventListener[/COLOR] COLOR=#000000[/COLOR];
[/LEFT]
But, stage is still null. So, when its loading into another swf, the stage property of your Main class isn’t being set before your constructor runs.
I can’t say this is the proper way - but attached are how I would set this up - I’d use the Doc class to generate the building blocks of my swf, but only the building blocks, not actual graphic execution. Files attached, hope this helps and if you find a more proper way around that issue, please post, thanks!
Hey folks. The issue you’re having is that the child swf is accessing the stage before the loader has been loaded to the stage. The document class is a great place to do graphics and such - so the best way to fix this issue is to make your constructor look like this:
public function MyConstructor()
{
addEventListener(Event.ADDED_TO_STAGE, init);
}
Now put whatever you had inside your constructor into the init() function, and you should be good to go. :thumb:
Thanks for clarifying anogar! Thats where I finally arrived at on my last set of posted files… lots to learn with AS3. So you got away from sunny SD and the other s workplace eh?