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

2013/6/9

breakout - ballcounter?

1
2
youjuli youjuli

2013/6/9

#
hey guys, i use greenfoot since 1 year - so don´t laught at me, i don´t know much:D. i want to do something like a ballcounter for the game breakout. in school, we haven´t had too much up to know, so no complicated codes please :). my ideas was something like three balls in the left corner, and so "if third ball is away - game over". but it just doesn´t work. is it possible on this way? and how can i remove an object at greenfoot? isn´t it (for the world) removeObject(Actor ball) e. g.? thank you so much :) i need it for school
danpost danpost

2013/6/9

#
You said you tried something and it did not work. Show the code you tried and maybe someone can help you fix it.
Zamoht Zamoht

2013/6/9

#
I'm sorry but I don't get the ball thing maybe you could try to be a little more clear about it or maybe someone else gets it. For the remove problem you have different ways to go around it. There are two methods for removing objects. The first one is removeObject(object); which deletes the passed object. Then you can use removeObjects(list of objects); which deletes all the objects in the list. If you want to delete objects from the same class you can do removeObjects(getObjects(ClassToDelete.class)); Remember that if you want to delete something from an actor you have to use the getWorld() method like this: getWorld().removeObjects(getWorld().getObjects(ClassToDelete.class)); I get it now and will work on some code for you in a minute.
youjuli youjuli

2013/6/9

#
but if i use "remove Object" there´s always the error "cannot find symbole"? the idea with the balls was that there are three of them and each time when space is pressed there disappears one of them. after the third time, it shall stop.
   if (Greenfoot.isKeyDown("space") &&
    getObjects(ball.class).size() == 0)
    {   
        {
        ball ball = new ball(); 
        addObject(ball, 320, 240);
       removeObject(ball1);
}
ball 1 and ball isn´t the same :)
Zamoht Zamoht

2013/6/9

#
Working on it. Your code is very wrong, but I will give you some code and explain.
danpost danpost

2013/6/9

#
Where did you get the name 'ball1'? Is it something you named yourself (do you have a field with that name that holds a ball object)?
youjuli youjuli

2013/6/9

#
apart from removeObject(ball1) everything worked (we did in school)
danpost danpost

2013/6/9

#
danpost wrote...
Where did you get the name 'ball1'? Is it something you named yourself (do you have a field with that name that holds a ball object)?
bump!
Zamoht Zamoht

2013/6/9

#
To be honest I don't he knows why he uses ball1 (no offense). Okay so I worked out some code: WorldCode:
public class BreakoutWorld extends World
{
    int attemptsCounter;
    Attempt attempts[] = {new Attempt(), new Attempt(), new Attempt()};
    boolean gameIsRunning;

    /**
     * Constructor for objects of class SoundWorld.
     * 
     */
    public BreakoutWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1); 
        
        setup();
    }
    
    public void setup()
    {
        attemptsCounter = 3;
        gameIsRunning = false;
        addObject(attempts[2], 100, 100);
        addObject(attempts[1], 180, 100);
        addObject(attempts[0], 260, 100);
    }
    
    public void act()
    {
        if (Greenfoot.isKeyDown("space") && gameIsRunning == false && attemptsCounter > 0)
        {
            gameIsRunning = true;
            
            attemptsCounter--;
            removeObject(attempts[2 - attemptsCounter]);
        }
    }
}
public class Attempt extends Actor
{
    /**
     * Act - do whatever the Attempt wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        // Add your action code here.
    }    
    
    public Attempt()
    {
        //set the image you want for the three balls
        setImage("ball.png");
    }
}
Please tell me what you get from this and what you don't get.
youjuli youjuli

2013/6/9

#
that´s a name of an class (actor)... ? confused...?
danpost danpost

2013/6/9

#
Instead of 'ball1', use 'getObjects(ball1.class).get(0).'
youjuli youjuli

2013/6/9

#
@zamoht: first of all, i am a she :D thank you for your help. so attempt is a new class? and it´s the ball-attempts, right? public void act() { if (Greenfoot.isKeyDown("space") && gameIsRunning == false && attemptsCounter > 0) { gameIsRunning = true; attemptsCounter--; removeObject(attempts); } } i dont get this part. :)
youjuli youjuli

2013/6/9

#
danpost wrote...
Instead of 'ball1', use 'getObjects(ball1.class).get(0).'
there is: method removeobject cannot be applied to given types? (confused?)
danpost danpost

2013/6/9

#
Ok, try this:
f (Greenfoot.isKeyDown("space") &&
    getObjects(ball.class).isEmpty())
{   
    ball ball = new ball(); 
    addObject(ball, 320, 240);
    if (!getObjects(ball1.class).isEmpty())
        removeObject(getObjects(ball1.class).get(0));
If this errors (which I do not believe it will), please show the entire error message.
Zamoht Zamoht

2013/6/9

#
Sorry about calling you a he. It's just almost only guys who program. Okay so the method act(). First of all the act method gets called frequently. First of all Greenfoot.isKeyDown("space") checks that space is pressed. Secondly gameIsRunning == false is a boolean meaning that it's a datatype which can be true og false. The gameIsRunning checks wether the game is running and by that I mean that a ball is in play, so basicly this secures that you can't spawn a new ball while another ball is in play (you should therefore add gameIsRunning = false to the place in your code where you check if the ball is lost and you have to spawn a new one). attemptsCounter > 0 checks how many attempts the player has left. gameIsRunning = true; Here I declare that the game is running because you spawned a new ball and therefore you should not be able to spawn another before the current ball is lost. attemptsCounter--; When you spawn a new ball by pressing the spacebar you lose an attempt. removeObject(attempts); This removes the attempt balls from right to left depending on how many attempts you have left. Okay a lot of code to go through (okay maybe not a lot), but I hope that at least some of what I said makes sense. A year ago I was the one asking questions here so I'm quite new at answering other peoples questions. I guess we are both practising on some level :)
There are more replies on the next page.
1
2