我的舞台有3个动画片段,分别是mc1,mc2和mc3。
现在每个人都是alpha = 0。
我在舞台上也有一个按钮,点击它时,1个动画片段将变为alpha = 1.
这是我的剧本:
var mcArray:Array = [mc1,mc2,mc3];
for (var j:int = 0; j < hotSpot.length; j++)
{
mcArray[j].alpha=0;
}
revealBtn.buttonMode = true;
revealBtn.useHandCursor = false;
revealBtn.addEventListener(MouseEvent.CLICK, revealClick);
function revealClick(event:MouseEvent):void
{
//what i need to write here?
}
答案 0 :(得分:0)
这是一个示例,显示如何在单击舞台后显示随机显示对象:
Main.as(文档类):
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
public class Main extends Sprite
{
private var _sprites:Vector.<Sprite>;
public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}// end function
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
_sprites = new Vector.<Sprite>();
for (var i:int = 0; i < 3; i++)
{
var sprite:Sprite = new Sprite();
sprite.graphics.beginFill(0xFF0000);
sprite.graphics.drawRect(0, 0, 100, 100);
sprite.graphics.endFill();
sprite.x = ((sprite.width + 50) * i);
sprite.y = 0;
addChild(sprite);
_sprites.push(sprite);
}// end for
hideSprites();
stage.addEventListener(MouseEvent.CLICK, onClick);
}// end function
private function onClick(e:MouseEvent):void
{
showSprite();
}// end function
private function showSprite():void
{
hideSprites();
(_sprites[random(0, 2)] as Sprite).alpha = 1;
}// end function
private function hideSprites():void
{
for each(var sprite:Sprite in _sprites)
{
sprite.alpha = 0;
}// end for each
}// end function
private function random(min:int, max:int):int
{
return (Math.floor(Math.random() * (1 + max - min)) +min);
}// end function
}// end class
}// end package
简单地说,你使用Math.round()
来获取显示对象数组的随机索引,在这个例子中就是这样:
(_sprites[random(0, 2)] as Sprite).alpha = 1;
private function random(min:int, max:int):int
{
return (Math.floor(Math.random() * (1 + max - min)) +min);
}// end function
您还可以将random()
方法的max
参数的值替换为_sprites.length - 1
,该值也将返回2.
答案 1 :(得分:0)
将我的评论扩展到问题,这是你要一步一步做的事情:
//Generate random number (integer) between 0 and mcArray.length
var i:Number=Math.floor(Math.random() * mcArray.length);
//Select ith element of mcArray
var movieClipToEdit:MovieClip=mcArray[i] as MovieClip;
//Change its alpha to 1
movieClipToEdit.alpha=1;
如果要隐藏所有其他影片剪辑,请添加:
for (var j:int = 0; j < mcArray.length; j++) {
mcArray[j].alpha=0;
}