2010-01-30 116 views
1

我在這裏有一個代碼,它會丟棄第一行,然後在第二行下降第二行。第一行立即下降,第二行放在計時器上。as3隨機丟棄物體

// 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); 
} 

我該如何在兩個設定的數字之間隨機地分別放置每個封面?我假設我將不得不爲每一個計算器分別應用一個計時器,然後以某種方式在兩個數字之間產生隨機數的時間?

不知道如何去做這件事。

感謝, 韋德

+0

你是說,而不是丟棄,一個行那麼其他的,你希望每個8張專輯,4每一行中,所有的下拉隨機並讓他們在一段時間內完成? – sberry 2010-01-31 22:01:42

+0

是的,正是我在找的東西。 – 2010-02-02 08:08:54

回答

0

我不知道你在這方面「降」,「行」和「蓋」的意思,反而使時間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... 
} 

這有幫助嗎?或者我誤解了你的問題?

+0

謝謝,我會稍微嘗試一下,看起來好像可以工作,但如果解決問題,我會告訴你。 封面,下拉和排只是我用來描述我的Flash組合的術語。我有8張專輯封面,分佈在兩排從頂部掉落的專輯封面。 – 2010-01-31 18:38:43

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]; 
    } 
}