Hi Andreas,
as for why it behaves like this, it would be easier if you sent more
code (especially the "die" method, where you declare "age", etc). But
if I were you I'd totally rewrite that part anyway.
I remember that you asked about lookup tables, so you try to be
efficient. You likely want to have as many bullets as possible at a
time, and still have some time left to do other stuff (like steering
the baddies). *Avoid* *function* *calls*. I can't stress this enough.
Calling a method on an object is the most expensive thing you can do
in AS. Trying to save time with lookup-tables and then doing three
function calls on each bullet is being "penny wise and pound foolish".
It's ok to have "die" as a method because bullets don't die often. But
"update" should be called only once and then loop through the bullets,
not the other way round (looping through the bullets and calling
"update" on each). Same with "checkEvents" and "move". Also, when you
create them, you can give them a deathDate property you test "time"
against. That way you move part of the math to creation/death time and
not do it each frame when it can be avoided.
Hth,
Mark
On 4/17/06, Andreas Rønning <[EMAIL PROTECTED]> wrote:
> 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
>
--
http://snafoo.org/
_______________________________________________
[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