Random image work but still get error

Hello

My random image generation works but occasionally an error message came out as follows: -.

Error #2007: Request parameter must be non-null.

Here is my code: -.

var ImgReq01:URLRequest = new URLRequest("images/random/image01.jpg");
var ImgReq02:URLRequest = new URLRequest("images/random/image02.jpg");
var ImgReq03:URLRequest = new URLRequest("images/random/image03.jpg");
var ImgReq04:URLRequest = new URLRequest("images/random/image04.jpg");

var imgList:Array is [ImgReq01, ImgReq02, ImgReq03, ImgReq04];.

var imgRandom = imgList [Math.floor (Math.random () * imgList.length - 1)];

var imgLoader:Loader = new Loader();
imgLoader.contentLoaderInfo.addEventListener (Event.COMPLETE, onComplete);
imgLoader.load (imgRandom);

function onComplete(event:Event):void
{
var randomImage:Bitmap = Bitmap (imgLoader.content);
randomImage.x =-203;
randomImage.y =-280;
addChild (randomImage);
event.currentTarget.removeEventListener (Event.COMPLETE, onComplete);
}

I understand no no matter who might shed light on why I get this error message?

Thank you very much

the line:

var imgRandom = imgList [Math.floor (Math.random () * imgList.length - 1)];

is the place where it fails.

Math.Random () returns a value between 0 and 1. Including zero, excluding 1.

Multiply it by the length of the results list something between 0 and something less than the length of the list.

subtract 1 and you have something less than zero.

That should do it

var imgRandom = imgList [Math.floor (Math.random () * imgList.length)];

No subtraction, just rounding down. Thus, he never returned a number being the length of the table prevent errors on this end. It will never be less than zero because Math.Random () returns nothing less than zero errors preventing on this end.

Tags: Adobe Animate

Similar Questions

Maybe you are looking for