1. Hello Guest! It's with a heavy heart that we must announce the removal of the Legacy GMC Archive. If you wish to save anything from it, now's the time! Please see this topic for more information.
    Dismiss Notice

Objects Behaving differently even though they are the same

Discussion in 'Programming' started by Arj72, Oct 14, 2017.

  1. Arj72

    Arj72 Guest

    I've been working on a 2.5D platformer game for about a week now and I've run into an issue. The main character encounters enemies, called Nuggets, that just walk left in right within designated zones and are supposed to hurt the player when you walk in to them. You can kill them by jumping on them. My problem I am encountering is some of the enemies function properly, and some just walk right by the player, never causing damage. Here is the two different codes, the first one for killing the enemy, by triggering an alarm that destroys it, and the second is where it hurts the player, before once again being destroyed.

    if (place_meeting(x,y,obj_player))

    {
    if (obj_player.y < y-6)
    {
    with (instance_nearest(x,y, Obj_Nugget))
    alarm[0]= .044*room_speed
    sprite_index= (Spr_NuggetDie);}
    }
    ----------------------------------------------------------
    if (place_meeting(x,y,obj_player))

    {
    if (obj_player.y >= Obj_Nugget.y)
    {
    with (instance_nearest(x,y, Obj_Nugget))
    sprite_index= Spr_NuggetDie
    global.phealth -= 1
    alarm[0] = .044*room_speed;}
    }
     
  2. WinuX

    WinuX Member

    Joined:
    Feb 17, 2017
    Posts:
    24
    I just assume that the 2 codes above are in Obj_Nugget.

    You are refering to Obj_Nugget in your Code a lot, that means you refering to all of them not just the one the player collides with.

    Try putting the code below into the Nuggets step event.

    if (place_meeting(x,y,obj_player))
    {
    if (other.y < y-6)
    {
    alarm[0]= .044*room_speed;
    sprite_index= (Spr_NuggetDie);
    }
    }
    ----------------------------------------------------------
    if (place_meeting(x,y,obj_player))
    {
    if (other.y >= y)
    {
    sprite_index= Spr_NuggetDie;
    global.phealth -= 1;
    alarm[0] = .044*room_speed;
    }
    }

    The "other" keyword is just refering to the other instance involved in the collision (obj_player).
     
  3. Arj72

    Arj72 Guest


    Thanks! I'll try that now and let you know how it works.
     
  4. Arj72

    Arj72 Guest

    It seems that with that code now the player's health is deducted even if I fall onto the Nugget. But all the Nuggets were doing damage this time.
     
  5. WinuX

    WinuX Member

    Joined:
    Feb 17, 2017
    Posts:
    24
    make an variable called dying or something like that and set it on true when the Nugget gets killed

    something like that


    CREATE:
    dying = false;


    STEP:

    if (place_meeting(x,y,obj_player) && !dying)
    {
    if (other.y < y-6)
    {
    dying = true;
    alarm[0]= .044*room_speed;
    sprite_index= (Spr_NuggetDie);
    }
    }
    ----------------------------------------------------------
    if (place_meeting(x,y,obj_player) && !dying)
    {
    if (other.y >= y )
    {
    dying = true;
    sprite_index= Spr_NuggetDie;
    global.phealth -= 1;
    alarm[0] = .044*room_speed;
    }
    }
     
  6. Arj72

    Arj72 Guest

    That didn't work either... I must have had some weird original coding somewhere because the Nuggets will die now, but it still hurts the player to jump on them too.
     
  7. WinuX

    WinuX Member

    Joined:
    Feb 17, 2017
    Posts:
    24
    Could you try this it should work.

    if (place_meeting(x,y,obj_player))
    {
    //Jump
    if (other.y < y-6 && !dying)
    {
    alarm[0]= .044*room_speed;
    sprite_index= (Spr_NuggetDie);
    dying = true;
    }

    //Walk in
    if (other.y >= y && !dying)
    {
    sprite_index= Spr_NuggetDie;
    global.phealth -= 1;
    alarm[0] = .044*room_speed;
    dying = true;
    }
    }
     
  8. Arj72

    Arj72 Guest


    For whatever reason it isn't working, the player is still taking damage from jumping on them .
     
  9. WinuX

    WinuX Member

    Joined:
    Feb 17, 2017
    Posts:
    24
    Can you try the code below.
    When you walk or jump into the nugget a popup message should appear.
    If you Jump on a Enemy it will say "Jumped on Enemy".
    If you walk into one it will say "Walked into an Enemy".

    When the Popups are correct about what is happening, then something is off somewhere else.

    if (place_meeting(x,y,obj_player))
    {
    //Jump
    if (other.y < y-6 && !dying)
    {
    alarm[0]= .044*room_speed;
    sprite_index= (Spr_NuggetDie);
    dying = true;
    show_message("Jumped on enemy");
    }

    //Walk in
    if (other.y >= y && !dying)
    {
    sprite_index= Spr_NuggetDie;
    global.phealth -= 1;
    alarm[0] = .044*room_speed;
    dying = true;
    show_message("Walked in Enemy");
    }
    }
     
  10. Arj72

    Arj72 Guest

    Thank you for the help, but I think I figured it out. Because I have such a weird system where an alarm gives the enemy enough time to play the death animation, having the code you gave me under step made it instantly check for a collision again before the nugget could die, and the player was no longer jumping on it. I put it back under collision and am hoping that will work.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice