J
Jon Kennel
Guest
v2.1.3.189
My game (loosely based on "Beowulf") has a character who can press 'x' to swing his sword. This creates my (obj_sword_hitbox) in front of the "obj_player" for a single frame. When the (obj_sword_hitbox) collides with (obj_enemy_parent), I want to create a knock-back effect on the enemy.
(I'M CREATING THIS GAME FOR MY ENGLISH CLASS AND IT IS DUE ON TUESDAY SO QUICK RESPONSES WOULD BE GREATLY APPRECIATED)
Anyway, the knock-back code only works properly if the enemy is not being knocked back against or near a wall. If I hit an enemy near a wall, the enemy seems to get stuck inside of the wall and continues to jump up and fall back down.
I have a state machine set up where the enemy switches between an "idle" state and a "damaged" state.
I think it's because the enemy is pushed inside of the wall by the knock-back and is therefore unable to touch the ground and reset back to it's "idle" state; and is therefore perpetually stuck in the "damaged" state.
But that's just my theory.
I've tried using (place_meeting) and (place_free) functions to check for a wall; but I haven't been able to get those to work. I've also tried looking through various forum posts for solutions; but I haven't had any luck with that either.
Here is the collision event with the (obj_sword_hitbox) - (inside of "obj_enemy_parent")
Step event - inside obj_enemy_parent
Create event - inside of obj_enemy_parent
Script "scr_enemy_damaged"
script "scr_enemy_idle"
Here is the script "scr_collision"
I think I've posted all relevant code here but if you have any questions or need any more info then just ask. Like I said before, I only have 3 days to finish this game and still have quite lot of work ahead of me. This knock-back issue is really holding me back, so I appreciate anyone who takes the time to respond.
Thanks.
My game (loosely based on "Beowulf") has a character who can press 'x' to swing his sword. This creates my (obj_sword_hitbox) in front of the "obj_player" for a single frame. When the (obj_sword_hitbox) collides with (obj_enemy_parent), I want to create a knock-back effect on the enemy.
(I'M CREATING THIS GAME FOR MY ENGLISH CLASS AND IT IS DUE ON TUESDAY SO QUICK RESPONSES WOULD BE GREATLY APPRECIATED)
Anyway, the knock-back code only works properly if the enemy is not being knocked back against or near a wall. If I hit an enemy near a wall, the enemy seems to get stuck inside of the wall and continues to jump up and fall back down.
I have a state machine set up where the enemy switches between an "idle" state and a "damaged" state.
I think it's because the enemy is pushed inside of the wall by the knock-back and is therefore unable to touch the ground and reset back to it's "idle" state; and is therefore perpetually stuck in the "damaged" state.
But that's just my theory.
I've tried using (place_meeting) and (place_free) functions to check for a wall; but I haven't been able to get those to work. I've also tried looking through various forum posts for solutions; but I haven't had any luck with that either.
Here is the collision event with the (obj_sword_hitbox) - (inside of "obj_enemy_parent")
Code:
/// Collision event with obj_sword_hitbox
if (state != scr_enemy_damaged) {
image_blend = make_color_rgb(220, 150, 150)
hsp = (sign(x-other.x)*4);
vsp = -3;
state = scr_enemy_damaged;
scr_collision();
}
if (instance_exists(obj_enemy_stats)) {
obj_enemy_stats.hp -= 1;
}
Step event - inside obj_enemy_parent
Code:
/// Step event
// Execute state
script_execute(state);
Create event - inside of obj_enemy_parent
Code:
/// Create event
//Initializes variables
hsp = 0;
vsp = 0;
grav = .2;
// Sets default state
state = scr_enemy_idle;
Script "scr_enemy_damaged"
Code:
/// scr_enemy_damaged
// Face the source of the damage
if (hsp != 0) {
image_xscale = -sign(hsp);
}
// Apply gravity
if (!place_meeting(x,y+1,obj_solid)) {
vsp += grav;
} else {
vsp = 0;
// Apply friction
hsp = lerp(hsp,0,1);
}
// Collision code
scr_collision();
// Change back to idle state
if (hsp == 0 && vsp == 0) {
image_blend = c_white;
state = scr_enemy_idle;
}
script "scr_enemy_idle"
Code:
/// scr_enemy_idle
// Set color back to normal
image_blend = c_white;
// Flips sprite towards player
if (instance_exists(obj_player)) {
image_xscale = -(sign(x-obj_player.x));
}
// Applies gravity
if (!place_meeting(x,y+1,obj_solid)) {
vsp += grav;
} else {
vsp = 0;
}
// Collsion code
scr_collision();
Here is the script "scr_collision"
Code:
/// scr_collision
// Horizontal collision
if (place_meeting(x+hsp,y,obj_solid)) {
while (!place_meeting(x+sign(hsp),y,obj_solid)) {
x += sign(hsp);
}
hsp = 0;
}
x += hsp;
// Vertical Collision
if (place_meeting(x,y+vsp,obj_solid)) {
while (!place_meeting(x,y+sign(vsp),obj_solid)) {
y += sign(vsp);
}
vsp = 0;
}
y += vsp;
I think I've posted all relevant code here but if you have any questions or need any more info then just ask. Like I said before, I only have 3 days to finish this game and still have quite lot of work ahead of me. This knock-back issue is really holding me back, so I appreciate anyone who takes the time to respond.
Thanks.