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 / 2D sprite (Diagonal) Movement

Author
Message
Beyond the Humans Knowledge
15
Years of Service
User Offline
Joined: 27th Oct 2010
Location:
Posted: 28th Oct 2010 07:30
Does anyone know how to make the sprite move diagonal. I got it to move right left down and up, but he wont move diagonal. Here is what i have. Its simple and im trying to keep it that way for this test.

sync on
sync rate 60
load image "media\guy\guy.png",1
X#=0
Y#=0
backdrop on
color backdrop 0
do
sprite 1,X#,Y#,1
if inkey$() = "d" then X# = X# + 3
if inkey$() = "a" then X# = X# - 3
if inkey$() = "w" then Y# = Y# - 3
if inkey$() = "s" then Y# = Y# + 3
sync
loop

i dont know how to use the floats to make it go diagonal. Please help.

onhcet d(-_-)b techno
Van B
Moderator
23
Years of Service
User Offline
Joined: 8th Oct 2002
Location: Sunnyvale
Posted: 28th Oct 2010 10:15
The problem is that inkey$() will only return 1 character at a time, so if you press W and D, you get EITHER W or D, not both.

If you run this:

Sync on : sync rate 0
Do
cls
text 0,0,str$(scancode())
sync
Loop

It will tell you the KEYCODE of the key you are pressing, take note of the WASD keycodes, and then replace the inkey$() part with something like this:

if keystate(33)=1 then inc X#,3.0

33 is just a guess, you will be able to find out whatever keycodes you need with that little program - you could also use the cursor keys, these are indipendant, and easy too. Upkey() for instance returns a 1 when it's pressed. Also note that you can use INC and DEC instead of X#=X#+1.0.

Health, Ammo, and bacon and eggs!
Hodgey
16
Years of Service
User Offline
Joined: 10th Oct 2009
Location: Australia
Posted: 28th Oct 2010 10:35
Here's a little demo to support what VanB was saying about the 'Keystate' command.



A clever person solves a problem, a wise person avoids it - Albert Einstein
Beyond the Humans Knowledge
15
Years of Service
User Offline
Joined: 27th Oct 2010
Location:
Posted: 28th Oct 2010 15:25
omg! thank you so much! i saw this on a video tutorial, but the guy explained it very badly. Thank You!!

onhcet d(-_-)b techno
CSL
17
Years of Service
User Offline
Joined: 22nd Mar 2009
Location: USA
Posted: 29th Oct 2010 00:22 Edited at: 29th Oct 2010 00:27
You can also try using the upkey(), downkey(), leftkey() and rightkey() commands. They are more intuitive and support diagonal movement.



EDIT: Well - Just realized that this works for arrow key movement only not WASD, duh!

CSL
Beyond the Humans Knowledge
15
Years of Service
User Offline
Joined: 27th Oct 2010
Location:
Posted: 29th Oct 2010 01:01
yes i got it to work! thanks alot!

onhcet d(-_-)b techno
Grog Grueslayer
Valued Member
21
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 29th Oct 2010 07:16 Edited at: 29th Oct 2010 07:17
You also shouldn't use # by your variables. It's makes it as floats (a number with a decimal) screen coordinates are always integers (no decimal).

MMM
17
Years of Service
User Offline
Joined: 21st Jan 2009
Location: NSW, Australia
Posted: 29th Oct 2010 10:30
@Grog:

With something as important as character movement, floats aren't always a bad idea for screen coordinates. You can use ceil() and floor() to round either up or down to the nearest integer, thereby getting a more precise coordinate. Just one pixel can be very important when, say, testing collision against two or more sprites.

I only realised this the other day.

Powerboard Productions - find the site yourself.
TheComet
18
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 29th Oct 2010 13:42
@ Grog

Floats are important if you move diagonally, especially on slow speeds. If my sprite moves at speed 2, and I move it 45° diagonally, it will actually only increment the x and y positions by 1.41. That gets rounded down to 1 if you use integers, thus you will be moving much slower when going diagonally...

TheComet

Grog Grueslayer
Valued Member
21
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 29th Oct 2010 21:43
If I move a sprite at a 45 degree angle and the sprite speed is 2 it'll increase by 2 pixels making it move exactly the same speed as it goes in horizontal and vertical at that speed. Using floats would slow it down vs integers. When the coordinates start at say 45.50,101.50 and you increase by only a fraction the sprite wouldn't actually move if the resulting coordinates were 45.75,101,75.

Lets have an integer vs float race and see who wins.



Login to post a reply

Server time is: 2026-07-22 04:38:02
Your offset time is: 2026-07-22 04:38:02