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.

2D All the way! / Getting a sprite to appear and dissappear every 2 seconds

Author
Message
Jan Eise
20
Years of Service
User Offline
Joined: 18th Dec 2003
Location:
Posted: 1st Mar 2009 14:05
Hello!

I'm having trouble with getting sprite 1 to appear (while sprite 2 is hidden) and sprite 1 to dissappear (while 2 gets displayed) every 2 seconds. The thing is, I need the user of the program to be able to do something at the same time, so "wait" commands aren't an option.

This is what i came up with (but somehow doesn't work because the (dis)appearance of the sprites seems very random)


Thanks in advance!
Virtual Nomad
Moderator
18
Years of Service
User Offline
Joined: 14th Dec 2005
Location: SF Bay Area, USA
Posted: 1st Mar 2009 17:59 Edited at: 1st Mar 2009 18:20
try this:



edit: i changed the variable name that holds the last time the switch was flipped to something more appropriate so it wasn't misleading.

i also want to point out that in your original code, you re-set x incorrectly by setting x# to 0 so, in essence, you never re-set the variable; x kept increasing well beyond 80.

Virtual Nomad @ California, USA
AMD Phenomâ„¢ X4 9750 Quad-Core @ 2.4 GHz . 8 GB PC2-6400 RAM
ATI Radeon HD 3650 @ 512 MB . Vista Home Premium 64 Bit
Jan Eise
20
Years of Service
User Offline
Joined: 18th Dec 2003
Location:
Posted: 1st Mar 2009 21:48
Thanks alot!

Very helpful
One thing I don't really understand yet, why did I incorrectly reset x# to 0? When it passes 80 it is reset to zero and the process starts all over again right?
Virtual Nomad
Moderator
18
Years of Service
User Offline
Joined: 14th Dec 2005
Location: SF Bay Area, USA
Posted: 2nd Mar 2009 11:17 Edited at: 2nd Mar 2009 19:51
because x and x# are 2 different variables. by adding the # sign, you declared and set a new variable, x#.

the # sign defines it's content as a real number.
x, without the # sign, is an integer, by default.

a brief example to demonstrate:


in your code, you continuously increment x by 1. when x = 80, you call x# and set it to 0 over and over again because x keeps increasing by 1 forever.

under dbpro help, Main Menu, look at Principles then Datatypes and Variables for lots more information on datatypes, syntax, etc.

enjoy

by the way, i could have simply told you the fact that x and x# were different up front. the reason i showed you a different method is because you specifically mentioned your desire for the sprites to swap every 2 seconds. your way would be dependent on how fast various computers would run your loop. the example i offered relies on actual time and would run "as intended" on various computers (as long as they didn't mess with their system timers).

Virtual Nomad @ California, USA
AMD Phenomâ„¢ X4 9750 Quad-Core @ 2.4 GHz . 8 GB PC2-6400 RAM
ATI Radeon HD 3650 @ 512 MB . Vista Home Premium 64 Bit

Login to post a reply

Server time is: 2024-05-02 06:34:53
Your offset time is: 2024-05-02 06:34:53