i had the reload working but then i added a ammo limit and now it wont work here is the code for the class that the reload is in:
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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 | import greenfoot.*; import greenfoot.MouseInfo; public class Player extends Actor { // all the variables for the players stats public final int speed = 3 ; public int firerate = 20 ; int health = 100 ; int stamina = 10 ; int cash = 0 ; int wave = 1 ; int ammo = 20 ; public int pause = 100 ; int kills = 0 ; int survivedTime = 0 ; // creates the player character public Player(){ setImage( new GreenfootImage( 70 , 50 )); getImage().setColor(Color.YELLOW); getImage().fillOval( 0 , 0 , 50 , 50 ); getImage().setColor(Color.BLACK); getImage().fillRect( 50 , 20 , 70 , 10 ); } // tells the player character to use all the methods that have been created as long as the conditions are right for the given method public void act() { move(); turnAround(); reload(); shoot(); } // tells the actor to turn to face the mouse public void turnAround(){ button button = new button(); if (Color.BLACK.equals(button.getCurrentColor())){ if (Greenfoot.getMouseInfo() != null ){ turnTowards(Greenfoot.getMouseInfo().getX(), Greenfoot.getMouseInfo().getY()); } } } // tells the actor to do if the button is black and if it isnt black public void shoot() { button button = new button(); if (Color.BLACK.equals(button.getCurrentColor())){ manualShoot(); } else { autoShoot(); } } // returns the buttons color to determine what the firemode is public static int getColor() { return button.colorValue; } // one of the firemodes that is player skilled based private void manualShoot() { MouseInfo mouse = Greenfoot.getMouseInfo(); if (pause> 0 ){ pause --; } if (pause == 0 ){ if (ammo> 0 ){ if (!Greenfoot.mousePressed( null )) return ; turnTowards(mouse.getX(),mouse.getY()); Actor bullet = new bullet(); getWorld().addObject(bullet, getX(), getY()); bullet.turnTowards(mouse.getX(), mouse.getY()); GreenfootImage drawedImage = new GreenfootImage( "gunshot.png" ); //the image that is drawed; getImage().drawImage(drawedImage, 70 , 50 ); ammo--; } pause = firerate; } } // the second fire mode that is more of a auto fire to the nearest enemy private void autoShoot() { if (pause> 0 ){ pause --; } if (pause == 0 ){ if (ammo> 0 ){ if (getWorld().getObjects(Zombie. class ).isEmpty()) return ; Actor closest = null ; int closeness = 9999 ; for (Object obj : getWorld().getObjects(Zombie. class )) { Actor enemy = (Actor)obj; int dist = ( int )Math.hypot(getX()-enemy.getX(), getY()-enemy.getY()); if (dist < closeness) { closeness = dist; closest = enemy; } Actor bullet = new bullet(); getWorld().addObject(bullet, getX(), getY()); bullet.turnTowards(closest.getX(), closest.getY()); } GreenfootImage drawedImage = new GreenfootImage( "gunshot.png" ); //the image that is drawed; getImage().drawImage(drawedImage, 70 , 50 ); ammo--; pause = firerate; } } } // gets the users inputs and moves the player accordingly public void move(){ if (Greenfoot.isKeyDown( "W" )||Greenfoot.isKeyDown( "UP" )){ setLocation( getX(), getY()-speed); } if (Greenfoot.isKeyDown( "A" )||Greenfoot.isKeyDown( "LEFT" )){ setLocation( getX()-speed, getY()); } if (Greenfoot.isKeyDown( "S" )||Greenfoot.isKeyDown( "DOWN" )){ setLocation( getX(), getY()+speed); } if (Greenfoot.isKeyDown( "D" )||Greenfoot.isKeyDown( "RIGHT" )){ setLocation( getX()+speed, getY()); } } // tells the character to reload his gun when the user hits r private void reload(){ if ((Greenfoot.isKeyDown( "R" ))){ pause = firerate; if (pause> 0 ){ pause --; } if (pause == 0 ){ ammo = 20 ; } } } } |