as3随机丢弃对象

时间:2010-01-30 20:56:44

标签: flash actionscript-3 actionscript timer

我这里有一个代码,它会删除第一行,然后再将第二行删除。第一行立即下降,第二行在计时器上。

// Bring in first row
var blueY:Tween = new Tween(blue, "y", Bounce.easeOut, -295, -49, 2, true);
var pinkertonY:Tween = new Tween(pinkerton, "y", Bounce.easeOut, -295, -49, 2, true);
var oddsEndsY:Tween = new Tween(oddsEnds, "y", Bounce.easeOut, -283, -37, 2, true);
var raditudeY:Tween = new Tween(raditude, "y", Bounce.easeOut, -283, -37, 2, true);

// Create and start second row timer
var coverDropTimer:Timer = new Timer(500, 1);
coverDropTimer.addEventListener(TimerEvent.TIMER, dropCovers);
coverDropTimer.start();

// Bring in second row on timer
var greenY:Tween;
var maladroitY:Tween;
var makeBelieveY:Tween;
var redY:Tween;
function dropCovers(e:TimerEvent):void{
    greenY = new Tween(green, "y", Bounce.easeOut, -460, -40, 2.5, true);
    maladroitY = new Tween(maladroit, "y", Bounce.easeOut, -460, -40, 2.5, true);
    makeBelieveY = new Tween(makeBelieve, "y", Bounce.easeOut, -457, -37, 2.5, true);
    redY = new Tween(red, "y", Bounce.easeOut, -457, -37, 2.5, true);
    coverDropTimer.removeEventListener(TimerEvent.TIMER, dropCovers);
}

如何在两个设定数字之间随机单独删除每个封面?我假设我必须为每个人应用一个单独的计时器然后以某种方式使它在两个数字之间随机下降的时间?

不知道如何去做。

谢谢, 涉

2 个答案:

答案 0 :(得分:0)

我不确定你在这个上下文中'drop','row'和'cover'是什么意思,但是要在T1和T2之间制作2个随机定时事件,你可以将这些定时器链接在一起:

// Choose random start time between T1 and T2
var deltaT:int=T2-T1;
var startTime:int = T1 + Math.random()*deltaT;

// Choose random interval (some time before T2)
var waitTime:int = Math.random()*(T2-startTime);

// Create and start first event timer
var firstTimer:Timer = new Timer(startTime, 1);
firstTimer.addEventListener(TimerEvent.TIMER, firstEvent);
firstTimer.start();

function firstEvent(e:TimerEvent):void{
 //do stuff...
 // start the second timer
var secondTimer:Timer = new Timer(waitTime, 1);
secondTimer.addEventListener(TimerEvent.TIMER, secondEvent);
secondTimer.start();
}

function secondEvent(e:TimerEvent):void{
 //do more stuff...
}

这有帮助吗?或者我误解了你的问题?

答案 1 :(得分:0)

这是一种方法:

// Generate the absolute times the covers will drop
// e.g. "Cover 1 will drop 1 second after loading,
// ..Cover 2 will drop 4 seconds after loading, etc."
// MIN_TIME = earliest you want a cover to drop
// MAX_TIME = latest you want a cover to drop
var dropTimes:Array = new Array();
for (var i:int=0; i < numCovers; ++i)
{
    dropTimes.push(Math.random() * (MAX_TIME - MIN_TIME) + MIN_TIME);
}

dropTimes.sort(Array.NUMERIC);

// Convert the times into intervals (how long since the last drop)
for (i = 1; i < numCovers; ++i)
{
    dropTimes[i] = dropTimes[i] - dropTimes[i-1];
}


var coverTweens:Array;

// Insert code here to make an array of cover tweens,
// ..in the order you want them to occur
// If you call myTween.stop() immediately after making a Tween,
// ..it won't play until you call myTween.start() .

var currCover:int = 0;
var coverDropTimer:Timer = new Timer(dropTimes[currCover]);
coverDropTimer.addEventListener(TimerEvent.TIMER, dropNextCover);
coverDropTimer.start();


function dropNextCover(e:TimerEvent):void {
    // Bring the cover in!
    coverTweens[currCover].start();

    ++currCover;

    // Stop if we're at the last cover
    if (currCover >= numCovers) {
        coverDropTimer.stop();
        coverDropTImer.removeEventListener(TimerEvent.TIMER, dropNextCover);
    }
    else {
        // Set the timer for the next cover
        coverDropTimer.delay = dropTimes[currCover];
    }
}