Just starting out? Need help? Post your questions and find answers here.
PowerSoft
User
Posts: 65 Joined: Sun Aug 16, 2015 2:54 pm
Post
by PowerSoft » Mon May 15, 2017 4:10 pm
I wont to change the variable FacingAzimuth with the left- or right key.
The code is working but when I tough the key for a short moment PB is so fast the value is change to much.
How can I change the the code in the way that the value is increment or decrement with one independent of how long I
push the button.
Any help will be great
Code: Select all
ExamineKeyboard()
If KeyboardReleased(#PB_Key_Escape)
Exit = #True
EndIf
If KeyboardPushed(#PB_Key_Left)
;Debug "#PB_Key_Left"
FacingAzimuth=FacingAzimuth-1
If FacingAzimuth < 0
FacingAzimuth = 0
EndIf
EndIf
If KeyboardPushed(#PB_Key_Right)
;Debug "#PB_Key_Right"
FacingAzimuth=FacingAzimuth+1
If FacingAzimuth > 360
FacingAzimuth = 360
EndIf
EndIf
OS X 10.10.5 PB 5.31(x64)
netmaestro
PureBasic Bullfrog
Posts: 8451 Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada
Post
by netmaestro » Mon May 15, 2017 4:25 pm
Try a Delay(<some value>) after If KeyboardPushed... and before FacingAzimuth + 1
A delay of 30 or so might do, but try values until you hit one you like.
BERESHEIT
kenmo
Addict
Posts: 2046 Joined: Tue Dec 23, 2003 3:54 am
Post
by kenmo » Mon May 15, 2017 4:40 pm
Use something like:
Code: Select all
LeftPrev = LeftNow
LeftNow = KeyboardPushed(#PB_Key_Left)
If (LeftNow and (Not LeftPrev))
;Debug "#PB_Key_Left"
FacingAzimuth=FacingAzimuth-1
If FacingAzimuth < 0
FacingAzimuth = 0
EndIf
EndIf
kenmo
Addict
Posts: 2046 Joined: Tue Dec 23, 2003 3:54 am
Post
by kenmo » Mon May 15, 2017 4:43 pm
If you want the angle to continue changing while key is held, try something like:
Code: Select all
If KeyboardPushed(#PB_Key_Left)
If (LeftCount = 0)
;Debug "#PB_Key_Left"
FacingAzimuth=FacingAzimuth-1
If FacingAzimuth < 0
FacingAzimuth = 0
EndIf
EndIf
LeftCount = (LeftCount + 1) % 10
Else
LeftCount = 0
EndIf
Or using floats:
Code: Select all
If KeyboardPushed(#PB_Key_Left)
;Debug "#PB_Key_Left"
FacingAzimuth.f=FacingAzimuth-0.1
If FacingAzimuth < 0
FacingAzimuth = 0
EndIf
EndIf