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.

DarkBASIC Discussion / function problems

Years of Service
User Offline
Joined: 4th Sep 2011
Posted: 3rd Oct 2011 23:07
so i was trying to write a library to load animated sprites and play them to do this i wrote two seperate functions. but i ran into a problem. when i try to control the playspeed of two seperate sprites they bot play at them same speed. i really cant figure this one out so any help would be very much appreciated.

this is the actual "game"

this is the #include file

sry if its not very clean right noww, im so busy trying to figure it out i having practiced good coding.
Years of Service
User Offline
Joined: 7th Apr 2007
Location: USA
Posted: 4th Oct 2011 05:54 Edited at: 4th Oct 2011 05:58
I noticed the Playsprite function is not even called from the code you listed.

Why so many timers? You don't need all of those. When you go to play the animation, store the current time + whatever delay you want (say 100), and add that to the time so you know when to increment the frame. Something like this:

Also, I coded it to use the text command instead of using print commands, that way it will always be in the same spot.

You called for it to SYNC at the end of your function; this will slow down your game unnecessarily. Leave the SYNC in your main game loop, but don't add a SYNC to your functions.

The variable, tyme, will need to be made an array and that way you can use it for multiple sprites. DIM this array early in the code.

If you call the Playsprite function from your main loop, it should work. I see you have SYNC ON. Suggest your set the SYNC RATE to 60.

Years of Service
User Offline
Joined: 23rd Jul 2006
Posted: 4th Oct 2011 06:06

To add onto what LBFN said:

Variables in DBC functions are static. That means that the variables continue to hold whatever value they were assigned even after the function has returned from it's initial call. In your function that means the timing would be the same for each sprite you send to the function.

To help solve this, you should use an array that can store separate timing for each sprite. There are a couple of ways to manage this where you can use memory management techniques to keep your arrays only as big as you need. Or you can use a brute force method which just sets aside an array sized to 65535 indexes where each index is a sprite number.

At any rate, the things we'll probably want to keep track of for each sprite are:

1. The image numbers that make up the animation. Hopefully these are all sequential in which case we can use start and end numbers like you have in your function
2. The current frame that is being displayed
3. The time in ms before a frame can advance for the current sprite
4. A direction for the steps between frames. This can allow us to go forward or backward through an animation.

Something to keep in mind also is the overall speed of your animation is going to be limited by your programs sync rate and the number of images that make up the animation. Each image in the sprite will only be displayed (updated) after a sync is called. So if your sync rate is 60, for example, you will only be able to change images at a maximum rate of 17 ms per frame. So if you had a sprite animation made up of say 360 images (a complete rotation at 1 degree per image for example) it would take about 6 seconds to complete the animation.

So, in building your animation function, you may also want a step parameter so you can skip frames as necessary.

Enjoy your day.

Login to post a reply

Server time is: 2023-06-02 23:34:14
Your offset time is: 2023-06-02 23:34:14