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

2017/1/24

i need a two hit kill object help

ineedhelp ineedhelp

2017/1/24

#
working on a brick breaker game for class but I can't figure out how to add more hits to the brick pls help asap int health = 2; /** * Act - do whatever the SuperBrick wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { loseHealth(); broken(); } public void loseHealth() { Actor Ball = getOneIntersectingObject(Ball.class); if(Ball !=null) { health = health - 1; } } public void broken() { if(health == 1) { getWorld().removeObject(this); } }
danpost danpost

2017/1/24

#
We will need to see the codes for the Ball class, as well. Please use code tags when posting code.
Super_Hippo Super_Hippo

2017/1/24

#
Maybe change the '1' at the end to a '0'. You also have to make sure that it only activates once when touching the ball. Usually, the code of touching is in the moving object, so in your ball class. The Brick shouldn't really do anything on its own.
ineedhelp ineedhelp

2017/1/24

#
I fixed the 1 here is the ball code
ineedhelp ineedhelp

2017/1/24

#
private int deltaX;         // x movement speed
    private int deltaY;         // y movement speed
    private int count = 2;
    private boolean stuck = true;   // stuck to paddle
    World myWorld = getWorld();
    /**
     * Act. Move if we're not stuck.
     */

    public void act() 
    {
        if (!stuck) 
        {
            move();
            makeSmoke();
            checkOut();
        }

        Actor brick = getOneIntersectingObject(Brick.class);
        if(brick != null)
        {
            World myWorld = getWorld();
            myWorld.removeObject(brick);
            Board board = (Board)myWorld;
            Counter counter = board.getCounter();
            counter.addScore(); 
        }
        Actor superbrick = getOneIntersectingObject(SuperBrick.class);
        if(superbrick != null)
        {
           
        }
    }

    /**
     * Move the ball. Then check what we've hit.
     */
    public void move()
    {
        setLocation (getX() + deltaX, getY() + deltaY);
        checkPaddle();
        checkWalls();
        brickCheck();
    }

    /**
     * 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;
        }
    }

    /**
     * Check whether we're out (bottom of screen).
     */
    private void checkOut()
    {
        if (getY() == getWorld().getHeight()-1) {
           World myWorld = getWorld();
            myWorld.removeObject(this);
            Board board = (Board)myWorld;
            Counter counter = board.getCounter();
            counter.takeLives();           
        }
    }

    private void checkPaddle()
    {
        Actor paddle = getOneIntersectingObject(Paddle.class);
        if (paddle != null) {
            deltaY = -deltaY;
            int offset = getX() - paddle.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 = 1;
        }
    }

    /**
     * Release the ball from the paddle.
     */
    public void release()
    {
        deltaX = Greenfoot.getRandomNumber(15) - 5;
        deltaY = -5;
        stuck = false;
    }

    public void brickCheck()
    {
        Actor brick = getOneIntersectingObject(Brick.class); 
        if(brick != null)
        {
            deltaY = -deltaY;   
        }

    }
}

ineedhelp ineedhelp

2017/1/24

#
super brick code
public int health = 2;
    /**
     * Act - do whatever the SuperBrick wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        if(checkHit()) 
        {
            health = health - 1;
        }
        if(checkHealth())
        {
            getWorld().removeObject(this);
        }
    }

    public boolean checkHealth()
    {
        if(health == 0)
        {
         return true;   
        }
        return false;
    }
   
    public boolean checkHit()
    {
        if(isTouching(Ball.class))
        {
            return true;
        }
        return false;
    }
}
danpost danpost

2017/1/25

#
The only thing you need, other than the 'health' field, in the SuperBrick class is:
public void hit()
{
    health--;
    if (health == 0) getWorld().removeObject(this);
}
At line 31 in the Ball class above, you can then have:
((SuperBrick)superbrick).hit();
ineedhelp ineedhelp

2017/1/25

#
danpost I'm a little confused can u tell me what lines to take out of the Super Brick class
danpost danpost

2017/1/25

#
ineedhelp wrote...
danpost I'm a little confused can u tell me what lines to take out of the Super Brick class
import greenfoot.*;

public class SuperBrick extends Actor // or Brick or whatever
{
    public int health = 2;

    public void hit()
    {
        health--;
        if (health == 0)
        {
            getWorld().removeObject(this);
        }
    }
}
ineedhelp ineedhelp

2017/1/25

#
thank you so much for trying to help me but it still dies in one hit can you pls recheck to see if I'm missing anything
public class SuperBrick extends Actor
{
    public int health = 2;
    /**
     * Act - do whatever the SuperBrick wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void hit() 
    {
        health--;
        if(health == 0)
        {
            getWorld().removeObject(this);
        }
    }
}
 private int deltaX;         // x movement speed
    private int deltaY;         // y movement speed
    private int count = 2;
    private boolean stuck = true;   // stuck to paddle
    int totalBricks = 12; 
    World myWorld = getWorld();
    /**
     * Act. Move if we're not stuck.
     */

    public void act() 
    {
        if (!stuck) 
        {
            move();
            makeSmoke();
            checkOut();
        }
        hitBrick();
        hitSuper(); 
    }

    /**
     * Move the ball. Then check what we've hit.
     */
    public void move()
    {
        setLocation (getX() + deltaX, getY() + deltaY);
        checkPaddle();
        checkWalls();
        brickCheck();
    }

    /**
     * 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;
        }
    }

    /**
     * Check whether we're out (bottom of screen).
     */
    private void checkOut()
    {
        if (getY() == getWorld().getHeight()-1) {
            World myWorld = getWorld();
            myWorld.removeObject(this);
            Board board = (Board)myWorld;
            Counter counter = board.getCounter();
            counter.takeLives();           
        }
    }

    private void checkPaddle()
    {
        Actor paddle = getOneIntersectingObject(Paddle.class);
        if (paddle != null) {
            deltaY = -deltaY;
            int offset = getX() - paddle.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 = 1;
        }
    }

    /**
     * Release the ball from the paddle.
     */
    public void release()
    {
        deltaX = Greenfoot.getRandomNumber(15) - 5;
        deltaY = -5;
        stuck = false;
    }

    public void brickCheck()
    {
        Actor brick = getOneIntersectingObject(Brick.class); 
        if(brick != null)
        {
            deltaY = -deltaY;   
        }

    }

    public void hitBrick()
    {
        Actor brick = getOneIntersectingObject(Brick.class);
        if(brick != null)
        {
            World myWorld = getWorld();
            myWorld.removeObject(brick);
            Board board = (Board)myWorld;
            Counter counter = board.getCounter();
            counter.addScore();  
        }
    }
    
    public void hitSuper()
    {
      Actor superbrick = getOneIntersectingObject(SuperBrick.class);
        if(superbrick != null)
        {
            ((SuperBrick)superbrick).hit();
        }  
    }
}
danpost danpost

2017/1/25

#
You still need to bounce off the superbrick. You could insert the following after line 135:
deltaY = -deltaY;
but then there are other issues. Like, what if you intersect the brick from its side. Changing the vertical direction of movement does not change the horizontal one and you could continue inside the brick irratically (this is something you could try out for fun and learning). Because of this, there is a requirement to check movement along the vertical and horizontal separately so that you can tell whether to bounce horizontally or vertically. This also applies to the regular bricks and the paddles.
ineedhelp ineedhelp

2017/1/25

#
well, it's too late now I already submitted the program because it was due but thank you for all the help u provided for me. You are A wonderful person hope u have a nice day
You need to login to post a reply.