Time-Based but still too fast


(Secret) #1

Hi guys, I’m trying to make a Snake Clone and I have this code.

    if(_timer < 10000){
	_timer += FP.elapsed;
}else{
	_timer -= 10000;
	move();
	moveEntities();
}

Basically what I want it to do is to wait one second then it should move. All of this is in the update method of the Entity. Now I think this should work but whenever I run it, the Snake goes zooming very fast like it’s calling the move() and moveEntities() function everytime it updates. Changing the amountof milliseconds doesn’t change anything.

Any insight on what I’m doing wrong here?

The move() and moveEntities() are just functions that enable the snake to move a tile at a time with conditions on control etc… so I don’t think the problem is in those methods.


(Nate ) #2

is _timer declared as a Number?

EDIT:

Not sure what might be the issue without seeing _timer, and without knowing what your functions do.

Just for making it easier to test I would either take out the function calls or try something like this:

if(_timer < 10000){ _timer += FP.elapsed;}

if(_timer == 10000){_timer -= 10000; move(); moveEntities();}

(Jacob Albano) #3

One possibility: You never initialize _timer to 0, so it starts as NaN and causes undefined behavior.

Alternately, you could use an alarm for this too:

function tick():void
{
    move();
    moveEntities();
}

addTween(new Alarm(1, tick, Tweener.LOOPING), true);

Adding distance to my projectiles
(Secret) #4

Yes it is a number.

I just found out the culprit, the _timer is not initialized so it automatically goes to the else part of the condition since it’s null I guess. Wow, this is weird since I’d expect an error if this was another language. Anyway, better be more careful next time.


(Jacob Albano) #5

In AS3, ints and uints are default-initialized to 0, and Numbers are default-initialized to NaN. It’s caused me some grief more than a few times.


(Secret) #6

Oh I just realized we replied at almost the same time. Wow, didn’t know about that with the ints and Numbers. Interesting. Any idea why is is like that?

Also, I’m interested in the Tween but I never got to try it yet. What advantage does it have over not having tween and is it the more “standard” way of doing stuff in AS3?


(Jacob Albano) #7

Number is a floating point type, which means it can store decimal numbers. It can also represent invalid values, like NaN (Not a Number), Math.POSITIVE_INFINITY, and Math.NEGATIVE_INFINITY. I’m not sure why NaN is the default value, but I’m sure smarter people than I have a good reason for it. :slight_smile: It can get annoying if you forget about it though.


(Jacob Albano) #8

Oh, and as for your second question: I like using Tweens (especially Alarms) because they let you set up complicated chains of events without having to add in a bunch of timing code to your entities. Just make a function for it to call when it finishes, and it’s fire and forget.