Moving Diagonally

Blitz3D Forums/Blitz3D Beginners Area/Moving Diagonally

Hello hello,

I was wondering how one would get a sprite to move diagonally. I tried doing a "If keydown(up) and keydown(right) then..." type thing, but that doesn't seem to work.

Right now it's just one large if statement determining what direction you're pressing and will later include a "fire" button as well as a "bomb" button.

While Not KeyDown(1)

If KeyDown(up) Then
	ship_y = ship_y - ship_speed
	DrawImage gfxship,ship_x,ship_y
ElseIf KeyDown(Left) Then
	ship_x = ship_x - ship_speed
	DrawImage gfxship,ship_x,ship_y
ElseIf KeyDown(Right) Then
	ship_x = ship_x + ship_speed
	DrawImage gfxship,ship_x,ship_y
ElseIf KeyDown(down) Then
	ship_y = ship_y + ship_speed
	DrawImage gfxship,ship_x,ship_y
Else
	DrawImage gfxship,ship_x,ship_y
EndIf

Flip
Cls

Wend


Thanks!

Greetings Puppies,

Try splitting the If statement above into two sections, one dealing with ship_y and one dealing with ship_x.

Peace,

Jes

Thanks! Works great and makes sense.

You can do away with all those DrawImage commands.
You only need to put it once before Flip.

It works also on that way:


Graphics3D 640,480

cam = CreateCamera()
MoveEntity cam,0,0,-5

sp = CreateSprite()
RotateSprite sp,20

While Not KeyDown(1)

If KeyDown(200) And KeyDown(205) Then
;this statement first, otherweise each of those two
; would be catched either by
; If KeyDown(200)... or
; If KeyDown(205)...
MoveEntity sp,.1,.1,0

ElseIf KeyDown(200) Then ; up
MoveEntity sp,0,.1,0
ElseIf KeyDown(203) Then ;left
MoveEntity sp,-.1,0,0


ElseIf KeyDown(205) Then ;right
MoveEntity sp,.1,0,0

ElseIf KeyDown(208) Then ;down
MoveEntity sp,0,-.1,0
EndIf

RenderWorld
Text 10,10, "UP : " +Str$(KeyDown(200))
Text 10,30, "RIGHT : " +Str$(KeyDown(205))
Text 10,50, "DOWN : " +Str$(KeyDown(208))
Text 10,70, "LEFT : " +Str$(KeyDown(203))


Flip

Wend
End