pizza0502
pizza0502

Reputation: 333

AS3 Randomly load movieclip from library without repeatation

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.

Upvotes: 0

Views: 3037

Answers (2)

pizza0502
pizza0502

Reputation: 333

Thanks @shanethehat for the answer. But after the last MC is loaded and press the next button again it will prompt an error, it is because there are no more MC to load from library.

I've modify abit of the code so that when all the MC from library are loaded and the button know it and won't have any errors.

var showMcNum:Number = 0;
var movieList:Array = [mc1,mc2,mc3];

function getRandomMovie():MovieClip
{
    var index:int = Math.floor(Math.random() * movieList.length);
    var mcClass:Class = movieList.splice(index,1)[0];
    return new mcClass();
}

nextBtn.addEventListener(MouseEvent.CLICK, nextClick);

//declare mc outside;
var mc:MovieClip;
function nextClick(event:MouseEvent):void
{
    if (showMcNum < 3)
    {
        //remove mc first
        if (mc && mc.parent)
        {
            removeChild(mc);
        }//(optional) free up old mc for garbage collection
        //now replace the contents of mc with a new random instance
        mc = getRandomMovie();
        addChild(mc);
        mc.x = stage.stageWidth / 2;
        mc.y = stage.stageHeight / 2;
        showMcNum++;
    }
    else
    {
        //code what you wanna do here after all the MC is loaded
    }
}

mc = getRandomMovie();
addChild(mc);
mc.x = stage.stageWidth / 2;
mc.y = stage.stageHeight / 2;
showMcNum++;

Upvotes: 0

shanethehat
shanethehat

Reputation: 15570

If you don't want an MC to repeat, just remove it from the list when you return it.

function getRandomMovie():MovieClip
{
    var index:int = Math.floor(Math.random()*movieList.length);
    var mcClass:Class = movieList.splice(index,1)[0];
    return new mcClass();
}

Here is a version using a second array to allow you to repeat the list as per TheSHEEEP's comment:

function getRandomMovie():MovieClip
{
    if(!movieList.length) {
        movieList = spareList;
        spareList = [];
    }
    var index:int = Math.floor(Math.random()*movieList.length);
    var mcClass:Class = movieList.splice(index,1)[0];
    spareList.push(mcClass);
    return new mcClass();
}

Removing the previous MovieClip

In order to remove the previous MovieClip, you should keep a record of it outside of your nextClick function, so that you can remove it before getting the next one:

//declare mc outside
var mc:MovieClip;
function nextClick(event:MouseEvent):void
{
    //remove mc first
    if(mc && mc.parent) removeChild(mc);
    //(optional) free up old mc for garbage collection
    //now replace the contents of mc with a new random instance
    mc = getRandomMovie();
    addChild(mc);
    mc.x = stage.stageWidth / 2;
    mc.y = stage.stageHeight / 2;
}

Of course you may need to do more than just remove the previous mc. Before you point your mc reference 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.

Upvotes: 1

Related Questions