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

2018/3/16

Remove all the actors of a class

Aqurila Aqurila

2018/3/16

#
Hello, I am trying to make that when the player touches one of the spawned balls, all of them disappear. In the code that I have now, when they collide, only the player and the ball which hit the player disappear. But I want to do that all the balls, which have already spawned in the world, disappeard and do that every ball that spawns gets removed immediatly. Here's the code:
if (isTouching(Player1.class))
        {
            removeTouching(Player1.class);
            getWorld().removeObject(this);
        }
This in the Ball class btw. Thanks for the help! :)
danpost danpost

2018/3/16

#
There is a method in the World class that may help.
Aqurila Aqurila

2018/3/16

#
You mean the method which is creating the Balls?
int BallSpawnTimer;
  public void act(){
      
      runBallSpawnTimer();
    
    }
    
  private void runBallSpawnTimer(){
    BallSpawnTimer = (BallSpawnTimer+1)%1500;
    if (BallSpawnTimer == 0 ) {
        spawnBall();
     }
    }
   
  private void spawnBall(){
    int x;
    int y;
    int sign;
    GreenfootImage background;
    GreenfootImage ballImage;
    
    background = this.getBackground();
    
    Ball[] ball = new Ball[2];
    
    sign = 1;
    
     for (int i = 0; i < 2; i++){
        
        ball[i] = new Ball(sign, 1); 
        ballImage = ball[i].getImage();
        
        x = ballImage.getWidth() / 2 + Greenfoot.getRandomNumber(background.getWidth() - ballImage.getWidth()); 
        
        y = 50;

        //y = ballImage.getHeight() / 2 + Greenfoot.getRandomNumber(background.getHeight() - ballImage.getHeight() -150);
                                      
        this.addObject(ball[i], x, y);
        
        sign = -sign;
        
    }  
This is the only code that I have for the balls in the World.
danpost danpost

2018/3/16

#
Aqurila wrote...
You mean the method which is creating the Balls? << Code Omitted >> This is the only code that I have for the balls in the World.
No. I mean a method in among the methods provided here.
Aqurila Aqurila

2018/3/16

#
So if I use for example getWorld().removeObjects(java.util.Collection<? extends Actor> objects), I have to first create a list of objects?
danpost danpost

2018/3/16

#
Aqurila wrote...
So if I use for example getWorld().removeObjects(java.util.Collection<? extends Actor> objects), I have to first create a list of objects?
Yes. Find an additional method among those provided.
You need to login to post a reply.