-1

我试图创建一个小代码,其中舞台上的两个电影剪辑随机出现在随机位置。我可以让一个物体随机出现在舞台上。但是如何获得第二个对象?这是我的代码!

var myTimer:Timer = new Timer (1500);
myTimer.start();
myTimer.addEventListener(TimerEvent.TIMER, update);

function update(event:TimerEvent) : void
{
trace(Math.floor(Math.random() * 100 ));
object.x = Math.floor(Math.random() * 300 ) ;
object.y = Math.floor(Math.random() * 200 ) ;

//object.alpha = Math.floor(Math.random() * 1);
}

谢谢!

4

1 回答 1

1

像这样的东西?

import flash.display.MovieClip;

function createMovieClip($color:uint):MovieClip
{
    // Create an mc and draw a colored square on it
    var mc:MovieClip = new MovieClip();
    mc.graphics.beginFill($color, 1);
    mc.graphics.drawRect(0, 0, 100, 100);
    return mc;
}

function update(event:TimerEvent):void
{
    // Update each MC instance 
    updateMc(mc_1);
    updateMc(mc_2);
}

function updateMc($mc:MovieClip):void
{
    // As your code:
    $mc.x = Math.floor(Math.random() * 300 ) ;
    $mc.y = Math.floor(Math.random() * 200 ) ;
    // Add MC to stage if it isn't there already
    if($mc.parent == null) stage.addChild($mc);
}

// Create variables to hold your movie clips
var mc_1:MovieClip;
var mc_2:MovieClip;

// Create your movie clips
mc_1 = createMovieClip(0xFF0000);
mc_2 = createMovieClip(0x0000FF);

// Start your timer - as your original code
var myTimer:Timer = new Timer (1500);
myTimer.start();
myTimer.addEventListener(TimerEvent.TIMER, update);

// Update once immediately to add MCs to stage
update(null);
于 2012-08-17T12:11:03.130 回答