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

2015/1/18

I'm making Brick Breaker/breakout and my ball is only coliding with the top of the bricks

TTTT TTTT

2015/1/18

#
What am I doing wrong? This is my ball, most the code is in here.
import greenfoot.*;
import java.util.*;

// (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
 * Write a description of class Ball here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Ball extends Bar
{
    boolean start = true;
    Random r = new Random();
    int num = r.nextInt(2);
    Bar bar;

    public int X(){
        return getX();
    }

    public int Y(){
        return getY();
    }

    public void act() {
        if (start == true && Greenfoot.isKeyDown("space")){
            if (num == 1){
                turn(0);
                turn(315);
            }
            else{
                turn(0);
                turn (225);                
            }
            start=false;
        }

        if (start==false){
            move(1);
        }

        if (start == true && Greenfoot.isKeyDown("left")){
            move(-2);
        }
        if (start == true && Greenfoot.isKeyDown("right")){
            move(2);
        }
        endX();
        endY();
        bounceBar();
        bounceBrick();
        
    }

    public void endX(){//for left and right
        if (X()== 749){
            if (getRotation() == 315){
                turn(-90);
            }
            if (getRotation() == 45){
                turn(90);
            }
        }

        if (X()== 0){
            if (getRotation() == 225){
                turn(90);
            }
            if (getRotation() == 135){
                turn(-90);
            }
        }
    }

    public void endY(){ //for up and down
        if (Y()== 0){
            if (getRotation() == 315){
                turn(90);
            }
            if (getRotation() == 225){
                turn(-90);
            }
        }
        if (Y()== 659){
                System.out.println("You lose");
                Greenfoot.stop();
            
        }
    }

    public void bounceBar(){
        if (isTouching(Bar.class)){
            if (getRotation() == 315){
                turn(90);
            }
            if (getRotation() == 225){
                turn(-90);

            }
            if (getRotation() == 135){
                turn(90);

            }
            if (getRotation() == 45){
                turn(-90);

            }
        }

    }

    public void bounceBrick(){      
        if(isTouching(Brick.class)){
            if (getRotation() == 315){
                turn(90);
                removeTouching(Brick.class);
               
            }
            if (getRotation() == 225){
                turn(-90);
                removeTouching(Brick.class);
              
            }
            if (getRotation() == 135){
                turn(90);
                removeTouching(Brick.class);

            }
            if (getRotation() == 45){
                turn(-90);
                removeTouching(Brick.class);
            }
        }
    }
}
danpost danpost

2015/1/18

#
In your 'bounceBrick' method, if the check for a rotation of 315 is true and changes the rotation to 45, then the fourth check will be true and change the rotation back. Likewise, if the second check for 225 is true, then the rotation becomes 135 and the third check will change it back.
TTTT TTTT

2015/1/18

#
Thanks. I added a delay
Super_Hippo Super_Hippo

2015/1/18

#
You could just add an 'else' before the 'if' or do it like this:
if(isTouching(Brick.class))
{
    removeTouching(Brick.class);
    switch (getRotation())
    {
        case 45: case 225: turn(-90); break;
        case 135: case 315: turn(90); break;
    }
}
Or:
if(isTouching(Brick.class))
{
    removeTouching(Brick.class);
    int r = getRotation();
    if (r==45 || r==225) turn(-90);
    if (r==135 ||r==315) turn(90);
}
You need to login to post a reply.