views:

269

answers:

2

Hey,

I have a code here that drops a first row then drops a second row down right after. First row drops right away and second row is on a 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);
}

How would I drop each cover individually at random between two set numbers? I assume I would have to apply a separate timer for each one then somehow make the time at which it drops random between two numbers?

Not sure how to go about doing that though.

Thanks, Wade

A: 

I'm not sure what you mean by 'drop', 'row' and 'cover' in this context, but to make 2 randomly timed events between times T1 and T2 you could chain the timers together like this:

// 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...
}

Does this help? Or have I misunderstood your question?

Richard Inglis
Thanks, I'll be giving this a try in a bit, looks like it could work but I'll let you know if it works out.Cover, drop and row were just terms I was using to describe my Flash composition. I have 8 album covers spread across two rows that drop from the top.
Wade D Ouellet
A: 

Here's one way of doing it:

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