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

2021/3/12

Can't spawn new Actors of a class after I removed them

hernry2812 hernry2812

2021/3/12

#
What I would like to do is basically to spawn 2 Actors of the Class Maulwurf on a specific color, which are getting removed after some time and after that I want 2 new Actors of the Maulwurf Class to spawn. When I start my program, 2 actors of Maulwurf are spawning in the right position, but after they are getting removed, there aren't spawning new ones. It would be really kind if someone could help me fixing my problem^^
public raceTrack()
    {    
        super(1000, 750, 1);

        while (getObjects(Maulwurf.class).size() < 2)
        {
            int xPos = Greenfoot.getRandomNumber(getWidth());  
            int yPos = Greenfoot.getRandomNumber(getHeight());  
            Color color = (this).getColorAt(xPos, yPos);    
            if (color.getRed() == 77 && color.getGreen() == 77 && color.getBlue() == 77){
                Maulwurf maulwurf = new Maulwurf();    
                addObject(maulwurf,xPos,yPos);
            }
        }


public class Maulwurf extends Actor
{   
    //Verschiedene Verzögerungszeiten werden festgelegt
    private int delay1 = 150;           
    private int delay2 = 130;
    private int delay3 = 105;
    private int delay4 = 95;
    private int delay5 = 90;
    private int delay6 = 250;
    public void act() 
    {   
        if (delay1 != 0) 
        {
            delay1--;
            return;
        }
        setImage("maulwurf_2.png");
        if (delay2 != 0) 
        {
            delay2--;
            return;
        }
        setImage("maulwurf_3.png");
        if (delay3 != 0) 
        {
            delay3--;
            return;
        }
        setImage("maulwurf_4.png");
        if (delay4 != 0) 
        {
            delay4--;
            return;
        }
        setImage("maulwurf_5.png");
        if (delay5 != 0) 
        {
            delay5--;
            return;
        }
        setImage("maulwurf_6.png");
        if (delay6 != 0) 
        {
            delay6--;
            return;
        }
        getWorld().removeObjects(getWorld().getObjects(Maulwurf.class));
    }
}
danpost danpost

2021/3/13

#
hernry2812 wrote...
What I would like to do is basically to spawn 2 Actors of the Class Maulwurf on a specific color, which are getting removed after some time and after that I want 2 new Actors of the Maulwurf Class to spawn. When I start my program, 2 actors of Maulwurf are spawning in the right position, but after they are getting removed, there aren't spawning new ones.
Move the while loop to an act method within the class.
hernry2812 hernry2812

2021/3/16

#
First of all thank you for helping me. Im new at programming, im just doing it for a school project. My Maulwurf Class now looks like this
    public void act() 
    {   
       [...]
        
        getWorld().removeObjects(getWorld().getObjects(Maulwurf.class));
        erschaffeMaulwurf();
    }
    public void erschaffeMaulwurf()
    {
        while (getWorld().getObjects(Maulwurf.class).size() < 2)
        {
            int xPos = Greenfoot.getRandomNumber(getWorld().getWidth());  
            int yPos = Greenfoot.getRandomNumber(getWorld().getHeight());  
            Color color = (this).getWorld().getColorAt(xPos, yPos);    
            if (color.getRed() == 77 && color.getGreen() == 77 && color.getBlue() == 77){
                Maulwurf maulwurf = new Maulwurf();    
                getWorld().addObject(maulwurf,xPos,yPos);
            }
        }
    }
What code should now be in my raceTrack World? When I simply add an Actor of the Maulwurf Class at specific coordinates it still doesnt spawn another one after it got removed. For testing if it works I tried the following code for my raceTrack World
public raceTrack()
    {    
        super(1000, 750, 1);
        
        Maulwurf maulwurf = new Maulwurf();
        addObject(maulwurf,300,300);
        
    }
I am now getting the following error:
danpost danpost

2021/3/16

#
hernry2812 wrote...
My Maulwurf Class now looks like this << Code Omitted >>
You incorrectly placed the while loop in the wrong act method. Put it back in the raceTrack class (in an act method).
danpost wrote...
Move the while loop to an act method within the class.
hernry2812 hernry2812

2021/3/19

#
Hey @danpost, thank you again for helping me. Your help really means a lot to me. Now I pasted the while loop to my raceTrack World. There are spawning 2 actors of the maulwurf class at the beginning (that works now), but there arent respawning actors of the maulwurf class after the first 2 get removed. My raceTrack world looks like this:
public class raceTrack extends World
{   
    public raceTrack()
    {    
        super(1000, 750, 1);
        
        [...]

        erschaffeMaulwurf();
    }
    public void erschaffeMaulwurf()
    {
        while (getObjects(Maulwurf.class).size() < 2) 
        {
            int xPos = Greenfoot.getRandomNumber(getWidth());  
            int yPos = Greenfoot.getRandomNumber(getHeight());  
            Color color = (this).getColorAt(xPos, yPos);    
            if (color.getRed() == 77 && color.getGreen() == 77 && color.getBlue() == 77){
                Maulwurf maulwurf = new Maulwurf();    
                addObject(maulwurf,xPos,yPos);
            }
        }
    }
}
Hope you or someone else can help me out with this.
danpost danpost

2021/3/20

#
Insert the following at line 8:
}

public void act()
{
danpost danpost

2021/3/20

#
OR: Remove lines 9 and replace line 11 with:
public void act()
hernry2812 hernry2812

2021/3/20

#
Thank you very much, it works now ;)
You need to login to post a reply.