Looking to do a 6 chamber revolver reload system, ie fires six shots, then a lengthy reload (say 3 seconds, to account for half a second per round) i know my timer approxes, just need help with the code. im newish to greenfoot. Ive included my current code, which includes a timer delay between shots already, its the full player class. i want it to reload automatically, no reload between chamber empties.
Thanks in advance for any help :)
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 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 | import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Player here. * * @author (your name) * @version (a version number or a date) */ public class Player extends Actor { //class level attributes private int prevX; private int prevY; private int setRotation; private int getRotation; private int delay; private int timer= 0 ; private int shoot; public byte GunX, GunY; //private int lives; /** * Act - do whatever the Player wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { // Add your action code here. storeCurrentPosition(); storeCurrentRotation(); checkMove(); //checkCollision(); checkWalls(); } //public int getTapes() //{ // return Tapes; //} //private void updateTapes(int change) //{ // Tapes += change; // updateImage(); //} private void storeCurrentPosition() { prevX = getX(); prevY = getY(); } // private void setRotation() // { //if(Greenfoot.getKey()=="a") // getRotation == setRotation(0); //if(Greenfoot.getKey()=="d") // getRotation == setRotation(180); //if(Greenfoot.getKey()=="w") // getRotation == setRotation(90); //if(Greenfoot.getKey()=="s") // getRotation == setRotation(270); //} private void storeCurrentRotation() { //getRotation = setRotation(); } //private void checkCollision() //{ //if(atWallsEdge(this)) // setLocation(prevX, PrevY); // if(!getObjectsInRange(35, Walls.class).isEmpty()) // setLocation(prevX, prevY); //} private void checkMove() { if (Greenfoot.isKeyDown( "A" )) setLocation(getX() - 1 , getY()); if (Greenfoot.isKeyDown( "D" )) setLocation(getX() + 1 , getY()); if (Greenfoot.isKeyDown( "W" )) setLocation(getX() , getY()- 1 ); if (Greenfoot.isKeyDown( "S" )) setLocation(getX() , getY()+ 1 ); if (Greenfoot.isKeyDown( "left" )) setRotation( 0 ); if (Greenfoot.isKeyDown( "up" )) setRotation( 90 ); if (Greenfoot.isKeyDown( "right" )) setRotation( 180 ); if (Greenfoot.isKeyDown( "down" )) setRotation( 270 ); if (timer> 0 ) timer--; if (timer== 0 && Greenfoot.isKeyDown( "space" )) { fireGun(); timer= 30 ; } //if(Greenfoot.getKey() =="A, D, S, W") // aimGun(); } //private void checkFire() //{ //if(timer>0)timer--; //if(timer==0 && Greenfoot.isKeyDown("space")) //{ // fireGun(); // timer=120; //} //} private void checkWalls() { Actor walls = getOneIntersectingObject(Walls. class ); if (walls != null ) { setLocation(prevX, prevY); } } public void fireGun() { //Projectile projectile = new Projectile(); //if ((Greenfoot.isKeyDown("space"))) //{ // ProjectileW projectileW = new ProjectileW(); // getWorld().addObject (projectileW, getX() -14, getY()); // // //rotations? //} if ((Greenfoot.isKeyDown( "left" ))) { GunX = - 1 ; ProjectileW projectileW = new ProjectileW(); getWorld().addObject (projectileW, getX() - 14 , getY()); timer= 60 ; //delay = timer+1000; //shoot = false; //rotations? } else if ((Greenfoot.isKeyDown( "right" ))) { GunX = + 1 ; ProjectileE projectileE = new ProjectileE(); getWorld().addObject (projectileE, getX() + 14 , getY()); timer= 60 ; //delay = timer+1000; //shoot = false; //to add, [REDACTED] rotations } else if ((Greenfoot.isKeyDown( "up" ))) { GunY = - 1 ; ProjectileN projectileN = new ProjectileN(); getWorld().addObject (projectileN, getX(), getY() - 15 ); timer= 60 ; //delay = timer+1000; //shoot = false; //rotations? } else if ((Greenfoot.isKeyDown( "down" ))) { GunY = + 1 ; ProjectileS projectileS = new ProjectileS (); getWorld().addObject (projectileS, getX(), getY() + 15 ); timer= 60 ; //delay = timer+1000; //shoot = false; //to add, [REDACTED] rotations } //if(delay <= timer) //{ // shoot = true; //} } //aimGun(); //consider adding a 'set aim to X' code on checkMove as well //end of method } |