Results 1 to 4 of 4
Thread: delay a loop?
-
January 11th, 2010, 12:01 PM #138Registered User
postsdelay a loop?
This adds 3 movieclips to the stage all at once. I would like them to be added one after another - perhaps as soon as the tween finishes, the next one is added. I have tried timers but I ended up with 3 timers for 3 functions. Ideally I will be adding up to 10 and eventually I would like the time between each addition to decrease - you know, like when popcorn starts popping, slowly at first then it really starts popping fast. Sort of like that
. Ideas?
Code:var dArr:Array = new Array(); dArr.push({ss:.05, xp:30, yp:35, es:.18}); dArr.push({ss:.05, xp:120, yp:125, es:.28}); dArr.push({ss:.05, xp:30, yp:100, es:.18}); function pop4(e:TimerEvent):void { for (var j:int = 0; j < 3; j++){ var dol4:MovieClip = new dollar(); dol4.scaleX = dol4.scaleY = dArr[j].ss; dol4.x = dArr[j].xp; dol4.y = dArr[j].yp; stage.addChild(dol4); var sm5 = new Tween(dol4, "scaleX", Elastic.easeOut, .05, dArr[j].es, 1, true); popper.play(0,0,soundSetting); var sm6 = new Tween(dol4, "scaleY", Elastic.easeOut, .05, dArr[j].es, 1, true); } }
-
January 11th, 2010, 05:46 PM #210Registered User
postsOnly idea I've got without Timers and whatnot like you mentioned is using the TweenLite class. Has a built in delay function.
I usually don't like when someone asks one thing and someone responds with an answer like this, but it really is easier.
-
January 12th, 2010, 10:27 AM #3
I would say something like this ?
(copy and paste into a new file to see it working)
EDIT - attached as a standalone .fla fileCode:package { import flash.display.Sprite; import flash.utils.Timer; import flash.events.TimerEvent; import flash.events.Event; public class Main extends Sprite { private var amount:int = 100; // amount of popcorn private var delay:Number = 1000; // initial delay private var acceleration:Number = .9; // amount delay is reduced by every 'pop()' private var popcorn:Array=new Array(); // array of objects to add to stage, in this case yellow sprites, in your case something different private var myTimer:Timer; public function Main():void { createPopcorn(); createTimer(); } //Essentially this is adding the items you want on stage to an array - obviously you would add your items to an array totally differently private function createPopcorn():void /*create an array populated with 'popcorn' sprites*/ { for(var i:int=0; i<amount ;i++) { var s:Sprite = new Sprite; s.graphics.beginFill(0xFFFF33); s.graphics.drawRect(0,0,20,20); popcorn.push(s); } } //Self explanatory private function createTimer():void /*add popcorn to stage*/ { myTimer = new Timer(delay,1); myTimer.addEventListener(TimerEvent.TIMER_COMPLETE, pop, false, 0, true); myTimer.start(); } private function pop(e:TimerEvent):void /*Add popcorn to stage. Restart timer if more popcorn*/ { //I think this is where you would run your pop4() code //add popcorn to stage popcorn[0].x=Math.random()*stage.stageWidth; popcorn[0].y=Math.random()*stage.stageHeight; addChild(popcorn[0]); popcorn.shift();// you will still need this to shorten your array //check if there is more popcorn and restart timer if there is if(popcorn.length>0) // and you will need this as well. { delay *= acceleration; myTimer.delay = delay; myTimer.start(); } } } }Last edited by Shaedo; January 12th, 2010 at 10:39 AM.
-
January 12th, 2010, 02:09 PM #438Registered User
poststoo cool. I owe you one. Thanks!

Reply With Quote


Bookmarks