I have a spot a difference game that every time I solve an image, and I click next it will load another MC from library.
Below is my code to randomly loaded the MC from library:
var showMcNum:Number = 0;
var movieList:Array = [mc1,mc2,mc3];
function getRandomMovie():MovieClip
{
return new movieList[Math.floor(Math.random()*movieList.length)];
}
nextBtn.addEventListener(MouseEvent.CLICK, nextClick);
function nextClick(event:MouseEvent):void
{
var mc:MovieClip = getRandomMovie();
addChild(mc);
mc.x = stage.stageWidth / 2;
mc.y = stage.stageHeight / 2;
}
I would like to every time I click the next button, then it will load another MC from library without repeatation of those MC.
If you don’t want an MC to repeat, just remove it from the list when you return it.
Here is a version using a second array to allow you to repeat the list as per TheSHEEEP’s comment:
Removing the previous MovieClip
In order to remove the previous MovieClip, you should keep a record of it outside of your
nextClickfunction, so that you can remove it before getting the next one:Of course you may need to do more than just remove the previous mc. Before you point your
mcreference at a new object, you should free the other one up for garbage collection by stopping any internal code from executing and removing any listeners.