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 Professional Discussion / Player Sprite moves way more than 1 tile when direction key pressed

Author
Message
Frank C
16
Years of Service
User Offline
Joined: 3rd Jul 2010
Location: Houston TX
Posted: 17th Aug 2010 21:41
I have implemented PlayerMovement code for my 2D tilebased RPG but when I press the direction keys the player sprite moves much further than just one tile. What am I missing to limit the movement?

Hawkblood
16
Years of Service
User Offline
Joined: 5th Dec 2009
Location:
Posted: 17th Aug 2010 21:45
Use something like this:

for each of your direction keys. This will basically wait for you to release the key before proceeding.

The fastest code is the code never written.
Frank C
16
Years of Service
User Offline
Joined: 3rd Jul 2010
Location: Houston TX
Posted: 17th Aug 2010 21:58
So should I place this just before the end of the key section i.e.

Hawkblood
16
Years of Service
User Offline
Joined: 5th Dec 2009
Location:
Posted: 17th Aug 2010 21:59
Yes. That will do it.

The fastest code is the code never written.
Frank C
16
Years of Service
User Offline
Joined: 3rd Jul 2010
Location: Houston TX
Posted: 17th Aug 2010 22:08
thanks that worked, all I got to do now is get my map scrolling to work correctly
Rasdini
22
Years of Service
User Offline
Joined: 20th Feb 2004
Location:
Posted: 19th Aug 2010 23:21 Edited at: 19th Aug 2010 23:31
one of the most dangerous lock up problems comes from the while endwhile. The longer the key is pressed, the longer the game freezes. I use code something like this:

[/code]
if upkey()=0 then releaseupkey=0
rem option
if timer()>releaseuptimer then releaseupkey=0

rem in one line could read
if upkey()=0 or timer()>releaseuptimer then releaseupkey=0

if upkey()=1 and releaseupkey=0
releaseupkey=1
Player movement code here.
rem option to add time delay with something like this:
releaseuptimer=timer()+200
endif
[code]

this assigns a variable a value of one and thus cannot enter code sequence until either key is released or timer()>time nabbed+200. At the same time, you are still looping throught the program and other computing can occur, like AI and environment controls keep working.

I use while endwhile only for file access and I know I will eventually get a -1 for get file type() and leave the loop as long as I remember my find next before the endwhile.


Richard

Login to post a reply

Server time is: 2026-07-24 20:35:42
Your offset time is: 2026-07-24 20:35:42