Hi, I've a question:
I want to limit the munition, so the player can only shoot a limited number of times and the remaining munition is shown by a counter. I already archieved the ammo limitation with this code:
How can I connect the shooting method with the AmmoCounter to show how many bullets are left. I already tried to archieve this like this Tutorial: How to access one object from another?, but it was not possible to count backwards fro e.g. 15 to 0 because I was only getting compiling errors.
Another question is, how can I extend the number of available bullets by collecting additional ammo?
For the collection I used this code:
How can I reload the ammo?
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 | public class Ship extends Actor { public int ammo; public Ship { ammo= 15 ; } private boolean spaceDown; public void shoot(){ if (!spaceDown && Greenfoot.isKeyDown( "space" )) { if (Munition()== true ) { spaceDown= true ; getWorld().addObject( new Missile(), getX(), getY()); ammo--; } } if (spaceDown && !Greenfoot.isKeyDown( "space" )) { spaceDown= false ; } } public boolean Munition(){ if (ammo> 0 ){ return true ; } else { return false ; } } } |
1 2 3 4 5 6 | public void collectAmmo(){ Actor ammo = getOneObjectAtOffset( 0 , 0 ,Ammo. class ); if (ammo != null ){ getWorld().removeObject(ammo); } } |