I am trying to set these bricks to have to be broken twice (touch ball 2) but I tried the below code and it is not working, I also tried different booleans, variables, and it won't work. The bricks are being eliminated like the normal bricks that only need to be broken once to be removed from the world. Any ideas? :)
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Brick2 here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Brick2 extends Actor
{
public int size;
public GreenfootImage image1 = new GreenfootImage("brick2.png");
public GreenfootImage image2 = new GreenfootImage("brick3.png");
public boolean cracked = false;
public boolean cracked2 = false;
public int HIT = 2;
///**
// * Act - do whatever the Brick2 wants to do. This method is called whenever
// * the 'Act' or 'Run' button gets pressed in the environment.
// * one method is checked in act called checkBall();
// */
/**
* bricks gonna be bricks, but tougher.
* checks to see if it is intersecting with the ball
* if it is, then it changes to a cracked image.
* if it is on the cracked image and intersects with the ball again, then it removes itself.
*
*/
public void act()
{
Board board = (Board) getWorld();
if (isTouching(Ball.class))// && HIT ==2)
{
//this.cracked = true;
setImage(image2);
this.cracked = true;
//HIT--;
board.countScore((int)size*board.round/10);
}
if (isTouching(Ball.class) && cracked == true)//HIT ==1)
{
this.cracked2 = true;
getWorld().removeObject(this);
board.countScore((int)size*board.round/10);
//board.nextRound(1);
}
}
}
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* The ball of the game. It moves and bounces off the walls and the paddle.
*
* @author mik
* @version 1.0
*/
public class Ball extends Actor
{
private int deltaX; // x movement speed
private int deltaY; // y movement speed
private int count = 2;
int size;
public static int score = 0;
public static int levelScore = 0;
private Counter scoreKeeper;
private boolean stuck = true; // stuck to paddle
// public static boolean brokenBrick = false;
// public static boolean ultimateBall = false;
public static int timer = 251;
public int timerReset = 250;
/**
* Act. Move if we're not stuck.
*/
public void act()
{
if (!stuck)
{
move();
makeSmoke();
checkOut();
}
}
/**
* Move the ball. Then check what we've hit.
*/
public void move()
{
setLocation (getX() + deltaX, getY() + deltaY);
checkPaddle();
checkWalls();
checkBricks();
checkBricks2();
}
/**
* Check whether we've hit one of the three walls. Reverse direction if necessary.
*/
private void checkWalls()
{
if (getX() == 0 || getX() == getWorld().getWidth()-1) {
deltaX = -deltaX;
}
if (getY() == 0) {
deltaY = -deltaY;
turn(180);
}
}
/**
* Check whether we're out (bottom of screen). If we are, spawn a new ball on the paddle assuming we have lives left.
*/
private void checkOut()
{
Board board = (Board) getWorld();
if (getY() == getWorld().getHeight()-1) {
// for (int i=3; i>0; i--)
// {
((Board) getWorld()).ballIsOut();
getWorld().removeObject(this);
board.livesScore((int)size*board.round/10);
// BALL_COUNT = true;
}
// how can i get it to detect if lives = 0
// if (BALL_COUNT = false)
// {
// board.gameOver();
// }
}
// }
/**
* Check whether we are touching the paddle, and bounce off of it if we are.
*/
private void checkPaddle()
{
Actor paddle = getOneIntersectingObject(Paddle.class);
if (paddle != null) {
deltaY = -deltaY;//flips the direction on the y-axis
turn (180);
int offset = getX() - paddle.getX();
deltaX = deltaX + (offset/10);
if (deltaX > 7)
{
deltaX = 7;
}
if (deltaX < -7)
{
deltaX = -7;
}
}
}
/**
* check whether we are touching a brick, and bounce off it if we are (and remove the brick we touched)
* adds score if we remove a brick or hit a brick (not yet)
*/
private void checkBricks()
{
Actor brick = getOneIntersectingObject(Brick.class);
if (brick != null) {
deltaY = -deltaY;
turn(180);
int offset = getX() - brick.getX();
deltaX = deltaX + (offset/10);
if (deltaX > 7) {
deltaX = 7;
}
if (deltaX < -7) {
deltaX = -7;
}
}
}
/**
* check whether we are touching a brick of the other brick class (this was for making bricks that require 2 hits to destroy.
* One hit changed the image, the second hit removed it.
*/
private void checkBricks2()
{
Actor brick2 = getOneIntersectingObject(Brick2.class);
if (brick2 != null) {
deltaY = -deltaY;
turn(180);
int offset = getX() - brick2.getX();
deltaX = deltaX + (offset/10);
if (deltaX > 7) {
deltaX = 7;
}
if (deltaX < -7) {
deltaX = -7;
}
}
}
/**
* Move the ball a given distance sideways.
*/
public void move(int dist)
{
setLocation (getX() + dist, getY());
}
/**
* Put out a puff of smoke (only on every second call).
*/
private void makeSmoke()
{
count--;
if (count == 0) {
getWorld().addObject ( new Smoke(), getX(), getY());
count = 2;
}
}
/**
* Release the ball from the paddle.
*/
public void release()
{
deltaX =Greenfoot.getRandomNumber(11) - 5; //sets the intial angle when released from paddle
deltaY = -5; // controls the speed of the ball
stuck = false;
}
}
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * The game board. The board had a paddle that can move. * * @author mik * @version 1.0 */ public class Board extends World { private Paddle paddle; private Ball ball; private int startStars = 2500; private Lives livesCounter; private Counter scoreCounter; private Counter levelCounter; public int round = 1; public int BALL_COUNT = 3; public int niveau = 2; public boolean level4; // public boolean level1 = true; /** * Constructor for objects of class Board. * adds in the paddle, the counters etc. * */ public Board() { super(460, 520, 1); // all of these will be put in this order the first will be the top layer of the screen, the following will be under it like the asteroid going on top of the healthbar GreenfootImage background = getBackground();//use this to add text, the first line before the equal sign background.setColor(Color.BLACK); background.fill(); createStars(startStars); setPaintOrder ( Ball.class, Brick.class, Smoke.class, PowerUps.class ); paddle = new Paddle(); addObject ( paddle, getWidth() / 2, getHeight() - 40); ballIsOut(); livesCounter = new Lives("Lives: "); addObject(livesCounter, 430, 500); livesCounter.add(3); levelCounter = new Counter("Level: "); addObject(levelCounter, 255, 500); levelCounter.add(1); scoreCounter = new Counter("Score: "); addObject(scoreCounter, 70, 500 ); } /** * Add a given number of stars to our world. */ private void createStars(int number) { GreenfootImage background = getBackground(); for(int i = 0; i < number; i++) { int x = Greenfoot.getRandomNumber(getWidth()); int y = Greenfoot.getRandomNumber(getHeight()); int r = Greenfoot.getRandomNumber(256); int g = Greenfoot.getRandomNumber(256); int b = Greenfoot.getRandomNumber(256); background.setColor(new Color(r,g,b)); background.fillOval(x, y, 4 , 4); //the two two controls the size of my stars! //we add the same r value to make it gray if we would have many then we would get rainbow! } } public void started() { if (Ball.levelScore == 0 && getObjects(Brick.class).isEmpty()) buildBricks(2); } /** * counts the lives, everytime we hit the bottom it goes up by the int */ public void livesScore(int add) { // for (int i=3; i>0; i--) // { // if (BALL_COUNT != 0 && alive == true) // { livesCounter.add(-1); // BALL_COUNT--; // } // else if (BALL_COUNT == 0 && alive != true) // { // // alive = false; // //BALL_COUNT = 0; // gameOver(); // } } /** * to change the # of bricks after increase in levels, and give us our * level clear board and a delay after each level up. */ public void nextRound(int add) { if (niveau <4)// && normal==true) { round++; levelCounter.add(1); //removeObjects(getObjects(Brick.class)); removeObjects(getObjects(Ball.class)); removeObjects(getObjects(Smoke.class)); levelUp(); niveau++; //level4 = false; startlevel(); //normal = true; //addObjects(Ball.class); //addObjects(Smoke.class); } else if (niveau == 4) { round++; levelCounter.add(1); //removeObjects(getObjects(Brick.class)); removeObjects(getObjects(Ball.class)); removeObjects(getObjects(Smoke.class)); //level4 = true; levelUp(); niveau++; startlevel4(); } } /** * to change the # of bricks after increase in levels, and give us our * level clear board and a delay after each level up. */ public void startlevel() { // if (niveau <5) //&& level4 == false) // { Greenfoot.delay(110); removeObjects(getObjects(LevelBoard.class)); buildBricks(niveau+1); ///} } /** * to change the # of bricks after increase in levels, and give us our * level clear board and a delay after each level up. */ public void startlevel4() { //if (niveau == 5 && level4 == true) // { Greenfoot.delay(110); removeObjects(getObjects(LevelBoard.class)); level4(2); //} } /** * counts the score, everytime we hit a brick it goes up by the int */ public void countScore(int add) { scoreCounter.add(20); // score += add; } /** * checks to see what the score, is * checks to see what level it is * checks to see if cheat keys are pressed * checks to see if it is time to advance to a new level * picks random numbers and if it is a certain range it drops a power up */ public void act() { if(getObjects(Brick.class).isEmpty()) { nextRound(1); } } /** * checks to see if the ball is out */ public void ballIsOut() { paddle.newBall(); } /** * checks to see what level it currently is and if it needs to advance. */ public void checkLevel() { } /** * builds the bricks 1 by 1 for each of the levels */ public void buildBricks(int numberOfRows) { for (int y=0; y<numberOfRows; y++) { for (int x=0; x<4; x++) { addObject (new Brick(), 150+x*50, 100+40*y); //addObject (new Brick(), 75+i*50, 180+40*b); Greenfoot.delay(6); } } paddle.newBall(); } // /** // * builds the bricks 1 by 1 for each of the levels // */ // public void buildBricks2(int numberOfRows) // { // // Greenfoot.delay(110); // for (int b=0; b<numberOfRows; b++) for (int i=0; i<5; i++) // { // addObject (new Brick(), 125+i*50, 100+40*b); // addObject (new Brick(), 125+i*50, 140+40*b); // addObject (new Brick(), 125+i*50, 180+40*b); // Greenfoot.delay(6); // } // // addObject (new Ball(), (paddle.getX()), (paddle.getY()-20)); // } /** * separate method for a special level design */ public void level4(int numberOfRows) { for (int b=0; b<numberOfRows; b++) for (int i=0; i<5; i++) { addObject (new Brick2(), 125+i*50, 100+40*b); addObject (new Brick2(), 125+i*50, 140+40*b); addObject (new Brick2(), 125+i*50, 180+40*b); Greenfoot.delay(6); } paddle.newBall(); //Greenfoot.stop(); } /** * separate method for a level design with bricks that need to be hit twice. */ public void level5() { } /** * cheat keys to advance levels. */ public void cheats() { } /** * resets all factors involved with level advance. */ public void reset() { } /** * This method is called when the player has leveled up to display the new level. */ public void levelUp() { addObject(new LevelBoard(levelCounter.getValue()), getWidth()/2, getHeight()/2); //the two two controls the size of my stars! //we add the same r value to make it gray if we would have many then we would get rainbow! } // the coordinates div // TODO: show the score board here. Currently missing. /** * This method is called when the game is over to display the final score. */ public void gameOver() { removeObjects(getObjects(Brick.class)); removeObjects(getObjects(Ball.class)); removeObjects(getObjects(Smoke.class)); addObject(new ScoreBoard(scoreCounter.getValue()), getWidth()/2, getHeight()/2); // the coordinates div // TODO: show the score board here. Currently missing. } }