Hi, I was wondering how I can set my actors to random locations when they hit the borders of the world
Thanks!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | public void bounceAtEdges( int width, int height) // Turns when hits edge { int worldX = getWorld().getWidth(); int worldY = getWorld().getHeight(); if (getY()==height) //top border { setLocation(getX() + Greenfoot.getRandomNumber( 20 ), getY() + Greenfoot.getRandomNumber( 20 )); } if (getX()==width) //left border { setLocation(getX() + Greenfoot.getRandomNumber( 20 ), getY() + Greenfoot.getRandomNumber( 20 )); } if (getX()==worldX-width) //right border { setLocation(getX() + Greenfoot.getRandomNumber( 20 ), getY() + Greenfoot.getRandomNumber( 20 )); } if (getY()==worldY-height) //bottom border { setLocation(getX() + Greenfoot.getRandomNumber( 20 ), getY() + Greenfoot.getRandomNumber( 20 )); } } |