• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

[SOLVED] Problem with enemie collisions

V

Vincent

Guest
So my game is a platformer, and I'm a noob in coding. My idea was to create a collision that made you jump instantly and lose a bit of health. I've made the code myself and it works more or less, but each time i jump (it worke before the new code) i get launched to the sky and lose health constantly, and I don't know why. I'm looking for a code that works and doesn't affect the rest of the actions

Player's Create event:
Code:
gravedad = 1
vel_horizontal = 0
vel_vertical = 0
vel_salto = 16
vel_movimiento = 6

global.hp = 12
global.dano = true
Player's Step event 1
Code:
///Movimiento y gravedad
//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

//Gravedad
if (vel_vertical < 10)
    {
    vel_vertical += gravedad
    }
Player's Step event 2
Code:
if (global.dano = true)
and (place_meeting (x+1, y, obj_arana))
and (sprite_index = spr_fred_caminar_der) or (sprite_index = spr_fred_saltar_der)
    {
    alarm[0] = room_speed
    {
    global.dano = false
    global.hp -= 1
    y -= 128
    sprite_index = spr_fred_dano_der
    image_speed = 0.2
    }
    global.dano = true
    }
    
if (global.dano = true)
and (place_meeting (x+1, y, obj_arana))
and (sprite_index = spr_fred_caminar_izq) or (sprite_index = spr_fred_saltar_izq)
    {
    alarm[0] = room_speed
    {
    global.dano = false
    global.hp -= 1
    y -= 128
    sprite_index = spr_fred_dano_izq
    image_speed = 0.2
    }
    global.dano = true
    }
Thanks!!
 
F

frumple

Guest
I'm not sure about this but try:
Code:
if (global.dano = true) and (place_meeting (x+1, y, obj_arana))
{
  if (sprite_index = spr_fred_caminar_der) or (sprite_index = spr_fred_saltar_der)
  {
  ...
instead of:
Code:
if (global.dano = true)
and (place_meeting (x+1, y, obj_arana))
and (sprite_index = spr_fred_caminar_der) or (sprite_index = spr_fred_saltar_der)
The way you have chained "and" and "or" statements seems wrong to me
 
V

Vincent

Guest
I'm not sure about this but try:
Code:
if (global.dano = true) and (place_meeting (x+1, y, obj_arana))
{
  if (sprite_index = spr_fred_caminar_der) or (sprite_index = spr_fred_saltar_der)
  {
  ...
instead of:
Code:
if (global.dano = true)
and (place_meeting (x+1, y, obj_arana))
and (sprite_index = spr_fred_caminar_der) or (sprite_index = spr_fred_saltar_der)
The way you have chained "and" and "or" statements seems wrong to me
Now it works!!
Thanks a lot! I've been stuck with this since like a month :O
 
F

frumple

Guest
Glad to help.

When you need to use "and" and "or" statements to make several comparisons it's better to check if one is true, then if it is check if another is true and so on.

You can do if (something) and (something) and (something) and (something)
If all 4 are true, the code in {} will execute.
You can also do if ( (something A) and (something B) ) and ( (something C) or (something D) ) {...}
It would work if both A and B, and one of C or D were true.
What you can't do is write all the conditions and expect GM to guess how you want to compare them.

It's similar to using:
a + b * c - d
vs.
((a + b) * c) - d

In the first example the order the operations are performed is unclear and will not be the same on all platforms. Will it go left to right? Or right to left? Or will it perform multiplication before addition/subtraction or not?
The second example is very specific and allways adds a and b, then multiplies that by c, and finally subtracts d from that.
 
Top