Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Newcomers DBPro Corner / Moving sprites at different speeds.

Author
Message
Azraine
17
Years of Service
User Offline
Joined: 22nd Oct 2007
Location:
Posted: 22nd Oct 2007 23:24
New here, im trying to figure out how to move my sprite at a speed lower than an integer, without using the "move sprite" command. i dont like have to mess around with the angles, as it affects the images i have to draw, and its annoying.


thats what i have so far, but i want to make the y value move slower, and 1 is the lowest integer arggghhhh, anyone have a solution?
(Sorry if my code is utter nonsense!, new)
LBFN
17
Years of Service
User Offline
Joined: 7th Apr 2007
Location: USA
Posted: 23rd Oct 2007 02:43
Instead of adding to the sprite y() value, you could establish float variables to handle a smaller addition, like this :


A better way is to simply turn the sync on and set the sync rate, like this:

Otherwise your code's speed will depend on the machine's speed in which it is being ran. Moving by 1 is not very much, let alone going with smaller increments than that. It can be done, as you see in the first example, but I would recommend against it.

LB
Azraine
17
Years of Service
User Offline
Joined: 22nd Oct 2007
Location:
Posted: 23rd Oct 2007 22:01
thank you very much, i love it, im very new to this, and didnt even realise you could do that with sync. I did kind of realise that my code would depend on the speed of the machine (Well i guessed) so im glad you fixed that for me, your awesome! cheers !
TDK
Retired Moderator
22
Years of Service
User Offline
Joined: 19th Nov 2002
Location: UK
Posted: 30th Oct 2007 05:30 Edited at: 30th Oct 2007 05:33
Quote: "im trying to figure out how to move my sprite at a speed lower than an integer"


It's useful to understand the reasoning behind LBFN's first example as it shouldn't really work (but it obviously does).

First of all, all 2D screen co-ordinates are integers because they are pixels - each one a whole integer value.

So, it's impossible to paste a sprite at 134.2 pixels across. As such, the X and Y co-ordinates should be integers.

However, if your X co-ord variable is a float, the Paste Sprite command will convert it to an integer (not round it up or down).

134.2 will just become 134.

If you add 0.2 to it, it becomes 134.4 - which is still 134 as an integer. In fact, you have to add 0.2 to 134.0 five times before it becomes 135.0 - at which time the integer becomes 135 and the sprite moves one pixel.

The result is that the sprite moves 5 times slower than using integers. But, the speed the viewer sees the sprite moving still depends on the speed of the machine they are using - even when using Sync Rate.

The only accurate way to fix the speed of a moving sprite is to use the Timer() function so that the paste sprite function is only called after a fixed number of milliseconds have elapsed. Even then, it will only work with faster machines - very slow ones won't be able to keep up with the timing rate and will still move the sprite slower...

TDK_Man

Zeus
18
Years of Service
User Offline
Joined: 8th Jul 2006
Location: Atop Mount Olympus
Posted: 1st Nov 2007 13:41
Normally, I have the sprite's coordinates in a variable,

x=0
y=0

and then I put the sprite there (duh)

do
sprite 1,x,y,1
sync
loop

to make it move,



STOP reading my signature!

Login to post a reply

Server time is: 2024-11-25 12:55:16
Your offset time is: 2024-11-25 12:55:16