Hello,
I’m trying to load some PNGs onto the stage and make them move around. However, I can’t seem to grasp Loader. I get this error:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Cloud/::imgLoaded()
Here’s the Cloud class that generates it:
package
{
import flash.display.Sprite;
import gs.TweenLite;
import flash.events.Event;
import com.gskinner.utils.Rnd;
import flash.display.Loader;
import flash.net.URLRequest;
public class Cloud extends Sprite
{
var loader:Loader = new Loader();
function Cloud():void
{
var request:URLRequest = new URLRequest('cloud' + Rnd.integer(1,4) + '.png');
loader.load(request);
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, imgLoaded);
}
function imgLoaded(e:Event):void
{
this.addChild(e.target.content);
this.y = Math.random() * 100;
this.x = Math.random() * 100;
TweenLite.to(this, 2, {x:stage.stageWidth, y:this.y});
if( this.x <= 0 )
{
TweenLite.to(this, 2, {x:stage.stageWidth, y:this.y});
}
else if( this.x >= stage.stageWidth )
{
TweenLite.to(this, 2, {x:0, y:this.y});
}
}
} // end class
}
On the first frame of the main timeline I run this:
function populateStage():void
{
for(var i:int; i < 2; i++)
{
var aCloud:Cloud = new Cloud();
aCloud.y = Math.random() * 100;
aCloud.x = Math.random() * 100;
}
}
//stop();
populateStage();
I know a null reference means it’s pointing to nothing, but why is that? My code has it pointing to e.target.content right? Anyone know why I’m getting that error?
Thanks in advance.