If image = null do nothing

This code works apart from when the image = null.

So I have tried a few extra parts of code like lf then … but always get error

Error #2044: Unhandled IOErrorEvent:. text=Error #2035: URL Not Found.

how would I get it to load a default “0.jpg” if it was = null.

Any help be appreciated

Steven

function picLoad():void
{
	trace (trackNumber);
	picLoader = new Loader();
	picLoader.load(new URLRequest(dir + trackNumber + ".jpg"));
	picLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onImageLoaded);
}

/*********************************************************/

function onImageLoaded(e:Event):void
{
	var bit:Bitmap = e.target.content;
	if(bit != null)
	{
	bit.smoothing = true;
	bit.x = -(picLoader.width/2);
	bit.y = -(picLoader.height/2);
	art_MC.addChild(picLoader);
	}
}

You’re getting an unhandled IO error event, so handle it. Something like:

picLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onImageLoadFailure)

function onImageLoadFailure(e:* = null):void {
    picLoader.load(new URLRequest('0.jpg'))
}

Thanks krilnon :smile:

Had to change code a little to remove previous child elements.

 /******** ! F U N C T I O N S ! **************************/

function onImageLoadFailure(e:* = null):void
{
    picLoader.load(new URLRequest(dir + '0.jpg'));
	picLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onImageLoaded);
}

/*********************************************************/

function picLoad():void
{
	
	
	picLoader = new Loader();
	picLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onImageLoadFailure);
	picLoader.load(new URLRequest(dir + trackNumber + '.jpg'));
	picLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onImageLoaded);
}

/*********************************************************/

function onImageLoaded(e:Event):void
{
	var bit:Bitmap = e.target.content;
	if(bit != null)
	{
	bit.smoothing = true;
	bit.x = -(picLoader.width/2);
	bit.y = -(picLoader.height/2);
	art_MC.removeChildren();
	art_MC.addChild(picLoader);
	}
}
	
/*********************************************************/

Thanks again from a 48yr old newbie to Xpert.

1 Like