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

2013/12/18

Random spawning my barrel actor

1
2
A\aron91 A\aron91

2013/12/18

#
Iam trying to random spawn 4 barrels from the right, wich works, and remove them when they hit the left edge of the world. But they only respawn when they hit another barrel. heres my code. Could someone help me with finding my fault?
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;

/**
 * Write a description of class barrel here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class barrel extends Actor
{
    
     /* Act - do whatever the barrel wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.*/
    

  

  public void act()
  
  {
      spawn();
  if(isgeraakt())
  {
      {
       int x = Greenfoot.getRandomNumber(getWorld().getWidth());
       int y = Greenfoot.getRandomNumber(getWorld().getWidth());
       getWorld().removeObject(this);
      
      
    }
  }
  else
  {
      int x = Greenfoot.getRandomNumber(2);
      move(-x);
  }

  
 }
  
 
  public boolean isgeraakt()
  {
    boolean raak = false;
    Actor r = getOneObjectAtOffset(0,0,boot.class);
    if ( r !=null)
    {
      raak = true;
    }
    return raak;
  }
  public void spawn()
  {
      List objectslookingfor = getWorld().getObjects(barrel.class);
      if(objectslookingfor.size() <= 3)
      {
          getWorld().addObject(new barrel(), 760, Greenfoot.getRandomNumber(760) + 450);
    
      } 

      Actor barrel;
      barrel = this.getOneObjectAtOffset(1, 1, barrel.class);
      
      if(barrel != null)
      {
          getWorld().removeObject(barrel);
          return;
      } 
    }
    
    public void ifAtWorldEdge()  
    {  
        if (atWorldEdge())  
        {  
             getWorld().removeObject(this);  
        }  
    }  
    
    public boolean atWorldEdge()  
{  
    return getX() == 0 ||   
               getX() == getWorld().getWidth() - 1 ||  
               getY() == 0 ||  
               getY() == getWorld().getHeight() - 1;  
} 
}
danpost danpost

2013/12/18

#
The 'act' method or any method it reaches does not deal with that issue at all, yet.
A\aron91 A\aron91

2013/12/18

#
do you know what i should do to remove the barrel if it hits the edge instead of another barrel? still new to greenfoot, and as iam playing around with my code. I see i have some code that i dont really need. Like the atWorldEdge right?
danpost danpost

2013/12/18

#
Your 'ifAtWorldEdge' method is coded for just that (to check for world edges and remove the object if at one). Just call that method from the 'act' method. However, you will then encounter the problem of two operations that remove the object from the world within the same act method which can cause errors if not dealt with properly (if removed in the first operation, the second one will not properly execute because it needs that actor to be in the world). 'isgeraakt' uses 'getOneObjectAtOffset and 'ifAtWorldEdge' calls 'atWorldEdge', which uses 'getX', 'getY' and 'getWorld', and itself uses 'getWorld'; every one of these 'get' methods require that the actor be in the world or an error will ensue. You could only execute the second operation after making sure the object is still in the world by checking if 'getWorld' returns 'null' or not. But, a simpler way is to remove the 'ifAtWorldEdge' method altogether and combining the 'atWorldEdge' condition with the 'isgeraakt' condition on line 23:
if (isgeraakt() || atWorldEdge())
{
    // the ints 'x' and 'y' have no meaning here
    getWorld().removeObject(this);
}
else
// etc.
A\aron91 A\aron91

2013/12/18

#
thanks for explaining! but now i dont get any spawning barrels anymore. This is my spawn code. Could you tell me where i gone wrong?
public void spawn()
    {
      if (Greenfoot.getRandomNumber(100) == 1) 
      {
          getWorld().addObject(new barrel(), 760, Greenfoot.getRandomNumber(760) + 450);
    
      }
    }
Here is my whole code just in case it went wrong somewhere else.
public void act()
  
   {
    spawn();
    if(isgeraakt() || atWorldEdge())
    {
       getWorld().removeObject(this);  
    }
    else
    {
      int x = Greenfoot.getRandomNumber(2);
      move(-x);
    }
    }
    
    public void spawn()
    {
      if (Greenfoot.getRandomNumber(100) == 1) 
      {
          getWorld().addObject(new barrel(), 760, Greenfoot.getRandomNumber(760) + 450);
    
      }
    }
    
  
 
    public boolean isgeraakt()
    {
     boolean raak = false;
     Actor r = getOneObjectAtOffset(0,0,boot.class);
     if ( r !=null)
     {
      raak = true;
     }
     return raak;
    }
       
    public boolean atWorldEdge()  
    {  
     return getX() == 0 ||   
               getX() == getWorld().getWidth() - 1 ||  
               getY() == 0 ||  
               getY() == getWorld().getHeight() - 1;  
    } 
}
danpost danpost

2013/12/18

#
The problem is not that they are not spawning; it is where you are spawning them at. Look at line 20, especially the y-coordinate at which spawning is to occur.
A\aron91 A\aron91

2013/12/18

#
Shouldnt this do the trick? if i wanted to start at 500px to 760px? cause they still arent appearing.
public void spawn()
    {
      if (Greenfoot.getRandomNumber(100) == 1) 
      {
          getWorld().addObject(new barrel(), 760, Greenfoot.getRandomNumber(500) + 260);
    
      }
    }
danpost danpost

2013/12/18

#
(1) because you are spawning them from the barrel class, you will need to first add a barrel to the world before spawning will take place; (2) if your world is only 500 pixels wide and you spawn your barrels at x = 760, they will be placed at the right edge of the world and then removed immediately. (3) the number of barrel is not limited to 4 as the spawn method is coded at this point; I am not sure that was intentional or not.
danpost danpost

2013/12/18

#
I would suggest that the spawning code be moved to the world class. You can use the condition 'getObjects(barrel.class).size() < 4' to determine when spawning is allowable and still have them randomly spawn by adding the condition 'Greenfoot.getRandomNumber(100) == 1'. By calling this action in the 'act' method of your world class, you do not have to initially add any barrels into the world.
A\aron91 A\aron91

2013/12/18

#
1. saw your answer. 2. My world is 760x760x1. what i mean with 500px to 760px is that they are y500px to y760px. my bad for not giving an x or y coordinate. 3. i did the 4 intentional cause i wanted to see if that works first before going further.
danpost danpost

2013/12/18

#
(1) if the number of barrels is not limited, then as long as a barrel is in the world, another will occasionally spawn; (2) I understand; however, you are spawning at x = 760 which will have it removed immediately cause it is a world edge; (3) I see.
A\aron91 A\aron91

2013/12/19

#
danpost wrote...
I would suggest that the spawning code be moved to the world class. You can use the condition 'getObjects(barrel.class).size() < 4' to determine when spawning is allowable and still have them randomly spawn by adding the condition 'Greenfoot.getRandomNumber(100) == 1'. By calling this action in the 'act' method of your world class, you do not have to initially add any barrels into the world.
how should i place the 'getObject(barrel.class).size() < 4' with the 'Greenfoot.getRandomNumber(100) == 1' in the act method in the world class? if i want to let them randomly spawn from the right side. should i use a while loop to let them appear?
danpost danpost

2013/12/19

#
If you want initially that all 4 barrels appear at the same time, then a loop ('while' or 'for') can be used in the world constructor. If you want them to appear at different times (with initially none in the world) then nothing needs to be coded in the world constructor. Either way, the act method of the world will need to check the number of barrels in the world and, possibly with a variable delay (the second condition 'Greenfoot.getRandomNumber(100) == 1'), to spawn new barrels. Spawn the barrels using the following:
addObject(new barrel(), 758, Greenfoot.getRandomNumber(500)+259);
I adjusted the coordinates to prevent adding the barrels into the world at a world edge.
A\aron91 A\aron91

2013/12/19

#
so i changed my spawning from the actor class to my world. It looks like this now. But there still arent any boats spawning. Could you point out my mistake here?
public class BotenUpgrade extends World
{
    private static final GreenfootImage bgImage = new GreenfootImage("Backgroundje.png");
    private static final int scrollSpeed = 1;

    private GreenfootImage scrollingImage;
    private int scrollPosition = 0;
    
   
    /**
     * Constructor for objects of class BotenUpgrade.
     * 
     */
    public BotenUpgrade()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(760, 760, 1); 

        GreenfootImage background = new GreenfootImage(760, 760);
        scrollingImage = getScrollingImage(760, 760);
        background.drawImage(scrollingImage, 500, 500);
        setBackground(background);

       
        setBackground("Backgroundje.png");
        
        prepare();
    }
    

    
    public void spawn()
    {
      if (Greenfoot.getRandomNumber(100) == 1) 
      {
          while (getObjects(barrel.class).size() < 4) {  
         addObject(new barrel(), 758, Greenfoot.getRandomNumber(500)+259);  
        }
      }
        
       
    }

    public void act()
    {
        if(scrollSpeed > 0 && scrollPosition <= 0) {
            scrollPosition = getWidth();
        }
        if(scrollSpeed < 0 && scrollPosition >= getWidth()) {
            scrollPosition = 0;
        }
        scrollPosition -= scrollSpeed;
        paint(scrollPosition);
    }

    /**
     * Paint scrolling image at given position and make sure the rest of
     * the background is also painted with the same image.
     */
    private void paint(int position)
    {
        GreenfootImage bg = getBackground();
        bg.drawImage(scrollingImage, position, 0);
        bg.drawImage(scrollingImage, position - scrollingImage.getWidth(), 0);
    }

    /**
     * Returns an image with the given dimensions.
     */
    private GreenfootImage getScrollingImage(int width, int height)
    {
        GreenfootImage image = new GreenfootImage(width, height);
        for(int x = 0; x < width; x += bgImage.getWidth()) {
            for(int y = 0; y < height; y += bgImage.getHeight()) {
                image.drawImage(bgImage, x, y);
            }
        }
        return image;
    } 



    /**
     * Prepare the world for the start of the program. That is: create the initial
     * objects and add them to the world.
     */
    private void prepare()
    {
        heart heart = new heart();
        addObject(heart, 142, 58);
        
    }
}
virginia001 virginia001

2013/12/19

#
Would you stop with copy/pasting code its really annoying
There are more replies on the next page.
1
2