Ways to limit player sprite movement.

Previous topic - Next topic

Lord_TNK

I know about changing the speed. I did include a dash button in the program (where it mentions KEY (57) in the code). The point was to have the sprite move a certain amount per press, sort of how Mario moves on the overworld maps in Super Mario Bros 3, and other games.

But I forgot about the showcase page on the main site. I'll see if I can find a game that did that, and look in its code. I will then report my findings if I come across how to do it.

Moru

Ah, misunderstood your "held down" as in speed going down while holding down the key. If you want it to slow down after release, you need to keep a counter or timer that starts after release of key. When the counter reaches 0, you disable the release variable again and stop moving.

One help for the release-event is here:
http://www.glbasic.com/forum/index.php?topic=679.0

It also has named keys, just like you wanted (sort of) :-)

MrTAToad

I would avoid using a keyboard control system and instead either use mouse or joystick.  This would avoid previously mentioned problems!

Moru

Not everything can be played only with mouse or controller. Besides, this can be easily adapted to use the same press/hold/release codes for joystick and mouse.

And the mouse is killing my shoulder and wrist... :-)

Lord_TNK

I plan on allowing custom buttons for the players in my games, so the codes need to work with keyboards as well as mice and gamepads.

Also, that code you linked to, Moru, looks like it could work. I'll see if I can incorporate that.

Slydog

#20
What somebody needs to do is create a generic 'Input()' TYPE that handles all the low level details for you, and allows you to query the input with input device type independency.

It would be great also for allowing the player to customize his input keys / buttons.

For example, the new TYPE would allow code such as the following:
Code (glbasic) Select
// Register input codes and events - Call before starting level
Input.Clear() // Clear any previous event entries
Input.AddKey("FIRE", 62) // Register key code '62' to trigger a 'fire' event
Input.AddMouseButton("FIRE", 0) // Register mouse button '0' to also trigger a 'fire' event
Input.AddMouseAxis("ROTATE", 0) // Register mouse axis '0' to control the 'rotation'

// In your main level game loop . . .
Input.Update() // This may be needed at the beginning of the game loop to set up the input states, unless you code it otherwise
IF Input.Event("FIRE") THEN . . . // Has the user triggered a 'fire' event? (by either clicking key 62 *or* mouse button '0')
playerRotation = Input.GetAxis("ROTATE") // Set the variable to the current 'ROTATE' axis value


[Edit] You could replace the event strings (ie: "FIRE") with event integer constants, if string code speed concerns you:
Code (glbasic) Select
CONSTANT InputFire% = 1
Input.AddKey(InputFire, 62) // Register key code '62' to trigger a 'fire' event
IF Input.Event(InputFire) THEN . . . // Has the user triggered a fire event?
My current project (WIP) :: TwistedMaze <<  [Updated: 2015-11-25]

kanonet

Slydog this is exactly what Gernots keydef and my improved version does.
Lenovo Thinkpad T430u: Intel i5-3317U, 8GB DDR3, NVidia GeForce 620M, Micron RealSSD C400 @Win7 x64

Slydog

@kanonet
Ha, good thing I didn't go ahead and do this myself then! :P
I've never heard of 'keydef' before (I don't think).  It isn't a GLBasic keyword.

I suppose it would be great to extend this idea to include key states somehow:
Code (glbasic) Select
IF Input.Event("FIRE") . . . // The event has started and finished (ie, key has been pressed down AND released this frame)
IF Input.EventOn("FIRE") . . . // Called when the user starts the event (ie. key is currently being pressed but not released)



My current project (WIP) :: TwistedMaze <<  [Updated: 2015-11-25]

Slydog

Is this what you are referring to?:
http://www.glbasic.com/forum/index.php?topic=8759.msg74517#msg74517

So what it does is allow you to store your desired input keys for certain events into an .ini file, and reread them when needed? 

That is close to what I outlined, but with fewer options. (ie: no mouse integration, events with multi keys / button definitions, axises, etc).  Should be good for most people though.  Just not my 'dream' input class! ha

In my Input class (ha, ok, TYPE) I have these predefined key settings:
Code (glbasic) Select
CONSTANT KEY_MINUS = 12
CONSTANT KEY_PLUS = 13
CONSTANT KEY_TAB = 15
CONSTANT KEY_CAPSLOCK = 17
CONSTANT KEY_ENTER = 28
CONSTANT KEY_SPACE = 57
CONSTANT KEY_COMMA = 51
CONSTANT KEY_PERIOD = 52

CONSTANT KEY_UP = 200
CONSTANT KEY_DOWN = 208
CONSTANT KEY_LEFT = 203
CONSTANT KEY_RIGHT = 205
CONSTANT KEY_PAGEUP = 209
CONSTANT KEY_PAGEDOWN = 201
CONSTANT KEY_INSERT = 210
CONSTANT KEY_DELETE = 211
CONSTANT KEY_HOME = 199
CONSTANT KEY_END = 207

CONSTANT KEY_SHIFT_L = 42
CONSTANT KEY_SHIFT_R = 54
CONSTANT KEY_CONTROL_L = 31

CONSTANT MOUSE_AXIS_X% = 0
CONSTANT MOUSE_AXIS_Y% = 1
CONSTANT MOUSE_WHEEL% = 2
CONSTANT MOUSE_BUTTON_1% = 3
CONSTANT MOUSE_BUTTON_2% = 4
CONSTANT MOUSE_BUTTON_3% = 5


But according to Moru's post on page 3 of that thread, these may not always be accurate, depending on which keyboard type you have?  Hmm, but it may be a good starting point . . . or have them stored into a few ini files, and detect which keyboard you have, and load the appropriate keyboard.ini file? ha, no idea if that is even possible, or desired.
My current project (WIP) :: TwistedMaze <<  [Updated: 2015-11-25]

kanonet

Slydog, Gernots keydef is in sampes/common/keydef (also see samples/_projects_/keydefmenu).
My improved version is here: http://www.glbasic.com/forum/index.php?topic=5695.

I dont want to push someone, its just cause you asked. :D
Lenovo Thinkpad T430u: Intel i5-3317U, 8GB DDR3, NVidia GeForce 620M, Micron RealSSD C400 @Win7 x64

Slydog

#25
Wow, pretty much exactly what I was proposing!   :enc:

I like your method of having each 'event' it's own instance of the TYPE, so code reads better:
Code (glbasic) Select
IF attack.DOWN() THEN DOSOMETHING()
Plus of course your automatic setting of the key/mouse in the while look example - very cool!

If you wanted to extend (ha, I didn't look at your code, it may already have this) your TYPE, you could add methods to detect 'Down', 'Up', 'Press' (Up then Down) or whatever.
My current project (WIP) :: TwistedMaze <<  [Updated: 2015-11-25]

kanonet

It already has 4 states:
-not pressed
-hit/ just pressed down
-pressed/ holding down
-released
Just read key.state if you need this information.
Lenovo Thinkpad T430u: Intel i5-3317U, 8GB DDR3, NVidia GeForce 620M, Micron RealSSD C400 @Win7 x64

Moru

Ah, there it was. I searched through the first 14 pages of code snippets yesterday since the search would not find it. Thanks :)
Should really sort up those in more categories or it's own site entirely.
There has been a few attempts at a wiki but it's hard to get it accepted and used.

MrTAToad

Yes, unfortunately - it's a shame

Lord_TNK

Quote from: Moru on 2012-Dec-06
Ah, misunderstood your "held down" as in speed going down while holding down the key. If you want it to slow down after release, you need to keep a counter or timer that starts after release of key. When the counter reaches 0, you disable the release variable again and stop moving.

One help for the release-event is here:
http://www.glbasic.com/forum/index.php?topic=679.0
The problem is the sample code shows errors when I try to compile. I'd bring that up on the thread, but it's an old one.

Quote from: kanonet on 2012-Dec-06
Slydog, Gernots keydef is in sampes/common/keydef (also see samples/_projects_/keydefmenu).
My improved version is here: http://www.glbasic.com/forum/index.php?topic=5695.

I dont want to push someone, its just cause you asked. :D
How would I incorporate that into my program? Here's an updated source code, by the way.

Code (glbasic) Select
SETCURRENTDIR("Media") // go to media files

SETTRANSPARENCY RGB (244, 124, 0) // I used that color because it's not a multiple of 8, and that's how I like to use the colors in the stuff I make.

SETSCREEN 640, 480, 0

LIMITFPS 60

LOADSPRITE "tnk shield.png", 5 //Any sprite will do, but it's best if it's 32x32.
GLOBAL shield% = 5

//Starting positions for the sprite. Some programs call these "px and py, but I prefer something more descriptive.
GLOBAL spritex = 288
GLOBAL spritey = 208
GLOBAL spritespeed = 1

WHILE KEY (1) = FALSE

MovePlayer()
DrawScreen()

WEND

FUNCTION MovePlayer:

LOCAL dirx, diry

        //Maps movement to the WASD keys. Adjustable of course.
IF KEY (17)
INC diry, -1
ENDIF

IF KEY (30)
INC dirx, -1
ENDIF

IF KEY (31)
INC diry, 1
ENDIF

IF KEY (32)
INC dirx, 1
ENDIF

        //So it knows to update the position of the sprite.
spritex = spritex + (spritespeed * dirx)
spritey = spritey + (spritespeed * diry)

        //Makes this essentially a dash button. Second line is so it knows to go to normal speed when not held.
IF KEY (57) THEN spritespeed=3
IF NOT KEY (57) THEN spritespeed=1

        //Halts sprite when at edges of screen.
IF spritex < 0 THEN spritex = 0
IF spritex > 608 THEN spritex = 608
IF spritey < 0 THEN spritey = 0
IF spritey > 448 THEN spritey = 448

ENDFUNCTION

FUNCTION DrawScreen:

DRAWSPRITE shield, spritex, spritey

SHOWSCREEN

ENDFUNCTION