Ways to limit player sprite movement.

Previous topic - Next topic

kanonet

Short answer:

lets say your code looks like this:
Code (glbasic) Select
// inside main loop:
IF KEY(32) THEN INC dirx

Then you would replace it with this:
Code (glbasic) Select
// on game start, before the main loop
LOCAL backward AS Tkey
backward.SET(32)

// inside your main loop:
IF backward.DOWN() THEN INC dirx
Lenovo Thinkpad T430u: Intel i5-3317U, 8GB DDR3, NVidia GeForce 620M, Micron RealSSD C400 @Win7 x64

Lord_TNK

Quote from: kanonet on 2012-Dec-09
Short answer:

lets say your code looks like this:
Code (glbasic) Select
// inside main loop:
IF KEY(32) THEN INC dirx

Then you would replace it with this:
Code (glbasic) Select
// on game start, before the main loop
LOCAL backward AS Tkey
backward.SET(32)

// inside your main loop:
IF backward.DOWN() THEN INC dirx

Would that be all, or would I need to do more?

I just tried so many things, and nothing seems to have any effect on the constant movement of the sprite (as in any way to pause or halt it, not the speed).

kanonet

This is just to show you how you replace one key with my routine, wasnt that your question? Of cause you would need to do more for the other keys.

And your changing of the movement speed is independent from this, can be done with it and without, just in the way you like it. Did you have a look at this one?: http://www.glbasic.com/forum/index.php?topic=5297.msg41251#msg41251
Lenovo Thinkpad T430u: Intel i5-3317U, 8GB DDR3, NVidia GeForce 620M, Micron RealSSD C400 @Win7 x64

Lord_TNK

Quote from: kanonet on 2012-Dec-09
This is just to show you how you replace one key with my routine, wasnt that your question? Of cause you would need to do more for the other keys.

And your changing of the movement speed is independent from this, can be done with it and without, just in the way you like it. Did you have a look at this one?: http://www.glbasic.com/forum/index.php?topic=5297.msg41251#msg41251
1. What I meant was if I could just use that code for each key and then just run it, or if I needed to put any more codes in there.

2. I wrote "not movement speed". I had already figured that one out. I did point out a dash button in the code after all. I meant halting the movement, even temporarily.

kanonet

1. Its the code thats needed for one key. If you want to replace multiple keys, you need to do this for each key.

2. Im not sure if I understand what you mean, but if you want to stop movement, just set spritespeed to 0 or set dirx and diry to 0.
Lenovo Thinkpad T430u: Intel i5-3317U, 8GB DDR3, NVidia GeForce 620M, Micron RealSSD C400 @Win7 x64

Lord_TNK

Quote from: kanonet on 2012-Dec-09
1. Its the code thats needed for one key. If you want to replace multiple keys, you need to do this for each key.

2. Im not sure if I understand what you mean, but if you want to stop movement, just set spritespeed to 0 or set dirx and diry to 0.
1. I meant if there was anything to add other than the LOCAL declaration and the IF adjustments. If that's it, I'll try it out today.

2. I meant moving the sprite one increment, and then either stopping or waiting a split second before moving anymore. If the code you gave can do that, awesome.

kanonet

1. You need the LOCAL and the SET at the beginning of the program for each key. And obviously you need the adjustment of the IF sentence for each key. Thats all.

2. Sorry i really dont know what you mean, i bet its really easy to do, but it seems, that my English skills are to low to help you here, sorry for that. Maybe someone else can do it.
Lenovo Thinkpad T430u: Intel i5-3317U, 8GB DDR3, NVidia GeForce 620M, Micron RealSSD C400 @Win7 x64

Moru

I think #2 is easing. A way of not moving full speed all the time but move at a lower speed, not one pixel each screen update. Faster computer = faster moving.

Try any of these:
http://www.glbasic.com/forum/index.php?topic=8530.0
http://www.glbasic.com/forum/index.php?topic=5892.msg46443#msg46443

Lord_TNK

Quote from: Moru on 2012-Dec-10
I think #2 is easing. A way of not moving full speed all the time but move at a lower speed, not one pixel each screen update. Faster computer = faster moving.

Try any of these:
http://www.glbasic.com/forum/index.php?topic=8530.0
http://www.glbasic.com/forum/index.php?topic=5892.msg46443#msg46443
I can just set the speed at a lower amount for that. The point is NOT the speed of the movement but causing the movement to not be constant.

kanonet

Maybe you can describe what you want to do in other words? Cause I have no idea what you want to do...
Anyway im out for tonight.
Lenovo Thinkpad T430u: Intel i5-3317U, 8GB DDR3, NVidia GeForce 620M, Micron RealSSD C400 @Win7 x64

Lord_TNK

Quote from: kanonet on 2012-Dec-10
Maybe you can describe what you want to do in other words? Cause I have no idea what you want to do...
Anyway im out for tonight.
Well if you run the code from the first post, or the code I later posted (just pick any picture for the sprite), you will see that the sprite moves smoothly and constantly as long as you press a directional button. I want a way to cause the sprite to move a certain amount, and then either stop moving altogether until the key is released, or wait a certain amount of time before moving again.

The real goal is a way to make the sprite move one "square" at a time, the way player characters in the first six Final Fantasy games moved.

Moru

Keep moving until x or y has passed 16 pixels.

Lord_TNK

Quote from: Moru on 2012-Dec-10
Keep moving until x or y has passed 16 pixels.
How do you put that in code?

Moru

This is where that highschool math comes in :-)

If your grid is 16 pixels you can use the command MOD(x,16).
This returns the reminder of the division x/16. When you are on even 16 pixel boundary, it will return 0 so you know when to set the speed to 0 after a keyboard release.

Untested pseudocode since I'm too long away from GLBasic...

Code (glbasic) Select

IF key_down("w")
speed_y = -1
ENDIF

IF key_released("w")
IF MOD(y,16) = 0
speed_y = 0
ENDIF
ENDIF

y = y + speed_y


repeat for each key ofcourse.

Slydog

My original code on the first pages attempts to move your player in a grid like fashion.
In my code, the grid was set at 32x32 - it will move 32 pixels each time you click left or right.
Then wait for another key to move another 32 pixels.

I coded it to use a tweening / easing library, but you could replace it with a simple linear ease, meaning it will move at the same speed for the entire 'trip'.

Here's my original post:
http://www.glbasic.com/forum/index.php?topic=8774.msg74593#msg74593
My current project (WIP) :: TwistedMaze <<  [Updated: 2015-11-25]