Hello, I have been trying to make a body bounce when bouncing off an edge in Newton's Lab 3. This is to make multiple enities when they touch the edge, change colour. Here is the current code:
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 | private void bounceAtEdge() { if (getX() == 0 || getX() == getWorld().getWidth()- 1 ) { setLocation(( double )getX(), ( double )getY()); getMovement().revertHorizontal(); accelerate( 0.9 ); changeColor() } else if (getY() == 0 || getY() == getWorld().getHeight()- 1 ){ setLocation(( double )getX(), ( double )getY()); getMovement().revertVertical(); accelerate( 0.9 ); changeColor() } } public void changeColor() { int g = Greenfoot.getRandomNumber( 255 ); int r = Greenfoot.getRandomNumber( 255 ); int b = Greenfoot.getRandomNumber( 255 ); image.setColor (color); } /** |