P
PointBlankX79
Guest
Hello everyone, I'm mostly new to coding and completely new to GameMaker, so I want to stress immediately that I'm not attempting to get anyone to build my game for me or anything like that. This is my first thread, and I want to (in some minor way) make something useful for other beginners like me.
Below is the player movement code for my first project, which is best described as a side-scrolling shooter... just I want to use something other than shooting. It's meant to reflect inertia with arcadey feeling movement at 60 fps.
Create Event
Step Event
The only potential problem I've found with this method so far is that the momentum system allows the player instance to go slightly outside of the room's boundaries, but I personally kind of like the way it manifests in-game for my project. The primary reason I'm sharing this is selfish in that I am asking if anyone else sees any flaws or obvious optimizations with the code. The secondary reason for this is altruistic in that I consider it worthy of sharing to other novices who might need guidance on their own 2D movement code (by the way, this is not meant to work for platformers at all).
Otherwise, if anyone wants to share anything else that could help improve the code in other ways, that would be very helpful to me and any potential lurkers out there (how could this code be modified by physics? how easy would it be to expand this system with more movement mechanics? is the system just a trash fire? etc). I built this from a cross stitching of tutorials and my own *adjusts glasses* genius, but I'm already pretty happy with the code as it is. I just thought I'd see if there was something in the later GameMaker tutorials or GML overview that I missed (I'll get to the rest soon, I promise) or if any of you had even better code and wanted to share.
Thanks to anyone that responds with anything other than rudeness, and I apologize if I'm irritating anyone here
Below is the player movement code for my first project, which is best described as a side-scrolling shooter... just I want to use something other than shooting. It's meant to reflect inertia with arcadey feeling movement at 60 fps.
diag = rate at which movement has to be multiplied to not have "diagonal" movement be faster than other movement
movespd = "movement speed" multiplier for quick adjustments
U, L, D, R moment = "Up, Left, Down, Right momentum" variables to impart inertia when moving (slightly different from friction, I think)
x, y dir = "horizontal 'x' or vertical 'y' direction" variables
movespd = "movement speed" multiplier for quick adjustments
U, L, D, R moment = "Up, Left, Down, Right momentum" variables to impart inertia when moving (slightly different from friction, I think)
x, y dir = "horizontal 'x' or vertical 'y' direction" variables
Create Event
Code:
///Variables
diag = 0.707
movespd = 1
Umoment = 0
Lmoment = 0
Dmoment = 0
Rmoment = 0
Code:
///Player Movement
//Input
xdir = keyboard_check(ord('D')) - keyboard_check(ord('A'));
ydir = keyboard_check(ord('S')) - keyboard_check(ord('W'));
//Restrictions
if x > room_width - (sprite_width / 2)
{
x = room_width - (sprite_width / 2)
}
if x < sprite_width / 2
{
x = sprite_width / 2
}
if y > room_height - (sprite_height / 2)
{
y = room_height - (sprite_height / 2)
}
if y < sprite_height / 2
{
y = sprite_height / 2
}
//Momentum
if ydir == -1
{
Umoment += 1
}
else
{
Umoment -= 1
}
if Umoment > 12
{
Umoment = 12
}
if Umoment < 0
{
Umoment = 0
}
if xdir == -1
{
Lmoment += 1
}
else
{
Lmoment -=1
}
if Lmoment > 12
{
Lmoment = 12
}
if Lmoment < 0
{
Lmoment = 0
}
if ydir == 1
{
Dmoment += 1
}
else
{
Dmoment -=1
}
if Dmoment > 12
{
Dmoment = 12
}
if Dmoment < 0
{
Dmoment = 0
}
if xdir == 1
{
Rmoment += 1
}
else
{
Rmoment -= 1
}
if Rmoment > 12
{
Rmoment = 12
}
if Rmoment < 0
{
Rmoment = 0
}
//Final Calculation
if xdir != 0 && ydir != 0
{
x += (Rmoment - Lmoment) * movespd * diag;
y += (Dmoment - Umoment) * movespd * diag;
}
else
{
x += (Rmoment - Lmoment) * movespd;
y += (Dmoment - Umoment) * movespd;
}
Otherwise, if anyone wants to share anything else that could help improve the code in other ways, that would be very helpful to me and any potential lurkers out there (how could this code be modified by physics? how easy would it be to expand this system with more movement mechanics? is the system just a trash fire? etc). I built this from a cross stitching of tutorials and my own *adjusts glasses* genius, but I'm already pretty happy with the code as it is. I just thought I'd see if there was something in the later GameMaker tutorials or GML overview that I missed (I'll get to the rest soon, I promise) or if any of you had even better code and wanted to share.
Thanks to anyone that responds with anything other than rudeness, and I apologize if I'm irritating anyone here