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

2017/4/25

I need objects to spawn from one side of the screen

BrianOK BrianOK

2017/4/25

#
Making a run and jump and all I need is for objects to spawn in the same position but move along and out of the screen. i need it to spawn from the right and leave from the left.
danpost danpost

2017/4/25

#
BrianOK wrote...
Making a run and jump and all I need is for objects to spawn in the same position but move along and out of the screen. i need it to spawn from the right and leave from the left.
Is there a question here? Where is your attempted code? What specifically are you having difficulty with?
BrianOK BrianOK

2017/4/25

#
This is all I've done
 Pipe pipe = new Pipe();
        addObject(pipe, 620, 300);
        Pipe[] pipes = new Pipe[Greenfoot.getRandomNumber(getWidth())];
        
I need my pipe to randomly spawn on the left screen and move horizontal to the right so my actor can jump over it
Super_Hippo Super_Hippo

2017/4/25

#
I really have no idea what you are trying to do with line 3. Well, you will need an int variable as a timer. Increase (or decrease) it in the act method (maybe you have to add one to the world). When it reaches a certain value, spawn a pipe and reset the variable. To move the pipe to the left, all it needs is this:
public void act()
{
    setLocation(getX()-1, getY()); //change 1 to something higher for higher speed
    if (getX()<1) getWorld().removeObject(this); //remove the object when it reached the left side
}
BrianOK BrianOK

2017/4/25

#
its like the impossible game, my actor needs to jump over the pipe as it is coming from the right but i need to randomize it so they keep spawning at different rates
danpost danpost

2017/4/25

#
You will probably set a minimum time between spawns (or you can use the location of the last spawned pipe to prevent another from being placed to quickly). I think I would prefer the prevention way -- then use a random chance to vary the gaps. Your world can keep a reference to the last pipe spawned:
private Pipe lastSpawned;
Then, in the act:
int minimumGap = 100; // adjust to suit
if (lastSpawned == null || lastSpawned.getX() < getWidth()-minimumGap+20)
{
    if (Greenfoot.getRandomNumber(150) < 2)
    {
        lastSpawned = new Pipe();
        addObject(lastSpawned, getWidth()+20, 300);
    }
}
You need to login to post a reply.