So i'm just playing around building a bullet system for my shooting
game. I want two things from the outset; i want it time based rather
than frame based, and i want it to support events for bullets based on
their age, for instance dying when 30 seconds have passed, aiming at the
player when 10 seconds have passed etc.
This is all basic stuff and i just set up a prototype to play around
with as i think it through. My issue is with getTimer()
<as>
//BEFORE
//on the root
function updateBullets() {
for (var i = bullets.length; i--; ) {
bullets[i].update();
}
}
//in Bullet instance
public function update(){
var thisTime = getTimer();
var time = (thisTime-lastTime)/100;
lastTime = thisTime;
age+=time;
trace(ID+":"+age);
checkEvents();
if(age>=lifeSpan){
die();
}else{
move(time);
}
}
</as>
Now i realize putting the time checking in every bullet instance on every
update may not be the cleverest thing in the book, but it was just to get it
done so what the hell.
However, i get odd, ODD results this way. I would trace out the bullet's ID,
it's current age and the value of the time variable on every update, and it'd
read something like this:
1 - 10.24 - 0.61
1 - 10.83 - 0.60
etc etc. This is all fine and dandy, and what i was expecting to see. But as
soon as one of the bullets reach their lifespan and die() (which removes the
bullet from the bullets array)
all the remaining bullets and all bullets created from there on trace out their
age and time like this:
2 - 42.04 - 42.04
2 - 42.24 - 42.24
At the same time, all bullets somehow get the same age at all times, from
creation and so forth (even though they are all instanced with age = 0), which
means ALL bullets will immediatly reach their lifespan and die(),
and they will all be at the same position too since i get their position by
multiplying their vector by time.
So i'm tearing my hair out, what the hell? While i'm knocking things over to work it out, i decide to put my mind at rest by simply copy pasting the time calculation from the Bullet class
into the updateBullets function, so the result becomes as such:
<as>
//NOW
//on the root
function updateBullets() {
var thisTime = getTimer();
var time = (thisTime-lastTime)/100;
lastTime = thisTime;
for (var i = bullets.length; i--; ) {
bullets[i].update(time);
}
}
//in Bullet instance
public function update(time:Number){
age+=time;
trace(ID+":"+age);
checkEvents();
if(age>=lifeSpan){
die();
}else{
move(time);
}
}
</as>
And ta-da, now it all works admirably. I can't for the love of god
figure out why this didnt work the other way (no matter how dirty that
was), and it's making me feel like an amateur again, so please.. Can
someone put my mind at ease and explain this behavior? Literally all i
did to make it work was copy paste the 3 lines of AS. Aggrevating.
Cheers,
- Andreas
_______________________________________________
[email protected]
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com