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

2018/3/16

Calling a method from an actor to world

Aqurila Aqurila

2018/3/16

#
Hey, I am trying to make a second condition for a method in the world.
if (BallSpawnTimer == 0 /* && add condition -> method disappear*/) {
            spawnBall();
        }
In this condition I want to make a boolean which is testing if two actors (Player1, Ball) are intersecting. If they aren't the code shouldn't get executed because no other ball should spawn. Here the code that makes disappear the first wave of balls and the player:
public void disappear(){
        if (this.getOneIntersectingObject(Ball.class) != null)
        {
            if (!getWorld().getObjects(Ball.class).isEmpty())
            {
                getWorld().removeObjects(getWorld().getObjects(Ball.class));
            }
            getWorld().removeObject(this); 

        
Is it possible to call this method again and if the method is being executed, to make that the code for the ballspawn isn't getting executed? Or is there something to just check if the two actors are colliding? Thanks for the help
Super_Hippo Super_Hippo

2018/3/16

#
If you want to spawn a new ball when a ball was removed, then call the method to add a new ball from the disappear method. (So the other way around.)
//right before removing the object
((WorldName) getWorld()).spawnBall();
Or since you have a timer, it could look like this:
((WorldName) getWorld()).ballGone();
private boolean ballLost = false;

public void ballGone()
{
    ballLost = true;
}

//spawn new ball
if (BallSpawnTimer == 0 && ballLost)
{
    spawnBall();
    ballLost = false;
}
danpost danpost

2018/3/16

#
I have a feeling that all you need to do is make use of this in the ball spawning code:
if (getObjects(Ball.class).isEmpty())
or its opposite (using ! in front).
Aqurila Aqurila

2018/3/17

#
I guess danpost is right. But the thing is that when the game starts we have no ball at all so the class is empty, so how do we make an exception that the isEmpty() waits for the first balls to spawn?
Super_Hippo Super_Hippo

2018/3/17

#
What do you mean with waiting? Shouldn't it create one ball if there isn't one? Or is the spawning of the first ball "special"?
Aqurila Aqurila

2018/3/17

#
Ok we got it finally :) thanks
Aqurila Aqurila

2018/3/17

#
No, actually if there is no ball it should stop to create any new balls which spawn because the code is getting executed again because of the timer :D but we finally got a solution
You need to login to post a reply.