X
XirmiX
Guest
I've followed some tutorials in the past and checked back on one in order to create a basic platformer, as it seems I had forgotten the basics of how to make it all work over time, since I'm basically redoing my entire game (reason being that everything's just bunched up, so I just created a new file and I'll be porting anything I need from the old file to the new). This is not about my tank game, btw, for those who are familiar with that out there.
Long and short of it is that I am adding acceleration and deceleration to my movement and because of that, collision checking becomes quite tricky. I guess I could enable physics, but it might bring in unwanted control into the game later on, so I'm trying to keep that out of here. The tutorial I checked back on was the following:
Here's some important code:
Problems that I am having is that the object slows down sometimes when moving horizontally near a wall, sometimes gets stuck in a wall, can't be moved in the opposite direction whilst next to the wall (so, left if there's a wall on the right and vice versa), the object doesn't decelerate horizontally when right or left key is let go of (but does so when the other horizontal movement control key is pressed, so, if I'm moving to the right and I press left, the character will move to the right slower over time and once it reaches 0, it starts moving to the right, which is intended) and the object doesn't jump at all. Another thing that might be of note is that I've purposefully made it so that you cannot move the object left or right whilst in mid-air (at least that works nicely).
And, I think I should also mention, before anyone brings this up that yes, I do understand my own code for the most part and what each thing is supposed to do. What I don't understand is why it does those glitches when it's not supposed to by my understanding.
Long and short of it is that I am adding acceleration and deceleration to my movement and because of that, collision checking becomes quite tricky. I guess I could enable physics, but it might bring in unwanted control into the game later on, so I'm trying to keep that out of here. The tutorial I checked back on was the following:
Here's some important code:
Movable object's Create event
Movable object's Step event
Movable object's Alarm 0
Code:
///Initial variables
//Variables for movement
horizontal_speed = 0;
vertical_speed = 0;
//hspeed = horizontal_speed;
//vspeed = vertical_speed;
minimal_vertical_acceleration = 1;
minimal_horizontal_acceleration = 1;
accelerative_horizontal_speed = 1; //Pixels per frame
accelerative_vertical_speed = 16; //Pixels per frame
max_horizontal_speed = 4; //Pixels per frame
max_vertical_speed = 16; //Pixels per frame
//Variables for collision
collisionObjects = ds_list_create();
ds_list_add(collisionObjects, obj_collisionbox);
collisionCheck = ds_list_find_value(collisionObjects, 0);
[/I]
Movable object's Step event
Code:
///Setting movement keys
KeyRight = keyboard_check(vk_right);
KeyLeft = keyboard_check(vk_left);
KeyUp = keyboard_check_pressed(vk_up);
KeyDown = keyboard_check_pressed(vk_down);
//Up key//
//Up key not pressed
if !KeyUp
{
if place_meeting(x,y+1,collisionCheck)
{
vertical_speed = 0;
}
else
{
if vertical_speed != max_vertical_speed
{
vertical_speed += accelerative_vertical_speed;
}
}
}
//Up key pressed
if KeyUp
{
if place_meeting(x,y+1,collisionCheck)
{
//Start jumping only after 4 frames
alarm[0] = 4;
}
}
y += vertical_speed;
//Up key//
//Up key not pressed
if !KeyUp
{
if place_meeting(x,y+1,collisionCheck)
{
vertical_speed = 0;
}
else
{
if vertical_speed != max_vertical_speed
{
vertical_speed += accelerative_vertical_speed;
}
}
}
//Up key pressed
if KeyUp
{
if place_meeting(x,y+1,collisionCheck)
{
//Start jumping only after 4 frames
alarm[0] = 4;
}
}
y += vertical_speed;
//Right key//
//Right key press
if KeyRight
{
if place_meeting(x+horizontal_speed,y,collisionCheck)
{
while(!place_meeting(x+sin(horizontal_speed),y,collisionCheck))
{
x += minimal_horizontal_acceleration;
}
horizontal_speed = 0;
}
else if !place_meeting(x+horizontal_speed,y,collisionCheck)
{
if (horizontal_speed != max_horizontal_speed) && place_meeting(x,y+1,collisionCheck)
{
horizontal_speed += accelerative_horizontal_speed;
}
else if place_meeting(x,y+1,collisionCheck)
{
horizontal_speed = max_horizontal_speed;
}
else
{
horizontal_speed = horizontal_speed;
}
}
}
//Right key no press
if !KeyRight
{
if place_meeting(x+horizontal_speed,y,collisionCheck)
{
horizontal_speed = 0;
}
else if !place_meeting(x+horizontal_speed,y,collisionCheck)
{
if horizontal_speed != 0
{
horizontal_speed -= accelerative_horizontal_speed;
}
else
{
horizontal_speed = 0;
}
}
}
//Left key//
//Left key press
if KeyLeft
{
if place_meeting(x-horizontal_speed,y,collisionCheck)
{
while(!place_meeting(x+sin(horizontal_speed),y,collisionCheck))
{
x += minimal_horizontal_acceleration;
}
horizontal_speed = 0;
}
else if !place_meeting(x-horizontal_speed,y,collisionCheck)
{
if (horizontal_speed != -max_horizontal_speed) && place_meeting(x,y+1,collisionCheck)
{
horizontal_speed -= accelerative_horizontal_speed;
}
else if place_meeting(x,y+1,collisionCheck)
{
horizontal_speed = -max_horizontal_speed;
}
else
{
horizontal_speed = horizontal_speed;
}
}
}
//Left key no press
if !KeyLeft
{
if place_meeting(x-horizontal_speed,y,collisionCheck)
{
horizontal_speed = 0;
}
else if !place_meeting(x-horizontal_speed,y,collisionCheck)
{
if horizontal_speed != 0
{
horizontal_speed += accelerative_horizontal_speed;
}
else
{
horizontal_speed = 0;
}
}
}
x += horizontal_speed;
Movable object's Alarm 0
Code:
vertical_speed += accelerative_vertical_speed;
if accelerative_vertical_speed != minimal_vertical_acceleration
{
accelerative_vertical_speed = accelerative_vertical_speed / 2;
alarm[0] = 1;
}
Problems that I am having is that the object slows down sometimes when moving horizontally near a wall, sometimes gets stuck in a wall, can't be moved in the opposite direction whilst next to the wall (so, left if there's a wall on the right and vice versa), the object doesn't decelerate horizontally when right or left key is let go of (but does so when the other horizontal movement control key is pressed, so, if I'm moving to the right and I press left, the character will move to the right slower over time and once it reaches 0, it starts moving to the right, which is intended) and the object doesn't jump at all. Another thing that might be of note is that I've purposefully made it so that you cannot move the object left or right whilst in mid-air (at least that works nicely).
And, I think I should also mention, before anyone brings this up that yes, I do understand my own code for the most part and what each thing is supposed to do. What I don't understand is why it does those glitches when it's not supposed to by my understanding.