Object changing

P

peteynunchucks

Guest
Hello, GameMaker community. I was hoping to get some advice on how to program objects that change once they interact with another object/player. 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 how I should program the ball for my game. Should the ball be two objects representing two different colors? How would I make the ball so that when the ball is one color the opposite color paddle can't hit it? Any advice given would be appreciated.
 

chamaeleon

Member
Hello, GameMaker community. I was hoping to get some advice on how to program objects that change once they interact with another object/player. 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 how I should program the ball for my game. Should the ball be two objects representing two different colors? How would I make the ball so that when the ball is one color the opposite color paddle can't hit it? Any advice given would be appreciated.
For a case like this it seems like you'd only need to have two different sprites to switch between using sprite_index or two frames in the same sprite by setting image_index (with image_speed set to 0), since there are no other behavior changes. Maybe a variable stored in some instance (the ball) or global, indicating the player instance that should hit the ball. Any collision checking should only react to the player instance stored, ignoring the other player instance.
 
P

peteynunchucks

Guest
Thank you for the information. I have finally gotten back around to this project and what I was trying to figure out now is how can I have the ball when it hits the back wall changes colors? Might this be done with a switch statement? Listed below is some of my code in my step phase for the ball. I would have to imagine making it work around the ball hitting x=0 value would work. any help would be appreciated.

/// ball movement
// if the game has started, if the game is not pause, and if the game is not paused.
if(!global.gamestart && !global.gamepaused && !global.gameover)
{
x += vx;
y += vy;
}
//check boundry
if (y <= 0)
vy *= -1;
else if(y >= 576)
vy *= -1;
else if (x <= 0)
vx *= -1;
//check if player scored
if( x>= 1024)
{
//which player scored
if( x>= 1024)
global.player1_score += 1;
 
You could definitely use a switch statement, but just to make sure: a switch statement does not "switch" values, it's simply a short form if statement (with a little bit of extra on the side) and since you are only toggling between two values, a switch statement is really just overkill. Just one thing I noted about your code: I can see you've bracketed the top if statement properly, but I would really recommend bracketing every if statement. There's certainly people who like to not bracket single line if statements for the sake of quicker typing speed but, especially while you are learning, it's much better to get into the habit of bracketing everything so that you don't run into any unintended consequences (many a newbie has had 'bugs' occur because GMS is interpreting their lack of bracketing differently to how they themselves are interpreting it). If the wall you want the ball to change sides is the left wall, this is a potential method you can add in to your already existing code:
Code:
else if (x <= 0) {
   vx *= -1;
   if (sprite_index == spr_ball_colour_1) {
      sprite_index = spr_ball_colour_2;
   }
   else {
      sprite_index = spr_ball_colour_1;
   }
}
Then on collision with your paddle, you would check to see what colour side of the paddle the ball is hitting (I don't know how you have setup the paddle so can't help much here) AND what sprite_index the ball is using and act accordingly if they are the same or different.
 
P

peteynunchucks

Guest
Thank you for the information and help. I haven't quite figured out how I was going to program the paddles behavior with the different colored balls. In my code for the paddles, I only have the code for the movement in it at the moment.

case "player_1":
{
// set image for player 1
image_index = 0;

//move paddle up
if (keyboard_check(ord("W"))) || (gamepad_axis_value(0,gp_axislv) < 0)
vy = -movespeed;

//move paddle down
else if (keyboard_check(ord("S"))) || (gamepad_axis_value(0,gp_axislv) > 0)
vy = movespeed;

//if nothing then idle
else
vy = 0;

break;
}
 
Top