|
-
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.
-
Senior Member
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. -
-
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.
-
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|