How can i code the ball to bounce off the worlds edge when it hits either the top,bottom right or left edge?


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | public void act() { move( 5 ); checkWalls(); } public void checkWalls(){ if (atWorldEdge()){ setRotation(getRotation()*- 1 ); } } public boolean atWorldEdge() { if (getX() < 10 || getX() > getWorld().getWidth() - 10 ) return true ; if (getY() < 10 || getY() > getWorld().getHeight() - 10 ) return true ; else return false ; } |
1 2 3 4 5 6 7 8 9 10 | private void shoot() { if ( "f" .equals(Greenfoot.getKey())) { Bullet bullet= new Bullet(); getWorld().addObject(bullet,getX(), getY()); bullet.setRotation(getRotation()); bullet.move( 75 ); } } |
1 2 3 4 5 6 7 8 9 10 | private void shoot() { if ( "p" .equals(Greenfoot.getKey())) { Bullet2 bullet2= new Bullet2(); getWorld().addObject(bullet2,getX(), getY()); bullet2.setRotation(getRotation()); bullet2.move( 75 ); } |
1 2 3 | String key = Greenfoot.getKey(); if ( "f" .equals(key)) // code to have Tank object shoot a Bullet if ( "p" .equals(key)) // code to have Tank2 object shoot a Bullet2 |
1 2 3 4 5 6 7 8 9 | // instance field private boolean shotKeyDown; // in act method if (!shotKeyDown && Greenfoot.isKeyDown( "f" )) { shoot(); shotKeyDown = true ; } if (shotKeyDown && !Greenfoot.isKeyDown( "f" )) shotKeyDown = false ; |
1 2 3 | String key = Greenfoot.getKey(); if ( "f" .equals(key)) // code to have Tank object shoot a Bullet if ( "p" .equals(key)) // code to have Tank2 object shoot a Bullet2 |
1 2 3 4 5 6 7 8 9 | // instance field private boolean shotKeyDown; // in act method if (!shotKeyDown && Greenfoot.isKeyDown( "f" )) { shoot(); shotKeyDown = true ; } if (shotKeyDown && !Greenfoot.isKeyDown( "f" )) shotKeyDown = false ; |