A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: Timer Help: Setting Indivual Timers To Sprites In An Array

  1. #1
    Junior Member
    Join Date
    Jun 2009
    Posts
    6

    Timer Help: Setting Indivual Timers To Sprites In An Array

    What I have:
    Code:
    var glideShootingTimer:Timer = new Timer (500);
    glideShootingTimer.start();
    glideShootingTimer.addEventListener(TimerEvent.TIMER, glideShoot);
    function glideShoot(evt:Event)
    {
    	for (var g:Number = 0; g<glideArray.length; g++)
    	{
    		if (glideArray[g]._shoot == true)
    		{
    		//create new instance of torpedo
    		var newTorpedo = new EnemyTorpedo();
    		//set the torpedo's X and Y properties
    		newTorpedo.x = glideArray[g].x;
    		newTorpedo.y = glideArray[g].y;
    		//set the rotation, and give it a property, so we can animate it correctly
    		newTorpedo.rotation = newTorpedo._rot =  glideArray[g].rotation;
    		//stuff it into the array
    		torpedoArray.push(newTorpedo);
    		//put it on the stage
    		addChild(newTorpedo);
    		//put it on index just under glide that shot it
    		setChildIndex(newTorpedo, getChildIndex(glideArray[g]) - 1);
    		}
    	}
    }
    What it does:
    Every half second create a torpedo underneath every "glide" sprite on the stage.

    What is wrong:
    Every "glide" fires at the same time as the others.

    What I need:
    A way to individually time the firing. A way to make it fire in independent half second intervals, not all at once.

    Help is appreciated! Thanks!
    Last edited by iambubbathedog; 06-21-2009 at 12:36 AM.

  2. #2
    Senior Member cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,448
    Instead of using a for loop create a random number:

    var g:int = int(Math.random() * glideArray.length-1);

    But you need to work on the childindex. Use a counter for the childindex, which continuously counts up.
    Last edited by cancerinform; 06-21-2009 at 06:40 AM.
    - The right of the People to create Flash movies shall not be infringed. -

  3. #3
    Junior Member
    Join Date
    Jun 2009
    Posts
    6
    That will work for making them fire one at a time every half second, but I want them all to be able to fire whenever they come within range. Maybe if I made the interval (500/glideArray.length) that would work right. Thanks, but I don't think that completely solves my issue.

  4. #4
    Junior Member
    Join Date
    Jun 2009
    Posts
    6
    This is what I changed and it seems to work well enough, but if anyone has a better way of doing what I want to do let me know.

    Code:
    var glideShootingTimer:Timer = new Timer (500);
    glideShootingTimer.start(); // place this in if statement for faster run time (Start/Stop when needed)
    glideShootingTimer.addEventListener(TimerEvent.TIMER, glideShoot);
    function glideShoot(evt:Event)
    {
    	glideShootingTimer.delay = (500/glideArray.length);
    	var g:int = int(Math.random() * glideArray.length-1);
    		if (glideArray[g]._shoot == true)
    		{
    		//create new instance of torpedo
    		var newTorpedo = new EnemyTorpedo();
    		//set the torpedo's X and Y properties
    		newTorpedo.x = glideArray[g].x;
    		newTorpedo.y = glideArray[g].y;
    		//set the rotation, and give it a property, so we can animate it correctly
    		newTorpedo.rotation = newTorpedo._rot =  glideArray[g].rotation;
    		//stuff it into the array
    		torpedoArray.push(newTorpedo);
    		//put it on the stage
    		addChild(newTorpedo);
    		//put it on index just under glide that shot it
    		setChildIndex(newTorpedo, getChildIndex(glideArray[g]) - 1);
    		}
    	
    }
    And what's wrong with the child index?

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
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center