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

2015/6/8

Adding ONE actor during the game

ch3l53a29 ch3l53a29

2015/6/8

#
Hey guys, At the moment I´m creating a game like the classic "Arkanoid". After Hours of desperation i hope you guys can help me. I want to spawn a second ball after the first ball (which is already in the game) hits the barries (reihe4) to the 10th time. Here is my method: private int i = 1;
    public void ZweiterBall()
    {
        Reihe4 reihe4 = (Reihe4)getOneIntersectingObject(Reihe4.class);
        if( reihe4 != null )
        {
            i++;
        }

        if( i == 11)
        {
            Feld feld = (Feld) getWorld();
            feld.ZweiterBallHinzufügen();
        }
    }
Feld is my World and ZweiterBallHinzufügen(); is a World Mehtod:
    public void ZweiterBallHinzufügen()
    {
        addObject(new BallFürLevel2(), 261, 300);
    }
BallfürLevel2() is an Actor (second Ball which should be spawn). Now to the problem: This method spawns hundreds of balls but i just want ONE. how can i edit the mehtod to spawn just ONE instead of hundreds..? THANKS IN ADVANCE!.
danpost danpost

2015/6/8

#
Any time you want to limit, control, or otherwise restrict when an action is performed, you use an 'if' statement. In your case you only want to put a second ball in the world IF one has not been added yet (or if none is yet in the world).
public void ZweiterBallHinzufügen()
{
    if (getWorld().getObjects(BallFürLevel2.class).isEmpty())
    {
        addObject(new BallFürLevel2(), 261, 300);
    }
}
ch3l53a29 ch3l53a29

2015/6/9

#
Ok thank you very much!. If its okay, i would have another question. I want to speedup the speed of my ball after the ball hits the paddle 3 times or more.
    public void Special()
    {
        if( i == 3 )
        {
            Geschwindigkeit + 2;
            Geschwindigkeit2 + 2;
        }
    }
Geschwindigkeit and Geschwindigkeit2 are my speeds for the horizontal and vertical movement of the ball. i is a variable which counts in a other code the hits of the ball on the paddle. U see i tried in a very easy (stupid) way to add to the current Geschwindigkeit(5) and Geschwindigkeit2(7) 2. It didnt work (error msg= not a statement). Could u help me to find a way to speedup the movement after the third touch ?
danpost danpost

2015/6/9

#
I think you meant to use '+= 2' in lines 5 and 6, not '+ 2'. However, now you need to change something or add an extra condition so that the speeds do not continually increase until the ball hits the paddle a fourth time (where 'i' becomes 4 and the internal lines of the 'if' block will not longer execute).
ch3l53a29 ch3l53a29

2015/6/10

#
Ok once again Thank you very much but how or what could i change/add to stop the increase of the speeds? method looks like this now:
    public void Getroffen()
    {
        Reihe3 reihe3 = (Reihe3)getOneIntersectingObject(Reihe3.class);
        if( reihe3 != null )
        {
            i++;
        }
    }

    public void Special()
    {
        if( i == 3 )
        {
            Geschwindigkeit = + 2;
            Geschwindigkeit2 = + 2;
        }
    }
danpost danpost

2015/6/10

#
If you call Special from inside the 'Getroffen' method, it will only be called after the value of 'i' increases:
public void Getroffen()
{
    Reihe3 reihe3 = (Reihe3)getOneIntersectingObject(Reihe3.class);
    if (reihe3 != null)
    {
        i++;
        Special();
    }
}
ch3l53a29 ch3l53a29

2015/6/10

#
Thanks!. If its okay i would have another question. How can i write a method which counts the current number of objects in the world? i want to use it for a kind of counter. If the number is == 0 i want that the world sets a new world and remove the current.
danpost danpost

2015/6/10

#
ch3l53a29 wrote...
How can i write a method which counts the current number of objects in the world? i want to use it for a kind of counter. If the number is == 0 i want that the world sets a new world and remove the current.
Maybe you should see what methods are available here. Since you are concerned with the number of objects in the world, that would be the place to look.
ch3l53a29 ch3l53a29

2015/6/10

#
I already looked there. But the problem is that:
    public void ZweiterBall()
    {
        if (getObjects(Reihe4.class).isEmpty())
        {
            Greenfoot.stop();
        }
I wrote this mehtod. it should stop greenfoot if the Reihe4.class is empty (not any more in the world) but i doesnt stop. How i can edit this method in the way that if there is no Reihe4.class anymore greenfoot stops?
danpost danpost

2015/6/10

#
ch3l53a29 wrote...
I wrote this mehtod. it should stop greenfoot if the Reihe4.class is empty (not any more in the world) but i doesnt stop. How i can edit this method in the way that if there is no Reihe4.class anymore greenfoot stops?
Are you calling the method from the act method?
ch3l53a29 ch3l53a29

2015/6/10

#
Got me! Thanks!. It works. Have now this code int the world:
    public void Level4()
    {
        if (getObjects(Reihe4.class).isEmpty())
        {
            Level42();
        }
    }

    public void Level42()
    {
        Greenfoot.setWorld(new Feld4());
        //remove this world (Feld3)
    }
Now, If there are no Reihe4´s anymore greenfoot setting a new world. But how can i delete this current world?
danpost danpost

2015/6/11

#
ch3l53a29 wrote...
how can i delete this current world?
Once the current world is no longer active and there are no active references to it, it will automatically be flagged for garbage collection. Most, if not all, of that is done when you set the new world active (line 11).
You need to login to post a reply.