[AS3]随意做一些事情而不重复

时间:2012-03-22 11:00:18

标签: arrays actionscript-3 random

我在舞台上有3个movieclip,分别是mc1,mc2,mc3 起初他们是alpha = 0

我想要的是当我点击revealBtn时,其中一个会显示为alpha = 1.

但是使用下面的代码,有时我需要点击大约5次或更多才能让所有这些mc显示出来。

我想要的是什么解决方案?我试过拼接,但它仍然不能正常工作。

var mcArray:Array = [mc1,mc2,mc3];
for (var j:int = 0; j < mcArray.length; j++)
{
    mcArray[j].alpha = 0;
}

revealBtn.buttonMode = true;
revealBtn.useHandCursor = false;
revealBtn.addEventListener(MouseEvent.CLICK, revealClick);

function revealClick(event:MouseEvent):void
{
    var i:Number = Math.floor(Math.random() * mcArray.length);
    var movieClipToEdit:MovieClip = mcArray[i] as MovieClip;
    movieClipToEdit.alpha = 1;
}

2 个答案:

答案 0 :(得分:1)

这是许多可能的解决方案之一。它破坏了初始阵列。如果您不想更改初始数组,其余的则取决于您实际想要实现的目标。

var invisibleList:Array = [mc1,mc2,mc3];
for (var j:int = 0; j < invisibleList.length; j++)
{
    invisibleList[j].alpha = 0;
}

revealBtn.buttonMode = true;
revealBtn.useHandCursor = false;
revealBtn.addEventListener(MouseEvent.CLICK, revealClick);

function revealClick(event:MouseEvent):void
{

    if (invisibleList.length == 0) {
        return;
    }
    var i:Number = Math.floor(Math.random() * invisibleList.length);    
    var movieClipToEdit:MovieClip = invisibleList[i] as MovieClip;
    invisibleList.splice(i, 1);
    movieClipToEdit.alpha = 1;
}

答案 1 :(得分:0)

制作第二个数组作为选择源。每次从第二个数组中选择一个项目Splice时。另外,由于您的所有商品都是MovieClip,因此您应该使用Vector

var mcVector:Vector.<MovieClip> = [mc1,mc2,mc3]; 
var vector2:Vector.<MovieClip> = mcVector.Slice(0); // This just copies the Vector
for (var j:int = 0; j < mcVector.length; j++) 
{ 
    mcVector[j].alpha = 0; 
} 

revealBtn.buttonMode = true; 
revealBtn.useHandCursor = false; 
revealBtn.addEventListener(MouseEvent.CLICK, revealClick); 

function revealClick(event:MouseEvent):void 
{ 
    var i:Number = Math.floor(Math.random() * mcVector.length); 

    // Retrieves and deletes the item in one step:
    var movieClipToEdit:MovieClip = vector2.Splice(i, 1); 
    movieClipToEdit.alpha = 1; 
}