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?


1 2 3 4 5 6 7 8 9 10 11 | public boolean atWorldEdge() { if (getX() < 20 || getX() > getWorld().getWidth() - 20 ) { return true ; } if (getY() < 20 || getY() > getWorld().getHeight() - 20 ) { return true ; } else { return false ; } } |
1 | ((YourWorld) getWorld()).setBackground( "filename.png" ); //YourWorld has to be the classname of your world class. |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | 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 ; } } } |