When I create the variable in line 34 I get this error: java.lang.IndexOutOfBoundsException: Index 0 out of bounds for length 0
How can I fix that?
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 60 61 62 63 64 65 66 67 68 69 | import lang.stride.*; import java.util.*; import greenfoot.*; /** * */ public class Crosshair extends Actor { private int gunReloadTimer = 999999999 ; private boolean gunIsEmpty = false ; /** * */ public void act() { MouseInfo followMouse = Greenfoot.getMouseInfo(); if (followMouse != null ) { setLocation(followMouse.getX(), followMouse.getY()); } this .gunShooting(); } /** * */ public void gunShooting() { Sky sky = (Sky)getWorld(); if ( this .gunReloadTimer >= 1 ) { this .gunReloadTimer = this .gunReloadTimer - 1 ; } GunBarrel gb = getWorld().getObjects(GunBarrel. class ).get( 0 ); //<--- ERROR if (Greenfoot.mousePressed( this ) && ! this .isTouching(Star. class ) && this .gunIsEmpty != true ) { Greenfoot.playSound( "shot.mp3" ); sky.shotsMissed = sky.shotsMissed + 1 ; this .gunReloadTimer = 75 ; this .gunIsEmpty = true ; this .setImage( "transper.png" ); if (gb != null ) { gb.setImage( "transper.png" ); } } if (Greenfoot.mousePressed( this ) && this .isTouching(Star. class ) && this .gunIsEmpty != true ) { Greenfoot.playSound( "shot.mp3" ); this .removeTouching(Star. class ); sky.starsShot = sky.starsShot + 1 ; this .gunReloadTimer = 75 ; this .gunIsEmpty = true ; this .setImage( "transper.png" ); if (gb != null ) { gb.setImage( "transper.png" ); } } if ( this .gunReloadTimer == 0 && this .gunIsEmpty == true ) { Greenfoot.playSound( "reload.mp3" ); this .setImage( "crosshair.png" ); this .gunIsEmpty = false ; this .gunReloadTimer = 999999999 ; if (gb != null ) { gb.setImage( "gunbarrel.png" ); } } if ( this .gunReloadTimer == 0 && this .gunIsEmpty == false ) { this .gunReloadTimer = 999999999 ; } } } |