Help Needed Coding Enemies

NovaOzuka

Member
I wonder how many topics I need to make before I can make a demo of my first platformer. Anyway, I now need help coding two different enemies, and hopefully I can figure the rest out on my own. The first is a Goomba type enemy, and the other is an enemy that can't be hurt by simply jumping on it, but by the side instead. From there I should be able to code the rest of the enemy types I can come up with.

Obviously, they need to be able to move on their own, but they don't have to follow the player.
 

Xer0botXer0

Senpai
What is a goomba type enemy ? Is this a platformer ? The second enemy sounds like you just need to use player position relative to enemy code with key input.
 

TheouAegis

Member
In the collision event for your player with the goomba enemy, check if bbox_bottom-vspeed < other.bbox_top. Replace vspeed with whatever you use for vertical speed. If that expression is true, bounce up and deal damage to the enemy. If it's false, deal damage to the player and apply knockback/immunity.
 

NovaOzuka

Member
I can't seem to figure out how to get knockback working. Or how to code the enemy object deletion.

I mean, since the player isn't already set to collide with the enemy like it was a moving wall, it would be easy to get hsp right, because even if I got the hsp wrong, it would in theory constantly apply itself until the player completely passes through the enemy.
 

samspade

Member
I can't seem to figure out how to get knockback working. Or how to code the enemy object deletion.

I mean, since the player isn't already set to collide with the enemy like it was a moving wall, it would be easy to get hsp right, because even if I got the hsp wrong, it would in theory constantly apply itself until the player completely passes through the enemy.
What does you're current knockback code look like?
 

samspade

Member
if (other.x > x - 16)
{
hsp = -4
}
else if (other.x > x + 16)
{
hsp = 4
}
Where is this located? I'll assume this is in the enemy instance somewhere? If so, then the next question is, how is your enemy hsp set? If you're setting enemy hsp every step, you'll just over write this.
 

NovaOzuka

Member
Well I was trying to get knockback applied to the player, so the enemy's hsp shouldn't really matter, which is controlled by a simple state machine, and even if it does, I could always set it up so that the enemy turns away from the player upon bumping into the player.
 

NovaOzuka

Member
So I'm still having trouble with this. Specifically the knockback part.
From obj_enemy's collision event, we have
Code:
if (other.y+16<y and obj_player.vsp>0)
{other.vsp = -7
instance_destroy()}
else
{obj_HUD.currenthealth =-1
if (obj_player.hsp < 0)
{obj_player.knockback = 3}
if (obj_player.hsp = 0)
{if (state = "right")
{obj_player.knockback = 3}}
if (obj_player.hsp > 0)
{obj_player.knockback = -3}
if (obj_player.hsp = 0)
{if (state = "left")
{obj_player.knockback = -3}}}
And from obj_player's Step Event we have
Code:
//Knockback
if (knockback > 0)
{knockback = knockback - 1
hsp = 3}
if (knockback < 0)
{knockback = knockback + 1
hsp = -3}
Now the bounce up and destroy enemy part is working. The knockback? Not so much. Even if it were working incorrectly, it should be at least be pushing the player into the enemy rather than away from it, but it's not doing anything at all, regardless of if the player is moving or not. If the player wasn't moving, then the enemy should be able to push the player away, but not even that is working.
 

NovaOzuka

Member
You are probably resetting hsp in the code for each state.
The thing of it is the code from the player object is from the end of the Step Event code, so it should be resetting to (-)3 for 3 steps. I did use virtually the same piece of code for wall jumping, and that piece of code is still working.
 

TheouAegis

Member
And you verified the knockback variable is getting set and counting to 0?

And where do you increment x by hsp?

It should be

State code that sets hsp
Knockback read code that sets hsp
Collision code that might set hsp to 0
Increase x by hsp.
 

NovaOzuka

Member
If I remember right, my code goes

Increase x by hsp with player controlled movement
Wall collision
Wall slide and jump (which works)
Knockback

I might have the slide and jump before the collision only because they are both player controlled, but I don't remember.
 

NovaOzuka

Member
Yeah, I had the order wrong. The collision for the wall was after the wall slide/jump. Reordering the code seems to have worked. There was a small issue of the player getting stuck inside enemies, but adding in the mercy invincibility fixed that one.

I am having an issue with the player's health now though. for some reason, a single collision with the enemy appears to completely deplete the player's health.

Okay, I found the problem myself. The problem is I still don't know what I'm doing and made a silly mistake. XD "obj_HUD.currenthealth =-1" should've been "obj_HUD.currenthealth -=1"
 
Last edited:

NovaOzuka

Member
So, small problem I want to rectify: the enemy code that I posted above doesn't require the player to land on it to be defeated. The player merely has to collide while falling, regardless of it's from above or the side. How can I fix this?
 

Bentley

Member
So, small problem I want to rectify: the enemy code that I posted above doesn't require the player to land on it to be defeated. The player merely has to collide while falling, regardless of it's from above or the side. How can I fix this?
Code:
if (other.vspd > 0) instance_destroy();
 

JackTurbo

Member
On collision run a point_direction between the enemy and the player and use that to decide if the enemy should die or the player receive damage.

Like if it returns more than 65 but less than 115 then consider that a success and kill the enemy. Play around with those numbers to effect how strict you want to be about what you consider "above".
 

TheouAegis

Member
if other.vspd > 0 && other.bbox_bottom-other.vspd < bbox_top {

That's "technically" a collision from above. If diagonals are an area of contention for you, then you'd have to also check if there's a collision at x-hspd (but using the current y). But personally I think that's just going to give players a griefing point.
 

NovaOzuka

Member
if other.vspd > 0 && other.bbox_bottom-other.vspd < bbox_top {

That's "technically" a collision from above. If diagonals are an area of contention for you, then you'd have to also check if there's a collision at x-hspd (but using the current y). But personally I think that's just going to give players a griefing point.
That seems to have fixed it. Now I just need to figure out how to code a few more things and get a few more sprites ready before I can code my second enemy.
 

Bentley

Member
That's basically what I already have. That doesn't tell me how to fix the problem.
Oh, my bad, I misread your question. I thought you meant only destroy the enemy if the player is falling, regardless of whether he's above or below. I got it backwards haha. Ignore my post.
 
Last edited:
Top