Everybody! This is important. In a few days, these forums will be moving over to using the totally sweet Discourse platform. To ensure this migration happens smoothly with no loss of content, these forums are currently in a read-only mode. I do apologize for the inconvenience.

There is never a good time to turn the forums off for an extended period of time, but I promise the new forums will be a billion times better. I'm pretty sure of it.

See you all on the other side in a few days, and if you have any (non-technical) questions, please e-mail me at kirupa@kirupa.com. For technical questions, try to find a tutorial that corresponds to what you are looking for and post in the comments section of that page.

Cheers,
Kirupa

Results 1 to 4 of 4

Thread: delay a loop?

  1. #1

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

  2. #2
    10
    posts
    Registered User
    Only 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.

  3. #3
    I would say something like this ?
    (copy and paste into a new file to see it working)


    Code:
    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();
    		}
    	}
    }
    }
    EDIT - attached as a standalone .fla file
    Attached Files Attached Files
    Last edited by Shaedo; January 12th, 2010 at 11:39 AM.

  4. #4
    too cool. I owe you one. Thanks!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012