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 AppGameKit Corner / 4 Directional Movement (sort of)

Author
Message
DaRealWAZY
7
Years of Service
User Offline
Joined: 26th Sep 2017
Location:
Posted: 22nd Oct 2017 03:07
Hello guys,

Just decided to mess around with AppGameKit as I'm creating a prototype game similar to pokemon as I loved those games may or may not actually make a game from the prototype but thought it would be cool to have the systems that make a pokemon game in the prototype I will talk more about the actual pokemon clone in the future.

So I figured that movement would probably be the easiest but I did not want 8 directional movement (which is easier to make than 4 directional movement). This code works but is not great and I would like help to improve it



In this code I used the platform demo in the appgamekit small games thread as it had a structure that I liked so I did rip off a lot of the code

Bugs in this code
I noticed when you move the character in 2 directions it would move in that diagonal direction for like one loop (aka it would move 2 pixels to the side and 2 pixels up or down)

P.S I am new to AppGameKit and I just decided to use the code from the platform demo as the basis, I have had some experiance in a similar language called Basic4GL but AppGameKit seems pretty good I may convert a few small programs I made in B4GL to AppGameKit also it says I have plus 200 hours on AppGameKit on steam but that is a lie as I open it up on steam to do something and then leave it running while I do other things like play game or watch youtube
Cybermind
Valued Member
21
Years of Service
User Offline
Joined: 28th Nov 2002
Location: Denmark
Posted: 22nd Oct 2017 09:54
Here is a simple way of preventing diagonal movement. You could make it more advanced, so that it would move only in the direction of the first button pressed even though other buttons are being pressed, or only in the direction of the last button pressed.

13/0
Cybermind
Valued Member
21
Years of Service
User Offline
Joined: 28th Nov 2002
Location: Denmark
Posted: 22nd Oct 2017 10:48
The first example will stop player entirely if any other direction is pressed. This example will make the player continue in the direction of the first button pressed even though other directions are pressed after.

13/0
Cybermind
Valued Member
21
Years of Service
User Offline
Joined: 28th Nov 2002
Location: Denmark
Posted: 22nd Oct 2017 17:59 Edited at: 22nd Oct 2017 18:05
This example will make red player square move in the last pressed direction even though other directions are still held down.

13/0
Cybermind
Valued Member
21
Years of Service
User Offline
Joined: 28th Nov 2002
Location: Denmark
Posted: 22nd Oct 2017 18:04 Edited at: 22nd Oct 2017 18:07
I have added a globals part to the declare section btw.

EDIT: In the two last examples, I have removed the StopMoving() call.
13/0
DaRealWAZY
7
Years of Service
User Offline
Joined: 26th Sep 2017
Location:
Posted: 22nd Oct 2017 21:23 Edited at: 23rd Oct 2017 10:54
So Cybermind really thank full for all your examples I decided I personally like the first button pressed and I get why the need for last button pressed version would be used but I do prefer the first button used.

So I decided to start work on my project however since the start of my project was just the 4 directional movement here is a modular style of code implemented for use in a bigger project with Cybermind's first button pressed system.

here is main.agc


here is initialise.agc


here is playerMovement.agc
Cybermind
Valued Member
21
Years of Service
User Offline
Joined: 28th Nov 2002
Location: Denmark
Posted: 23rd Oct 2017 09:40
I am glad you could use it I have a question though, at the top of initialise.agc and playerMovement.agc, does stuff like "global firstDirection as integer" get initialized when they are not part of a subroutine or function that is called from the main.agc?
13/0
DaRealWAZY
7
Years of Service
User Offline
Joined: 26th Sep 2017
Location:
Posted: 23rd Oct 2017 10:53
Quote: "I am glad you could use it I have a question though, at the top of initialise.agc and playerMovement.agc, does stuff like "global firstDirection as integer" get initialized when they are not part of a subroutine or function that is called from the main.agc?"


That actually fixed a bug for me as the code for player position were out of the createPlayer () function the player kept being made in the top left corner of the screen instead of at the co-ordinates that I set so I fixed that.

As for global calls to variables if AppGameKit basic is like any other programming language I could be wrong but as long as the global variable has been declared some where in the code it will be initialized as the lines with #include sourcename.agc will on compile if I am correct will create all variables through out the code. Also the floppy bird source found on a thread of small game projects for AppGameKit 2 actually declares global variables for each code file to make it easier to know where the variable actually goes. I used that code to create my project with multiple source files but couldn't work out why my player was not going to the co-ordinates so I thank you for your help

in my previous post playerMovement.agc source file has been updated with the correct code now.
Supertino
7
Years of Service
User Offline
Joined: 22nd Jun 2017
Location: Behind you!
Posted: 23rd Oct 2017 11:08
I'd just use an elseif in playerMovement() function



it wont stop movement if more than 1 direction key is pressed but will prevent diagonals.
Cybermind
Valued Member
21
Years of Service
User Offline
Joined: 28th Nov 2002
Location: Denmark
Posted: 23rd Oct 2017 12:02
@Supertino - Yes, but that will produce a control system that prefers left key, then right key and so on. If you hold down left key all the other keys won't be accepted, if you hold down right key it will still accept left key but not the ones after. So when holding down the down key, all the others will be accepted whereas when doing the same with left key no other keys will be accepted.
13/0
29 games
18
Years of Service
User Offline
Joined: 23rd Nov 2005
Location: not entirely sure
Posted: 26th Oct 2017 20:12
Here's another way using a bit of maths and one if statement:


Hover Car Race Challenge! - available now on Google Play
Invaders of the 29th Dimension - available now on Google Play
Find me on indieDB
Cybermind
Valued Member
21
Years of Service
User Offline
Joined: 28th Nov 2002
Location: Denmark
Posted: 27th Oct 2017 11:26
@29 Games - You could make it like that, but that will stop the player from moving at all when they press multiple buttons at once. In the heat of battle, it might be annoying to be stopped because you frantically press multiple buttons.
13/0
PartTimeCoder
AGK Tool Maker
9
Years of Service
User Offline
Joined: 9th Mar 2015
Location: London UK
Posted: 28th Oct 2017 17:21 Edited at: 30th Oct 2017 00:28
Quote: "but that will stop the player from moving at all when they press multiple buttons at once."


Here's what I would do

Cybermind
Valued Member
21
Years of Service
User Offline
Joined: 28th Nov 2002
Location: Denmark
Posted: 29th Oct 2017 08:19
@PartTimeCoder - If you hold down left key, secondly hold down up key, lastly release left key, it will stop moving even though you are still holding down up key.
13/0
29 games
18
Years of Service
User Offline
Joined: 23rd Nov 2005
Location: not entirely sure
Posted: 29th Oct 2017 20:37
Judging by the original code DaRealWAZY gave, I think the idea is that if more than one directional button is pressed then all movement stops.

But you are right, for an action game this is clunky but I wouldn't use it for an action game. It wouldn't be suitable for something like Geometry wars for instance. I've just had a go with the code you posted and, yes, yours is a lot more fluid.
Hover Car Race Challenge! - available now on Google Play
Invaders of the 29th Dimension - available now on Google Play
Find me on indieDB
PartTimeCoder
AGK Tool Maker
9
Years of Service
User Offline
Joined: 9th Mar 2015
Location: London UK
Posted: 30th Oct 2017 00:27
@Cybermind, changing GetRawKeyPressed to GetRawKeyState solves that issue, I will update my post.
Cybermind
Valued Member
21
Years of Service
User Offline
Joined: 28th Nov 2002
Location: Denmark
Posted: 30th Oct 2017 11:26 Edited at: 30th Oct 2017 11:26
@PartTimeCoder, cool, now your code is slicker than mine
13/0

Login to post a reply

Server time is: 2024-11-22 20:45:00
Your offset time is: 2024-11-22 20:45:00