This site requires JavaScript, please enable it in your browser!
Greenfoot back
theDoctor
theDoctor wrote ...

2012/4/24

Changing Colors

theDoctor theDoctor

2012/4/24

#
I'm making a breakout game and trying to get it to where when the Ball hits a Brick, it changes to the color of the Brick it just hit. I need some help... Here's my Ball code: import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot) import java.awt.Color; import java.util.List; /** * A ball is an object that can hit other objects and bounce off the * edges of the world (except the bottom edge). It will bounce off of * a paddle as well. * * @author Barbara Ericson Georgia Tech * @version 1.0 April 6, 2007 */ public class Ball extends Actor { /** the radius of this ball */ private int radius = 10; /** the width of this ball (diameter) */ private int width = radius * 2; /** the color of the ball */ private Color color = Color.BLACK; /** the amount of change in x during each act */ private int velX; /** the amount of change in y during each act */ private int velY = 3; /** * Constructor that takes no arguments */ public Ball() { velX = Greenfoot.getRandomNumber(3) + 1; if (Greenfoot.getRandomNumber(2) == 0) velX = -1 * velX; updateImage(); } /** * Constructor that takes initial values for all fields * @param theRadius the radius to use * @param theColor the color to use * @param theVelX the amount to change in X per act * @param theVelY the amount to change in Y per act */ public Ball(int theRadius, Color theColor, int theVelX, int theVelY) { radius = theRadius; color = theColor; velX = theVelX; velY = theVelY; updateImage(); } /** * Balls will move and check if they have hit a brick or paddle or * one of the edges of the world */ public void act() { BreakoutWorld world = (BreakoutWorld) getWorld(); setLocation(getX() + velX, getY() + velY); Actor actor = this.getOneIntersectingObject(Actor.class); if (actor != null && !(actor instanceof Message)&& !(actor instanceof GreenPowerUp)&& !(actor instanceof BallX)) { velY = -velY; } if (actor instanceof Brick) { world.removeObject(actor); if(Greenfoot.getRandomNumber(5) == 1) { world.addObject(new GreenPowerUp(), this.getX(), this.getY()); } world.checkIfWon(); } if (getX() - radius <= 0) velX = -velX; if (getX() + radius >= BreakoutWorld.WIDTH) velX = -velX; else if (getY() - radius <= 0) velY = -velY; if (getY() + radius >= BreakoutWorld.HEIGHT) { world.removeObject(this); world.newBall(); } //checkHit() } /** * Method to set the ball color */ public void setColor(Color theColor) { this.color = theColor; updateImage(); } /** * Method to create the image and set it for the ball * If you change the ball width or color you should * invoke this again */ public void updateImage() { GreenfootImage image = new GreenfootImage(width,width); image.setColor(Color.BLACK); image.fillOval(0,0,width,width); setImage(image); } }
SPower SPower

2012/4/24

#
In the method which checks the hit, you need to change the color of the brick. If you post the code for that method, I can help you further.
IsVarious IsVarious

2012/4/24

#
I would place that code inside the "Brick" actor, and then you can setup how many times it takes to change the color of the brick being hit. Also, any brick you'll be hitting, you can store the code in there.
danpost danpost

2012/4/24

#
Add a public Color brickColor variable to the Brick class so when the Ball object hits one, it can get the color and set its image to that color (Tag! You're IT!)
ttamasu ttamasu

2012/4/24

#
Assuming that the brick is only an rectangular area that you filled with a certain color you need to add code like this to the Brick class: .... private boolean gotHit = false; private Color normalColor = Color.GREEN; private Color hiLight = Color.YELLOW; .. private void act(){ Ball ball = (Ball)getOneIntersectingObject(Body.class); if (gotHit && ball == null) { gotHit = false; GreenfootImage image = getImage(); image.color(normalColor); image.fill(); } else if (!gotHit && ball != null) gotHit = true; Greenfoot Image image = getImage(); image.color(hiLight); image.fill(); }
danpost danpost

2012/4/24

#
theDoctor wrote...
when the Ball hits a Brick, it changes to the color of the Brick
I think everyone who has responded on this thread ought to read more carefully.
danpost danpost

2012/4/24

#
In your act() method for the Ball, you have a section that already starts like the following, but this is what I believe you need after you add 'brickColor' to the Brick class..
if (actor instanceof Brick)
{
    Color c = ((Brick) actor).brickColor;
    updateColor(c); // create a method to change the color of the Ball object to 'c'
    world.removeObject(actor);
    etc. // continued as you have it
You need to login to post a reply.