Hey I was wondering what would the code be if when an actor hits the right or lefts edge of the world it will change the background?
public boolean atWorldEdge() {
if(getX() < 20 || getX() > getWorld().getWidth() - 20) {
return true;
}
if(getY() < 20 || getY() > getWorld().getHeight() - 20) {
return true;
}
else {
return false;
}
}((YourWorld) getWorld()).setBackground("filename.png"); //YourWorld has to be the classname of your world class.import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class bob here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class bob extends Actor
{
/**
* Act - do whatever the bob wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
checkForArrowKeys(4);
}
public void checkForArrowKeys (int adjust)
{
if ( Greenfoot.isKeyDown("a") )
{
setLocation(getX() - adjust, getY());
}
if ( Greenfoot.isKeyDown("d") )
{
setLocation(getX() + adjust, getY() );
}
//if ( Greenfoot.isKeyDown("w") )
// {
// s//etLocation(getX() , getY() - adjust );
// }
//if ( Greenfoot.isKeyDown("s") )
// {
//setLocation(getX() , getY() + adjust );
//}
}
public boolean atWorldEdge() {
if(getX() < 20 || getX() > getWorld().getWidth() - 20) {
return true;
}
if(getY() < 20 || getY() > getWorld().getHeight() - 20) {
return true;
((Background) getWorld()).setBackground("lol.png"); //YourWorld has to be the classname of your world class.
}
else {
return false;
}
}
}