Ways to limit player sprite movement.

Previous topic - Next topic

Ian Price

Moru's code wouldn't work if the characters needed to move exactly 16pixels every time - if you remove your finger from the key before the 16th pixel kicks in then the player might be halfway through a 16x16 square or even 15/16th through it. I presume that the player HAS to move 16 pixels smoothly from start to finish after a key has been pressed.

Here's what I think you want -

Code (glbasic) Select

// --------------------------------- //
// Project: test
// Start: Monday, December 10, 2012
// IDE Version: 10.283

// Create a virtual screen
CREATESCREEN 1,999,16,16

USESCREEN 1

// Draw a rectangle to create a sprite
DRAWRECT 0,0,16,16,RGB(255,0,0)

USESCREEN -1

LOCAL press%, move%, dir%

LOCAL x=320, y=240

WHILE TRUE

// Up
IF KEY(200) AND dir=0
IF press=0
  move=16
  dir=1
ENDIF
press=20
ENDIF

// Down
IF KEY(208) AND dir=0
IF press=0
  move=16
  dir=3
ENDIF
press=20
ENDIF

// Left
IF KEY(203) AND dir=0
IF press=0
  move=16
  dir=4
ENDIF
press=20
ENDIF

// Right
IF KEY(205) AND dir=0
IF press=0
  move=16
  dir=2
ENDIF
press=20
ENDIF

// Move square
IF move>0
IF dir=1 THEN DEC y
IF dir=2 THEN INC x
IF dir=3 THEN INC y
IF dir=4 THEN DEC x
DEC move
ENDIF

IF move=0 THEN dir=0

PRINT "PRESS "+press,10,10

IF press>0 THEN DEC press

DRAWSPRITE 999,x,y

SHOWSCREEN

WEND


There are many ways to recreate this.
I came. I saw. I played.

Lord_TNK

Quote from: Ian Price on 2012-Dec-10
Moru's code wouldn't work if the characters needed to move exactly 16pixels every time - if you remove your finger from the key before the 16th pixel kicks in then the player might be halfway through a 16x16 square or even 15/16th through it. I presume that the player HAS to move 16 pixels smoothly from start to finish after a key has been pressed.

Here's what I think you want -

...

There are many ways to recreate this.
That is pretty close to what I want. I'll see about incorporating it into the code I'm using.

Now I don't know if you came up with that yourself, or just are pasting it from elsewhere, but thank you either way.  :happy:

Ian Price

Um, why wouldn't I have come up with that myself? It's not exactly rocket science. You ask for help and then wonder if it's pasted from somewhere else. Nice. That's not the best way to ask for help in the future...
I came. I saw. I played.

Darmakwolf

See my post for Blobo's Quest - that had tile-based movement where the character would move on a grid until the key is released. I imitated RPG Maker's movement system, basically.

Lord_TNK

Quote from: Ian Price on 2012-Dec-11
Um, why wouldn't I have come up with that myself? It's not exactly rocket science. You ask for help and then wonder if it's pasted from somewhere else. Nice. That's not the best way to ask for help in the future...
I'm a newbie, both to this site and to programming languages. I honestly am not at the point where thinking up such code comes naturally. From my perspective, that could likely be a commonly used type of program, that's listed somewhere I haven't visited.

I was just stating I didn't know the origin of that program. If you think I was thinking you plagiarized it, I would have stated so, and not have thanked you or given a happy cloud emoticon in my response.

Quote from: Darmakwolf on 2012-Dec-11
See my post for Blobo's Quest - that had tile-based movement where the character would move on a grid until the key is released. I imitated RPG Maker's movement system, basically.
Got a link to it? I can look for it, but I wouldn't know which section, or how many pages in to click.

erico

There you have it:
http://www.glbasic.com/forum/index.php?topic=8791.0

I´m still to give it a try too.

You might want to use the "Show unread posts since last visit." button when you get on this forum to see recent stuff.

Ian Price

QuoteIFrom my perspective, that could likely be a commonly used type of program, that's listed somewhere I haven't visited.
Did it really matter where it came from? You got a simple answer to a simple problem. Glad it was what you were looking for. :) Something similar probably is listed elsewhere, but that example code is my own, and is actually based on something I am working on at the moment.

The GLBasic forum is filled with beginners, intermediates and experts that will have individual solutions to any problem you can think of. Users have just got to ask for help to a problem in a way that other people understand!

Just because it doesn't come naturally yet, doesn't mean it won't over time. :)
I came. I saw. I played.

Lord_TNK

Quote from: Ian Price on 2012-Dec-11
QuoteIFrom my perspective, that could likely be a commonly used type of program, that's listed somewhere I haven't visited.
Did it really matter where it came from? (1) You got a simple answer to a simple problem. Glad it was what you were looking for. :) Something similar probably is listed elsewhere, but that example code is my own, and is actually based on something I am working on at the moment.

The GLBasic forum is filled with beginners, intermediates and experts that will have individual solutions to any problem you can think of. Users have just got to ask for help to a problem in a way that other people understand!

Just because it doesn't come naturally yet, doesn't mean it won't over time. :) (2)
1. I guess it didn't come across when I wrote it, but my comment about that was mainly simple curiosity.

2. That's the plan, and should definitely happen with help from all of you.  =D

Lord_TNK

#53
Okay, with Price's code, I actually managed to make it stop until I released the key.

I took this line:
Code (glbasic) Select
IF KEY(200) AND dir=0
IF press=0
  move=16
  dir=1
ENDIF
press=20
ENDIF


Tweaked it to:
Code (glbasic) Select
IF KEY(200) AND dir=0
IF press=0
  move=16
  dir=1
ELSE
  move=0
ENDIF
press=20
ENDIF


And did it for the other three directions. Now the square will not move until I release the button, and the counter goes to zero.

But for some reason, changing "press=20" to another number only works at 17 or higher. Lower numbers ignore the pause. I honestly wonder why that is. I'm still working on more of this though.

Ian Price

That's because the "press" variable is being decreased while the player is moving (in total 16 counts) hence the "press" count needs to be more than 16.

My code already makes the character stop until the key is released. You haven't actually improved or understood the code at all. :P
I came. I saw. I played.

Lord_TNK

#55
Quote from: Ian Price on 2012-Dec-14
That's because the "press" variable is being decreased while the player is moving (in total 16 counts) hence the "press" count needs to be more than 16. (1)

My code already makes the character stop until the key is released. You haven't actually improved or understood the code at all. (2)
1. That makes sense. I tested it out by changing 16 to 8 and 20 to 10, and it still worked.

2. I understand what a program is doing when compiled and run, and when I used the code on my end, the square moved constantly (but 16 at a time) without those tweaks, but paused with them. There might be some differences in how our compilers work.

EDIT: Okay, I tried it again with my tweaks commented out, and now it works properly anyway. Not sure if something changed on my system, or I just wasn't paying attention. Regardless, it's nice to know this can at least be done.

Darmakwolf

Quote from: erico on 2012-Dec-11
There you have it:
http://www.glbasic.com/forum/index.php?topic=8791.0

I´m still to give it a try too.

You might want to use the "Show unread posts since last visit." button when you get on this forum to see recent stuff.

oh - wrong game you linked there xD this is my more recent object-based platformer. I'll try to upload it again, think it got lost...

Darmakwolf

Okay - attached is my project I mentioned before. I think it does what you are looking to do! Feel free to take what you'd like from it :)

[attachment deleted by admin]

Lord_TNK

Quote from: Darmakwolf on 2012-Dec-18
Okay - attached is my project I mentioned before. I think it does what you are looking to do! Feel free to take what you'd like from it :)
It might be a couple days, but I'll download it and try it.

Darmakwolf

Okay please do! It's one of my... unusual projects... but I think it does what you want!