Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Newcomers DBPro Corner / Step by step movement

Author
Message
Zwarteziel
13
Years of Service
User Offline
Joined: 22nd Jan 2011
Location: Netherlands
Posted: 2nd Mar 2012 14:06
Dear all,

I'm trying to write a program in which a player moves through a 3D-world step by step. So: if a user presses the [up-arrow]-key, he or she moves a specific amount of coordinates forward and then stops.

Now, I can make a camera move though a grid non-stop while a key is pressed, but I'm having difficulties getting the step-by-step part right. Can any of you help me in finding a solution?

Here's what I have now:



I've tried giving every function a for-next loop that looks like this...



...but the result a). moves very fast and b). still lets the user move non-stop while the key is pressed.
Quisco DaLuse
17
Years of Service
User Offline
Joined: 3rd Feb 2007
Location: USA
Posted: 2nd Mar 2012 18:47
One way you could do it is to add a variable to track whether or not one of the keys has been pressed, like this:



I didn't realize there was a MOVE CAMERA LEFT (or RIGHT, for that matter) command, but it does appear to work okay.

It might be more efficient to store the values of the different keystate checks and then look at the values instead of checking them twice (e.g. k17 = keystate(17) : k32 = keystate(32) )
Zwarteziel
13
Years of Service
User Offline
Joined: 22nd Jan 2011
Location: Netherlands
Posted: 3rd Mar 2012 13:59
Thank you very much for your reply Quisco. I'll try your suggesion once I get home and let you know if it works. As for the storing of keystate checks.. do you mean I should put the statues (1=pressed, 0=not pressed) in seperate variables and then check them? e.g: "If K17=0 then..."?
Zwarteziel
13
Years of Service
User Offline
Joined: 22nd Jan 2011
Location: Netherlands
Posted: 4th Mar 2012 00:46
Got the function working they way I wanted to, using a 'timeout' during which keypresses aren't registered. Here's the code, should fellow forum members ever want to use it:


Quisco DaLuse
17
Years of Service
User Offline
Joined: 3rd Feb 2007
Location: USA
Posted: 4th Mar 2012 21:55 Edited at: 4th Mar 2012 21:56
So, if I understand your function correctly, you are checking 10 times to see if the value returned by timer() is greater than the value held in MoveAllowed. It would be more efficient to check this once and move the camera accordingly.

Also, why don't you just move the camera (after checking the keystates) instead of branching to a function? Or maybe branch to a function that checks all of the keystates (ie. player movement) and reacts accordingly?

A limited example regarding storing the values of the keystates:


Zwarteziel
13
Years of Service
User Offline
Joined: 22nd Jan 2011
Location: Netherlands
Posted: 6th Mar 2012 20:26
Hi Quisco, I just realized you've left another reply after my last post. Your help is much appreciated as I realize my own methods are still quite unsophisticated. I'll get to work on trying out suggestions!
Zwarteziel
13
Years of Service
User Offline
Joined: 22nd Jan 2011
Location: Netherlands
Posted: 6th Mar 2012 22:28 Edited at: 6th Mar 2012 22:31
Right... So, I've been trying to implement the example you showed. It almost works. Almost

Here's what I have:

- The keystates are being stored in variables
- The program checks whether a key is pressed and stores this state as 1 or 0
- The program uses less loops to accomplish this all

Which boils down to this:



For some reason though, the step-by-step part isn't functioning anymore. I've spend a lot of time going over your examples and trying stuff.. but to no avail. I think I'm missing something quite small, but also quite essential
MrValentine
AGK Backer
13
Years of Service
User Offline
Joined: 5th Dec 2010
Playing: FFVII
Posted: 7th Mar 2012 16:00
tagging in folks



Zwarteziel
13
Years of Service
User Offline
Joined: 22nd Jan 2011
Location: Netherlands
Posted: 8th Mar 2012 09:57 Edited at: 8th Mar 2012 12:45
I could spend little time on my problem yesterday, but I ran into an obscure issue... The program doesn't seem to properly 'recognize' the if-then status of the variable 'key'. As far as I can see, it should run like this:

- If key = 0, a loop is initiated
- When, in this loop, a key is pressed, a function gets called and key gets a [value].
- If no key is pressed, key is reset to 0, allowing the loop to run once more.

If one uses the keyboard while key<>0, the loop should not be ran, and thus the movement-function not called. For some reason it seems it does run however. Any thoughts are greatly appreciated.
Quisco DaLuse
17
Years of Service
User Offline
Joined: 3rd Feb 2007
Location: USA
Posted: 8th Mar 2012 19:42 Edited at: 8th Mar 2012 19:44
Sorry it has taken me so long to get back - I keep getting mandated to work extra every day - uggh.

Anyway, the answer is in the checks you make to reset the value of key to 0. You have to base it upon the current keypress (i.e. the value of key). For example, if I press the forward key, and don't press any of the left, right or down keys, it will reset the value of key to 0. If you are still pressing the forward key, it will keep moving forward. Plug this in place of the other:



You don't have to write in '= 1' to all of those like I did, I simply did that to make sure that wasn't the problem.

Also, a minor note, you check keystate(72) twice.

Hope this helps.
Zwarteziel
13
Years of Service
User Offline
Joined: 22nd Jan 2011
Location: Netherlands
Posted: 8th Mar 2012 21:06
Hi Quisco,

thank you for replying once again! Don't worry about not getting back sooner: I sometimes have to deal with the same situation at work and thus truly appreciate your effort.

Your code did the trick...! I feel a bit embarrassed that I didn't see the solution, to be honest Ah well... I'll go grab a beer and drink it to your health. Here's to you!

Login to post a reply

Server time is: 2024-11-23 20:30:31
Your offset time is: 2024-11-23 20:30:31