• 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!

Programming different conditions for different players

P

peteynunchucks

Guest
Hello, GameMaker community. I was hoping to get some advice on how to program objects that can only interact with one player and not interact with another. To give more context, I am making a game which is like pong but plays like racketball. Both paddles are on one side and the color of the ball will correspond to which player needs to hit the ball. When the ball hits the other side of the play area it will change color when coming back.

What I need help with is figuring out how to program a statement in my paddles so that when the ball is a specific color the corresponding paddle can hit it.
 

Slyddar

Member
You must have a paddle colour variable for each player. Just use that in the collision event, or however you are colliding with ball, to allow the collision to only work when the ball has the same colour.
If it's a collision event in the ball with the paddle, something like this:
Code:
if other.paddle_colour == ball_colour {
  //your existing collision code would go here.
}
 

TsukaYuriko

☄️
Forum Staff
Moderator
What differentiates the paddles? Are they different objects? Do they have different sprites? Do they have the player's number assigned in a variable?

You can compare either of these to tell which is which.
 
P

peteynunchucks

Guest
The paddles are two separate objects and have different sprites. In the creation code for each paddle, there is an owner assigned which is either player 1 or player 2.
 

woods

Member
maybe something like this..

obj_player1_paddle step event
Code:
 if obj_ballcolor = blue {do stuff}

obj_player2_paddle step event
Code:
// if obj_ballcolor = red {do stuff}
 
P

peteynunchucks

Guest
so the code I have right now works for the most part except that the red paddle can hit both balls. Blue works perfectly and hits the specific ball that is designated to hit. This code is in the step event for the ball.

}
//check collision
if(place_meeting(x, y, OBJ_paddle_red && sprite_index == SPR_ball_red)){
vx *= -1;
vy *= -1;
}
else if(place_meeting(x, y, OBJ_paddle_blue && sprite_index == SPR_ball_blue)){
vx *= -1;
vy *= -1;
}
 

FrostyCat

Redemption Seeker
You misplaced the part after && and turned it into a logical error.
Code:
if (place_meeting(x, y, OBJ_paddle_red) && sprite_index == SPR_ball_red) {
Code:
else if (place_meeting(x, y, OBJ_paddle_blue) && sprite_index == SPR_ball_blue) {
It's important to always check how brackets match up when writing code, even when you don't have a syntax error.
 
P

peteynunchucks

Guest
That worked! thank you for the information and the heads up.
 
Top