PDA

View Full Version : What is the best way to deal with very slow 2d motion?



awflasher
February 13th, 2009, 06:42 AM
Making Motion in AS3 is not hard, but to make a very very slow motion, I met a lot of troubles today.

Well, I have some stuff on my stage and I wanna move them very very slowly, using a "Timer", then I have two ways to go. But both of them have disadvantages.

1) Make the interval as small as possible, and add 0.005 to sprite.x in each time handler;

Disadvantage:

I'm afraid Flash was working in a strange way since as2 when dealing with small float numbers, sometimes the x value of a sprite is even not changing when "sprite.x += 0.005" is executed.

2) Add 1 to sprite.x in each time handler but make the interval much bigger.

Disadvantage:

To let the sprite move in a bigger interval could also slow it down, even save a lot of the CPU cost. But then the animation is performing very bad smoothing.

So is there anybody who has a better solution for this case? Maybe not using Timer? Anyway, thanks in advance!

justkevin
February 13th, 2009, 10:02 AM
I can't find anything about this in the docs, but it seems Flash doesn't track movement at a scale below ~1/20 of a pixel. I store my object's position separately, then update the display object's coordinates.

E.g.:
protected var _x:Number = 0.0;
protected var _y:Number = 0.0;
protected var sprite:Sprite = new Sprite();
...

this._x += dx;
this._y += dy;
this.sprite.x = this._x;
this.sprite.y = this._y;

substance
February 13th, 2009, 02:48 PM
I would go with a tweener engine honestly. Basically it would allow you to say "move to this point over the course of XX seconds" and it will do the rest for you.

sandrapaul
February 18th, 2009, 06:59 AM
Quite interesting.