GML Problem: collision with enemies

V

Vincent

Guest
So I'm making a platformer, and I want to collide with and enemy, and when that happens, make the character jump, change sprite and decrease the hp, and I don't know how to do that.
This is the code

Player's "create" (variables)
Code:
gravedad = 1
vel_horizontal = 0
vel_vertical = 0
vel_salto = 16
vel_movimiento = 6

global.hp = 12
Player's "step"
Code:
//Variables
derecha = keyboard_check (ord("D"))
izquierda = -keyboard_check (ord("A"))
salto = keyboard_check_pressed (ord("W"))

//Movimiento
movimiento = derecha + izquierda
vel_horizontal = movimiento * vel_movimiento

if (vel_vertical < 10)
    {
    vel_vertical += gravedad
    }

//Colisiones
//Vertical
if (place_meeting (x, y+1, obj_cesped))
    {
    vel_vertical = salto * -vel_salto
    }

if (place_meeting (x, y+vel_vertical, obj_barrier))
    {
    while (!place_meeting (x, y+sign(vel_vertical), obj_cesped))
        {
        y += sign (vel_vertical)
        }
        vel_vertical = 0
    }

//Horizontal
if (place_meeting (x+vel_horizontal, y, obj_barrier))
    {
    while (!place_meeting (x+sign(vel_horizontal), y, obj_barrier))
        {
        x += sign (vel_horizontal)
        }
        vel_horizontal = 0
    }
   
x += vel_horizontal
y += vel_vertical
And that's the movement. How should I do it? Thanks!
(sorry if the code is very big, I'm a begginer in this xD)
 

Shawn Basnett

Discount Dev
Simple :)
In your Collision event with the enemy, place this code

Code:
//Enemigo Colisiones

if (sprite_index != spr_hurt)
{
vel_vertical = salto * -vel_salto;
global.hp -= 1;
alarm[0] = room_speed;
sprite_index = spr_hurt;
}
Replace spr_hurt with whatever sprite you want to change to when you get hurt.

Then, in the Alarm 0 event, place this code

Code:
sprite_index = spr_normal;
Just replace spr_normal with your normal sprite :)
 
V

Vincent

Guest
Simple :)
In your Collision event with the enemy, place this code

Code:
//Enemigo Colisiones

if (sprite_index != spr_hurt)
{
vel_vertical = salto * -vel_salto;
global.hp -= 1;
alarm[0] = room_speed;
sprite_index = spr_hurt;
}
Replace spr_hurt with whatever sprite you want to change to when you get hurt.

Then, in the Alarm 0 event, place this code

Code:
sprite_index = spr_normal;
Just replace spr_normal with your normal sprite :)
Cool, now the sprite works and the hp too, but I'm looking for a way that, when it collides, it jumps so it doesn't get all the hp down
Thanks!
 
V

Vincent

Guest
try adding to the "vel_salto" value, like this:

y-=vel_salto+32 (or whatever number)
This is the updated code:
Code:
///Colisiones enemigas
if place_meeting (x+1, y, obj_arana)
    and (sprite_index != spr_fred_dano_der)
    and (sprite_index = spr_fred_caminar_der) or (sprite_index = spr_fred_saltar_der)
    {
    y -= vel_salto
    global.hp -= 1;
    alarm[0] = room_speed;
    sprite_index = spr_fred_dano_der;
    image_speed = 0.3
    }
    
if place_meeting (x+1, y, obj_arana)
    and (sprite_index != spr_fred_dano_izq)
    and (sprite_index = spr_fred_caminar_izq) or (sprite_index = spr_fred_saltar_izq)
    {
    y -= vel_salto
    global.hp -= 1;
    alarm[0] = room_speed;
    sprite_index = spr_fred_dano_der;
    image_speed = 0.3
    }
But now, when I jump and I move around, I keep going up. Any ideas? xD
@anthropus @Shawn Basnett @Blakkid489
 
A

anthropus

Guest
This is the updated code:
Code:
///Colisiones enemigas
if place_meeting (x+1, y, obj_arana)
    and (sprite_index != spr_fred_dano_der)
    and (sprite_index = spr_fred_caminar_der) or (sprite_index = spr_fred_saltar_der)
    {
    y -= vel_salto
    global.hp -= 1;
    alarm[0] = room_speed;
    sprite_index = spr_fred_dano_der;
    image_speed = 0.3
    }
  
if place_meeting (x+1, y, obj_arana)
    and (sprite_index != spr_fred_dano_izq)
    and (sprite_index = spr_fred_caminar_izq) or (sprite_index = spr_fred_saltar_izq)
    {
    y -= vel_salto
    global.hp -= 1;
    alarm[0] = room_speed;
    sprite_index = spr_fred_dano_der;
    image_speed = 0.3
    }
But now, when I jump and I move around, I keep going up. Any ideas? xD
@anthropus @Shawn Basnett @Blakkid489
you need to use states (finite state machine: use enumerator, switch/case, and scripts). so when you hit an enemy, youre in the 'hit by enemy state' and only during that state your code (script) is executed. because now your code is in the step event so its probably telling the character to move up every step. fortunately i just learned about states so i can help you more with that.

but first check out these videos on states--they helped me tremendously!


(start videos at 0:00 ofc)
and
 

Shawn Basnett

Discount Dev
you need to use states (finite state machine: use enumerator, switch/case, and scripts). so when you hit an enemy, youre in the 'hit by enemy state' and only during that state your code (script) is executed. because now your code is in the step event so its probably telling the character to move up every step. fortunately i just learned about states so i can help you more with that.

but first check out these videos on states--they helped me tremendously!


(start videos at 0:00 ofc)
and
States are pretty simple, I wouldn't say they're absolutely necessary for this, but they are fairly useful.
 
Top