Legacy GM Two questions about jumping and melee attack.

B

Brian Le

Guest
Hi guys!
Last step of my game!
I am making a 2-player game in which they can punch each other. I have coded 2 objects, each stand for a player, named obj_player and obj_player2. They are the same except for the input keys. The problem is when player 1 attacks player 2, he receives the damage, but when player 2 attacks player 1, nothing happens. I will just add the 'receive damage' part of my code:
obj_player
Code:
var damage_place = instance_place(x,y,par_hitbox);
            if (damage_place != noone) {  //Check if the hitbox is collided with the player or not
                if (damage_place.type == 2) { //Check if the hitbox belongs to the second player
                    damaged = true;
                    damage_plus ++; //Count the number of damage for personal use (Just don't care much about it)
                    if (damage_plus > 2) {
                        damage_plus = 1; //If the number of damage is larger than 2 then it will be 1 (at most 2)
                    }
                    damage_delay = 0;          
                }  
            }
            if (damage_plus != 0) { //Check if the player is receiving the damage
                damage_delay ++;
                if (damage_delay >= 11) {
                    damage_plus = 0;
                    damaged = false;
                } //Check after about 11 frames all the damage will be remove.
            }
            if (damaged) {
                image_xscale = -obj_player2.image_xscale; //Flip the player 1 according to player 2
                ladder = false;
                sprite_index = spr_player_damaged; //Change //sprite
                if (damage_plus == 1) image_index = 0; //Check if the player receive the first damage, then the image_index is 1
                if (damage_plus == 2) image_index = 1;
                if (damage_plus == 1 && obj_player2.melee_counter == 3) {
                    damage_last = true; //Just a custom variable
                    vsp = -10; //Throw the player upwards
                }
            }
obj_player2 (similar):
Code:
var damage_place = instance_place(x,y,par_hitbox);
            if (damage_place != noone) {
                if (damage_place.type == 1) {
                    damaged = true;
                    damage_plus ++;
                    if (damage_plus > 2) {
                        damage_plus = 1;
                    }
                    damage_delay = 0;          
                }
            }
            if (damage_plus != 0) {
                damage_delay ++;
                if (damage_delay >= 11) {
                    damage_plus = 0;
                    damaged = false;
                }
            }
            if (damaged) {
                image_xscale = -obj_player.image_xscale;
                ladder = false;
                sprite_index = spr_player_damaged;
                if (damage_plus == 1) image_index = 0;
                if (damage_plus == 2) image_index = 1;
                if (damage_plus == 1 && obj_player.melee_counter == 3) {
                    damage_last = true;
                    vsp = -10;
                }
            }
par_hitbox Create Event Code:
Code:
type = 1;
par_hitbox Animation End Event Code:
Code:
instance_destroy();
obj_hitbox1, obj_hitbox2: Created when the player 1 is punching - no action.
obj_hitbox1_2, obj_hitbox2_2: Created when the player2 is puching - Create Event:
Code:
type = 2;
The question is very difficult for me, but may be simple for you guys.
Another question is about jumping: How can you check the player jumps from a certain height? i.e: about 50px from the ground.
Thanks in advanced!!!
 

samspade

Member
Hi guys!
Last step of my game!
I am making a 2-player game in which they can punch each other. I have coded 2 objects, each stand for a player, named obj_player and obj_player2. They are the same except for the input keys. The problem is when player 1 attacks player 2, he receives the damage, but when player 2 attacks player 1, nothing happens. I will just add the 'receive damage' part of my code:
obj_player
Code:
var damage_place = instance_place(x,y,par_hitbox);
            if (damage_place != noone) {  //Check if the hitbox is collided with the player or not
                if (damage_place.type == 2) { //Check if the hitbox belongs to the second player
                    damaged = true;
                    damage_plus ++; //Count the number of damage for personal use (Just don't care much about it)
                    if (damage_plus > 2) {
                        damage_plus = 1; //If the number of damage is larger than 2 then it will be 1 (at most 2)
                    }
                    damage_delay = 0;         
                } 
            }
            if (damage_plus != 0) { //Check if the player is receiving the damage
                damage_delay ++;
                if (damage_delay >= 11) {
                    damage_plus = 0;
                    damaged = false;
                } //Check after about 11 frames all the damage will be remove.
            }
            if (damaged) {
                image_xscale = -obj_player2.image_xscale; //Flip the player 1 according to player 2
                ladder = false;
                sprite_index = spr_player_damaged; //Change //sprite
                if (damage_plus == 1) image_index = 0; //Check if the player receive the first damage, then the image_index is 1
                if (damage_plus == 2) image_index = 1;
                if (damage_plus == 1 && obj_player2.melee_counter == 3) {
                    damage_last = true; //Just a custom variable
                    vsp = -10; //Throw the player upwards
                }
            }
obj_player2 (similar):
Code:
var damage_place = instance_place(x,y,par_hitbox);
            if (damage_place != noone) {
                if (damage_place.type == 1) {
                    damaged = true;
                    damage_plus ++;
                    if (damage_plus > 2) {
                        damage_plus = 1;
                    }
                    damage_delay = 0;         
                }
            }
            if (damage_plus != 0) {
                damage_delay ++;
                if (damage_delay >= 11) {
                    damage_plus = 0;
                    damaged = false;
                }
            }
            if (damaged) {
                image_xscale = -obj_player.image_xscale;
                ladder = false;
                sprite_index = spr_player_damaged;
                if (damage_plus == 1) image_index = 0;
                if (damage_plus == 2) image_index = 1;
                if (damage_plus == 1 && obj_player.melee_counter == 3) {
                    damage_last = true;
                    vsp = -10;
                }
            }
par_hitbox Create Event Code:
Code:
type = 1;
par_hitbox Animation End Event Code:
Code:
instance_destroy();
obj_hitbox1, obj_hitbox2: Created when the player 1 is punching - no action.
obj_hitbox1_2, obj_hitbox2_2: Created when the player2 is puching - Create Event:
Code:
type = 2;
The question is very difficult for me, but may be simple for you guys.
Another question is about jumping: How can you check the player jumps from a certain height? i.e: about 50px from the ground.
Thanks in advanced!!!
I'm not sure about the answer to your first question, though if I had to guess, I would guess that your object variable type isn't being set or read correctly. You can use the debugger or show debug messages to find out. A first step I use in a lot of my debugging problems is to start with show_debug_message('a'); show_debug_message('b'); etc in the code just to see where the code is getting to.

Regarding the second question, I'm not entirely sure I understand. You just want to know how to tell if a player was a certain height above ground at some point? This is pretty simple but the best solution would depend on other variables. For example, if the room is flat, you could just calculate if (abs(y - floor_height) > 50) high_enough = true; You could make it relative to the other player. You could check the player's fall speed. You could save the y at the start of a jump and compare it to the player's y when the player starts to fall (e.g. has positive y speed). You could go :

Code:
var y_check = y;
if (!place_meeting(x, y_check, ground)) {
    while (!place_meeting(x, y_check, ground)) {
        y_check += 1;
    }
    var height = abs(y - y_check);
}
All of these would work for some cases but not others.
 
B

Brian Le

Guest
I'm not sure about the answer to your first question, though if I had to guess, I would guess that your object variable type isn't being set or read correctly. You can use the debugger or show debug messages to find out. A first step I use in a lot of my debugging problems is to start with show_debug_message('a'); show_debug_message('b'); etc in the code just to see where the code is getting to.

Regarding the second question, I'm not entirely sure I understand. You just want to know how to tell if a player was a certain height above ground at some point? This is pretty simple but the best solution would depend on other variables. For example, if the room is flat, you could just calculate if (abs(y - floor_height) > 50) high_enough = true; You could make it relative to the other player. You could check the player's fall speed. You could save the y at the start of a jump and compare it to the player's y when the player starts to fall (e.g. has positive y speed). You could go :

Code:
var y_check = y;
if (!place_meeting(x, y_check, ground)) {
    while (!place_meeting(x, y_check, ground)) {
        y_check += 1;
    }
    var height = abs(y - y_check);
}
All of these would work for some cases but not others.
Thank you for a quick answer, however, I have checked the project many times, nothing different between 2 objects: player 1 and player 2. I don't understand why player 1 behaved in a different way, he can't not be damaged by player 2. If you have time, please take a look at my project. When you want to see it, please reply me under this post.
Thanks!
 

samspade

Member
Thank you for a quick answer, however, I have checked the project many times, nothing different between 2 objects: player 1 and player 2. I don't understand why player 1 behaved in a different way, he can't not be damaged by player 2. If you have time, please take a look at my project. When you want to see it, please reply me under this post.
Thanks!
I'd look at the project if you wanted me to.
 
B

Brian Le

Guest
I'd look at the project if you wanted me to.
Thank you very much, please give me your email.
My project is a little bit messy, and it may take your time.
I may send you about next 30 minutes because I am commenting so that you can understand.
 
Top