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

2019/1/6

Breakout issue

Quickske Quickske

2019/1/6

#
Hey all, i'm still making a breakout game and i want to do the following. ** when the ball hits the brick, return the ball in the opposite direction.
 Tegel mijnTegel = (Tegel)getOneIntersectingObject(Tegel.class);
        if(mijnTegel!=null) //when a brick has been found
        {
            intRichtingY=-intRichtingY; //change the Y direction into -Y
            mijnTegel.levens(intDamage); //substract a hitpoint from the brick
        }
This works fine if the ball hits the brick on the bottomside and the topside. When the ball hits the brick on the left side or the right side, i'd like it to keep going up or down (don't change the Y) but change the X direction. For example. The ball is coming from the left bottom corner, going to the right top corner. When hitting the brick on the left side, keep going up but change direction to the left top corner. i tried this with and getOneObjectAtOffset but it's giving me erros.
Tegel mijnTegelBoven = (Tegel)getOneObjectAtOffset(0,-26,Tegel.class);
        Tegel mijnTegelOnder = (Tegel)getOneObjectAtOffset(0,26,Tegel.class);
        Tegel mijnTegelLinks = (Tegel)getObjectsAtOffset(-26, 0,Tegel.class);
        Tegel mijnTegelRechts = (Tegel)getObjectsAtOffset(26,0,Tegel.class);
        if(mijnTegelBoven !=null)
        {
            intRichtingY=-intRichtingY;
           mijnTegelBoven.levens(intDamage);
        }
        else if (mijnTegelOnder!=null)
        {
          intRichtingY=-intRichtingY;
            mijnTegelOnder.levens(intDamage);
        }
        else if (mijnTegelLinks!=null)
        {
            intRichtingX=-intRichtingX;
            mijnTegelLinks.levens(intDamage);
        }
        else //(mijnTegelRechts!=null)
        {
            intRichtingX=-intRichtingX;
            mijnTegelRechts.levens(intDamage);
        }
the error that i'm getting is the following
consolescreen wrote...
java.lang.ClassCastException: java.util.ArrayList cannot be cast to Tegel at Speelbal.detectieBlock(Speelbal.java:70) at Speelbal.act(Speelbal.java:34) at greenfoot.core.Simulation.actActor(Simulation.java:567) at greenfoot.core.Simulation.runOneLoop(Simulation.java:530) at greenfoot.core.Simulation.runContent(Simulation.java:193) at greenfoot.core.Simulation.run(Simulation.java:183)
the entire code off the ball is the following
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Speelbal here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Speelbal extends Actor
{
    /**
     * Act - do whatever the Speelbal wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    private boolean blnIsDocked=true; //moment dat speelbal aangemaakt wordt, is hij docked
    private GreenfootSound ballTegenBalkSound = new GreenfootSound ("balTegenBalk.mp3");
    public int intRichtingX;//=getRandomX(0,11);//Greenfoot.getRandomNumber(11) - 5;//richting x word random gekozen 
    int intRichtingY = 5;//richting Y
    public int intDamage = 1; //verder uitwerking, meer damage

    private boolean blnReadyForRemove; //maakt volgens mij niet uit wanneer er waarde aangegeven word; in de constructor of bij aanmaak variabel

    public Speelbal()
    {
        blnReadyForRemove=false;
    }

    public void act() 
    {
        moveBal();
        checkBalk();
        checkWalls();
        detectieBlock();
        uitHetSpel();
        getRandomX(); //indien niet hier aangemaakt staat hij vast voor de rest van het spel. In act word hij iedere keer opnieuw aangemaakt. Bij verlies leven en opnieuw schieten heb je kans
        //op andere richting
        removeIfMarked();
    }    

    public void getRandomX()  //dees verwacht 2 integers uit methode "act"
    {
        if(intRichtingX==0)//zorgt er voor dat bal niet "recht omhoog" gaat. OM TE TESTEN !=0
        {
            //intRichtingX=Greenfoot.getRandomNumber(endRange-startRange)-startRange;
            intRichtingX=Greenfoot.getRandomNumber(11)-5;
        }

    }

    public void detectieBlock()
    {
        /*Tegel mijnTegel =(Tegel)getOneObjectAtOffset(-10,0,Tegel.class);
        if(mijnTegel!=null)
        {
        intRichtingY=-intRichtingY;
        mijnTegelOnderKant.levens(intDamage);
        }*/

        //Tegel mijnTegel = (Tegel)getOneIntersectingObject(Tegel.class);
        //if(mijnTegel!=null) //als er een tegel gevonden is
        //{
        //    intRichtingY=-intRichtingY;
        //    mijnTegel.levens(intDamage);
        //}
        
        
        Tegel mijnTegelBoven = (Tegel)getOneObjectAtOffset(0,-26,Tegel.class);
        Tegel mijnTegelOnder = (Tegel)getOneObjectAtOffset(0,26,Tegel.class);
        Tegel mijnTegelLinks = (Tegel)getObjectsAtOffset(-26, 0,Tegel.class);
        Tegel mijnTegelRechts = (Tegel)getObjectsAtOffset(26,0,Tegel.class);
        if(mijnTegelBoven !=null)
        {
            intRichtingY=-intRichtingY;
           mijnTegelBoven.levens(intDamage);
        }
        else if (mijnTegelOnder!=null)
        {
          intRichtingY=-intRichtingY;
            mijnTegelOnder.levens(intDamage);
        }
        else if (mijnTegelLinks!=null)
        {
            intRichtingX=-intRichtingX;
            mijnTegelLinks.levens(intDamage);
        }
        else //(mijnTegelRechts!=null)
        {
            intRichtingX=-intRichtingX;
            mijnTegelRechts.levens(intDamage);
        }
       

        //if(getY() <=mijnTegel.getY()+mijnTegel.getImage().getWidth()/2||getY() >=mijnTegel.getY()-mijnTegel.getImage().getWidth()/2)
        //{

        // }
        //else if(getX() >=mijnTegel.getX()+mijnTegel.getImage().getWidth()/2||getX() <=mijnTegel.getX()-mijnTegel.getImage().getWidth()/2)
        //als x loc van bal <= breedtetegel/2 of x van bal >= x tegel - breedtetegel/2
        //als x pos van bal links of rechts van tegel is

        /**
         * als bal aan linkse of rechtse kant komt van tegel, mag hij niet gewoon "terug keren" van waar hij komt
         * de linkse rechtse beweging moet omwisselen
         * als bal onderaan of bovenaan tegen tegel komt, moet de "boven//onder beweging omwisselen
         */
        //{
        //if(getY()>1)
        //{
        //       intRichtingX=-intRichtingX;

        //}
        //    mijnTegel.levens(intDamage);
        //}

        /*else if(getY() >=mijnTegel.getY()-mijnTegel.getImage().getWidth()/2)
        {
        intRichtingY=-intRichtingY;
        mijnTegel.levens(intDamage);
        }*/
        //}
    }

    public void checkBalk()
    {
        if(!blnIsDocked) //als bal niet gedocked is
        {
            // MyWorld mijnSpeelWereld=(MyWorld) getWorld();
            Balk mijnBalk = (Balk)getOneIntersectingObject(Balk.class);

            //mijnBalk=getOneObjectAtOffset(5,0,Balk.class);

            if(mijnBalk!=null) //als balk niet leeg is
            {
                //ballTegenBalkSound.play();
                //intRichtingY=-intRichtingY;
                //if(getX()>=mijnBalk.getX()-mijnBalk.getImage().getWidth()/2)

                //als x bal groter of gelijk aan is dan balk x - helftimage
                // 5
                //{
                //if(intRichtingX>getWorld().getWidth()/2)
                //{
                intRichtingY=-intRichtingY;
                intRichtingX=intRichtingX;//-1;
                //}
                //else
                //{
                //   intRichtingY=-intRichtingY+1;
                //  intRichtingX=intRichtingX;//+1;
                //}
                //}
                //else if (getX()<=mijnBalk.getX()-mijnBalk.getImage().getWidth()/2)
                //{
                //  if(intRichtingX<getWorld().getWidth()/2)
                // {
                //     intRichtingY=-intRichtingY-1;
                //     intRichtingX=intRichtingX;//-1;
                //}
                //else
                //{
                //   intRichtingY=-intRichtingY+1;
                //  intRichtingX=intRichtingX;//+1;
                //}
                //}
            }

        }
    }

    public void uitHetSpel()
    {
        if(getY()==getWorld().getHeight()-1)
        {
            MyWorld mijnSpeelWereld=(MyWorld) getWorld(); // refpunt wereld
            Leven mijnLeven=mijnSpeelWereld.getLevens(); // refpunt levens
            mijnLeven.lifeDown(); //methode oproepen
            mijnSpeelWereld.createBall(); //methode oproepen om ball te maken

            blnReadyForRemove=true; //ball klaarmaken voor remove
        }
    }

    public void checkWalls()
    {
        if(getX()==getWorld().getWidth()-1||getX()==0)
        {
            intRichtingX=-intRichtingX;
        }
        if(getY()==0)
        {
            intRichtingY=-intRichtingY;
        }
    }

    public void shoot()
    {
        blnIsDocked=false;//change boolean gelost naar true (bal is weg van balk)
    }

    public void moveBal()//move methode als bal niet meer vasthangt aan de balk
    {
        if(!blnIsDocked) //check om te zien dat ball NIET gedocked is
        {
            setLocation(getX()-intRichtingX,getY()-intRichtingY);
        }

    }

    /**
     * twee dezelfde methodes met dezelfde naam (hierboven en hieronder, nl moveBall) kunnen bestaan in zelfde class ZOLANG de parameters verschillend zijn
     * Bovenste is methode zonder verwachting, onderste is methode met verwachting (integer)
     */
    public void moveBal(int getMove)//move methode om de bal te moven als hij nog vasthangt aan de balk met een parameter voor de richting
    {
        if(blnIsDocked) //check om te zien da ball gedocked is
        {
            setLocation(getX()+getMove,getY());
        }
    }

    public boolean isRemoved()
    {
        return blnReadyForRemove;
    }

    public void removeIfMarked()
    {
        if(blnReadyForRemove)
        {
            getWorld().removeObject(this);
        }
    }
}
danpost danpost

2019/1/6

#
Quickske wrote...
Hey all, i'm still making a breakout game and i want to do the following. ** when the ball hits the brick, return the ball in the opposite direction. << Code Omitted >> This works fine if the ball hits the brick on the bottomside and the topside. When the ball hits the brick on the left side or the right side, i'd like it to keep going up or down (don't change the Y) but change the X direction. For example. The ball is coming from the left bottom corner, going to the right top corner. When hitting the brick on the left side, keep going up but change direction to the left top corner. i tried this with and getOneObjectAtOffset but it's giving me erros. << Code Omitted >>
Using getOneObjectAtOffset multiple times is not the way to go. Too many checks and awkward to work with. Once you get the side bouncing done, you will need to deal with corner hits -- will it be a side or a face hit? You will not be able to just choose one and be done with it (not some of the time). If you did just choose one, you could still end up intersecting the brick after the next move. Best is to use getOneIntersectingObject. Then use the size of the objects in conjunction with their locations to determine where the hit occurred (if abs(brick x - ball x) < half brick width) then top or bottom hit; which can be determined by sign of y speed of ball). Likewise for side hits. Corner hits could be treated similar to that of two balls colliding, where the corner location is the center of the other ball. You might consider using a smooth moving support class for the ball.
Quickske Quickske

2019/1/6

#
alright, so i shall be using getoneintersectingobject then and "calculating" it. Could you be so kind to expain what you mean by "if abs"? i know what the if is, but i'm scratching my head for the abs thingy. cheers!
danpost danpost

2019/1/6

#
Quickske wrote...
alright, so i shall be using getoneintersectingobject then and "calculating" it. Could you be so kind to expain what you mean by "if abs"? i know what the if is, but i'm scratching my head for the abs thingy. cheers!
if (Math.abs(brick.getX()-getX()) < brick.getImage().getWidth()/2)
abs is the unsigned value -- the absolute value.
You need to login to post a reply.