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

2018/5/5

Spawn Actor

Applez Applez

2018/5/5

#
I want to make an actor that moves down and when it hits the bottom of the world, it spawns back up at the top. I have no idea how to make a code for this. Please help. Thanks.
jamesN616 jamesN616

2018/5/5

#
This could work;
if(getY() == getWorld().getHeight()-1)
{
    setLocation(getX() , 0);
}
else
{
    setLocation(getX() , getY() + speed);
}
The actor constantly sets its location lower until it reaches the bottom edge of the world, then resets itself back at the top.
Applez Applez

2018/5/5

#
Thank you so much! It worked! (with a minor change) The variable "speed" was not found
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)


    public void act() 
    {
  setLocation(getX(), getY()+5);
        if(getY() == getWorld().getHeight()-1)
{
    setLocation(getX() , 0);
}
else
{
    setLocation(getX() , getY());
}
    }    
}
danpost danpost

2018/5/5

#
You do not need this part:
else
{
    setLocation(getX() , getY());
}
You need to login to post a reply.