Author Topic: Ways to limit player sprite movement.  (Read 14821 times)

Offline kanonet

  • Administrator
  • Prof. Inline
  • *******
  • Posts: 1142
    • View Profile
    • My GLBasic code archiv
Re: Ways to limit player sprite movement.
« Reply #30 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
Lenovo Thinkpad T430u: Intel i5-3317U, 8GB DDR3, NVidia GeForce 620M, Micron RealSSD C400 @Win7 x64

Offline Lord_TNK

  • Mr. Drawsprite
  • **
  • Posts: 54
    • View Profile
Re: Ways to limit player sprite movement.
« Reply #31 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).

Offline kanonet

  • Administrator
  • Prof. Inline
  • *******
  • Posts: 1142
    • View Profile
    • My GLBasic code archiv
Re: Ways to limit player sprite movement.
« Reply #32 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
Lenovo Thinkpad T430u: Intel i5-3317U, 8GB DDR3, NVidia GeForce 620M, Micron RealSSD C400 @Win7 x64

Offline Lord_TNK

  • Mr. Drawsprite
  • **
  • Posts: 54
    • View Profile
Re: Ways to limit player sprite movement.
« Reply #33 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.

Offline kanonet

  • Administrator
  • Prof. Inline
  • *******
  • Posts: 1142
    • View Profile
    • My GLBasic code archiv
Re: Ways to limit player sprite movement.
« Reply #34 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.
Lenovo Thinkpad T430u: Intel i5-3317U, 8GB DDR3, NVidia GeForce 620M, Micron RealSSD C400 @Win7 x64

Offline Lord_TNK

  • Mr. Drawsprite
  • **
  • Posts: 54
    • View Profile
Re: Ways to limit player sprite movement.
« Reply #35 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.

Offline kanonet

  • Administrator
  • Prof. Inline
  • *******
  • Posts: 1142
    • View Profile
    • My GLBasic code archiv
Re: Ways to limit player sprite movement.
« Reply #36 on: 2012-Dec-10 »
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

Offline Moru

  • Administrator
  • Prof. Inline
  • *******
  • Posts: 1766
    • View Profile
    • Homepage
Re: Ways to limit player sprite movement.
« Reply #37 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

Offline Lord_TNK

  • Mr. Drawsprite
  • **
  • Posts: 54
    • View Profile
Re: Ways to limit player sprite movement.
« Reply #38 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.

Offline kanonet

  • Administrator
  • Prof. Inline
  • *******
  • Posts: 1142
    • View Profile
    • My GLBasic code archiv
Re: Ways to limit player sprite movement.
« Reply #39 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.
Lenovo Thinkpad T430u: Intel i5-3317U, 8GB DDR3, NVidia GeForce 620M, Micron RealSSD C400 @Win7 x64

Offline Lord_TNK

  • Mr. Drawsprite
  • **
  • Posts: 54
    • View Profile
Re: Ways to limit player sprite movement.
« Reply #40 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.

Offline Moru

  • Administrator
  • Prof. Inline
  • *******
  • Posts: 1766
    • View Profile
    • Homepage
Re: Ways to limit player sprite movement.
« Reply #41 on: 2012-Dec-10 »
Keep moving until x or y has passed 16 pixels.

Offline Lord_TNK

  • Mr. Drawsprite
  • **
  • Posts: 54
    • View Profile
Re: Ways to limit player sprite movement.
« Reply #42 on: 2012-Dec-10 »
Keep moving until x or y has passed 16 pixels.
How do you put that in code?

Offline Moru

  • Administrator
  • Prof. Inline
  • *******
  • Posts: 1766
    • View Profile
    • Homepage
Re: Ways to limit player sprite movement.
« Reply #43 on: 2012-Dec-10 »
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.

Offline Slydog

  • Prof. Inline
  • *****
  • Posts: 929
  • KodeSource
    • View Profile
    • KodeSource
Re: Ways to limit player sprite movement.
« Reply #44 on: 2012-Dec-10 »
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]