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

2016/8/8

limit actor spawning

1
2
davemib123 davemib123

2016/8/8

#
Hi, I have this method in my world class to create "pipes":
pipeCounter++;      
        if (pipeCounter % 100 == 0)
        {
            createPipes();
        }
How do I get it to stop after several pipes are shown for a short time period before adding more "pipes"? Thanks in advance
danpost danpost

2016/8/8

#
davemib123 wrote...
How do I get it to stop after several pipes are shown for a short time period before adding more "pipes"?
One way is to limit the number of pipes in the world. For example, replace line 2 with this:
if (getObjects(Pipe.class).size() < 3 && pipeCounter%100 == 0)
davemib123 davemib123

2016/8/8

#
thanks for the suggestion. I've modified slightly to make it more random:
int numOfPipes = Greenfoot.getRandomNumber(7) + 6;
        pipeCounter++;      
        if (getObjects(Pipe.class).size() < numOfPipes && pipeCounter % 50 == 0)
        {
            createPipes();
        }
davemib123 davemib123

2016/8/8

#
say if I wanted to include a secondary obstacle in between the gap of 1 set of pipes and another how would I manage that? I've tried this:
 int numOfPipes = Greenfoot.getRandomNumber(7) + 6;
        pipeCounter++;      
        if (getObjects(Pipe.class).size() < numOfPipes && pipeCounter % 50 == 0)
        {
            createPipes();
            if(pipeCounter == 240 && getObjects(Pipe.class).size() < 6) System.out.println("add some object");
        }
but can't get anything in terminal to see if it even triggers. Could someone take a look and advise?
davemib123 davemib123

2016/8/8

#
should it be checked directly from the pipe object itself using an offset. This is my attempt at it:
 public void act() 
    {
        setLocation (getX() - pipeSpeed, getY() );
        
        boolean noObject = false;
        noObject = getOneObjectAtOffset(65, 0, Pipe.class) != null;
        if (noObject) 
        {  
            System.out.println("nothing"); 
        }  
        
         else if(getX() < - getImage().getWidth())
        {
            getWorld().removeObject(this);
        }
    }   
danpost danpost

2016/8/8

#
davemib123 wrote...
should it be checked directly from the pipe object itself using an offset?
I think the fact that the value of 'pipeCounter' will only be equal to '240' one time only and the chance of the number of pipes being '6' or more is quite good that you would be considered lucky if anything ever prints to the terminal. My logic stems from the fact that you would not have to ask if the number of pipes is less than '6' if none were initially placed in the world (the number added by this spawning process should only be '4' by the time the counter reaches '240', spawning at '50', '100', '150' and '200'). Maybe you should use a similar condition as the pipes themselves except with an offset:
if (pipeCounter%50 == 25)
for example. This would be triggered exactly halfway between the spawning of the pipe objects. EDIT: this was intended to be used with the code previous to your last code post.
davemib123 davemib123

2016/8/9

#
Not quite what I was looking for. here is the scenario: http://www.greenfoot.org/scenarios/17417 You will see that you have a set of pipes comes, a short gap and then some more pipes. I wanted to put something into the gap at random.
davemib123 davemib123

2016/8/9

#
I've tried this in the pipe method, but doesnt seem to work:
List noPipe = getObjectsAt(getWidth() + 150, 287, Pipe.class);
        if(!noPipe.isEmpty())  
       {  
           System.out.println("no pipe");
       }
Super_Hippo Super_Hippo

2016/8/9

#
Your code will print out 'no pipe' if a pipe is found at this specific location which will not happen because the location is outside the world and the pipes are created at the edge of the world. To the code in post #4. 'pipeCounter == 240' was never true because it was only executed when 'pipeCounter%50==0', so 0, 50, 100, and so on. I don't see a reason to count the number of pipes for a flappy bird remake.
danpost danpost

2016/8/9

#
To have an object spawn between the gaps at random (half the time), try:
if (pipeCounter%50 == 25 && Greenfoot.getRandomNumber(2) == 0)
davemib123 davemib123

2016/8/10

#
I've had a go at creating it. If a pipe is on the left and right then the condition is true. If the pipe is not on the left or the right the condition is false and an apple should appear. Does it look right - code wise?: http://www.greenfoot.org/scenarios/17422
Super_Hippo Super_Hippo

2016/8/10

#
You didn't publish the source code.
davemib123 davemib123

2016/8/10

#
try now. there is a 500px gap between 1 set of pipes and another. within the gap I was trying to add an apple.
danpost danpost

2016/8/10

#
If I understand what you want correctly, you need to change this part of your MyWorld act method:
if (getObjects(Pipe.class).size() < numOfPipes && pipeCounter % 50 == 0)
{
    createPipes();
}
to something like this:
if (pipeCounter%50 == 0)
{
    if (getObjects(Pipe.class).size() < numOfPipes)
    {
        createPipes();
    }
    else
    {
        createApples(); // method to be written
    }
}
Super_Hippo Super_Hippo

2016/8/10

#
The 'numOfPipes' condition is quite useless because the variable is set randomly every act cycle. If you want to create a random number of pipes in a set, you have to set this number once at/before the beginning of each set, so for example at the beginning and every time an apple is added. I changed the code a bit. Try it out and say if that is what you wanted. (I removed the prepare-method, let the pipes spawn outside the world and changed the act-method)
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class MyWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class MyWorld extends World
{
    private int pipeCounter = 0;
    private final int pipeSpacing = 150;
    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    public MyWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(1100, 400, 1, false);
        Greenfoot.start();
    }
    
    public void act()
    {   
        pipeCounter++;
        if (pipeCounter % 50 == 0)
        {
            if (pipeCounter == 300)
            {
                addObject(new Apple(), getWidth()+30, Greenfoot.getRandomNumber(getHeight()-10)+10);
                pipeCounter = 0;
            }
            else createPipes();
        }
    }
    
    private void createPipes()
    {
        // Create a pipe object
        Pipe topPipe = new Pipe();
        Pipe botPipe = new Pipe();


        GreenfootImage image = botPipe.getImage();

        addObject(botPipe, getWidth()+30, getHeight() / 2 + image.getHeight() - Greenfoot.getRandomNumber (100) - 10); 
        addObject(topPipe, getWidth()+30, botPipe.getY() - image.getHeight() - pipeSpacing);           
    }
}
There are more replies on the next page.
1
2