Storm1208
Member
I'm working on a finite state machine implementation for a platforming character. I initially coded the character normally (i.e. not using a state machine approach) but am now beginning to convert what I had previously implemented into a state machine format.
To aid in readability, each separate state is it's own script and some of the functionality within them is also being given its own scripts. I took the my horizontal and vertical collision code, placed them in their own scripts, and then called them from within the necessary state scripts. For some reason though, the collisions don't seem to be working now and my player is glitchily phasing through walls.
Here's a snippet of what my scripts look like:
running_state script (is called when the character is in the "running" state):
horizontal_collision script (handles horizontal collision):
To aid in readability, each separate state is it's own script and some of the functionality within them is also being given its own scripts. I took the my horizontal and vertical collision code, placed them in their own scripts, and then called them from within the necessary state scripts. For some reason though, the collisions don't seem to be working now and my player is glitchily phasing through walls.
Here's a snippet of what my scripts look like:
running_state script (is called when the character is in the "running" state):
GML:
// Script assets have changed for v2.3.0 see
// https://help.yoyogames.com/hc/en-us/articles/360005277377 for more information
function running_state(){
//covering horizontal movement
if(right_key - left_key != 0)
{
image_xscale = sign(hsp); //handling player facing direction
hsp = acc + hsp_walk * (right_key - left_key);
}
else
{
//handling gradual deceleration
if(hsp > 0)
{
hsp -= dec_const;
if(0 + hsp < 0.4)
{
hsp = 0;
my_state = PlayerState.idle;
}
}
else if(hsp < 0)
{
hsp += dec_const;
if(0 - hsp < 0.4)
{
hsp = 0;
my_state = PlayerState.idle;
}
}
}
//handling acceleration
if(right_key - left_key != 0 && hsp < 0)
{
if(hsp > sign(hsp) * speed_cap)
{
acc += sign(hsp) * acc_const;
}
}
else if(right_key - left_key != 0 && hsp > 0)
{
if(hsp < sign(hsp) * speed_cap)
{
acc += sign(hsp) * acc_const;
}
}
else if(right_key - left_key == 0)
{
acc = 0;
}
horizontal_collision();
}
GML:
// Script assets have changed for v2.3.0 see
// https://help.yoyogames.com/hc/en-us/articles/360005277377 for more information
function horizontal_collision(){
//horizontal collision//
/*checks to see if player is about to hit wall
and then inches them forward as close as it can
until they do*/
if(place_meeting(x + hsp, y, obj_ground))
{
while(!place_meeting(x + sign(hsp), y, obj_ground))
{
x += sign(hsp);
}
hsp = 0;
}
x += hsp;
}