This site requires JavaScript, please enable it in your browser!
Greenfoot back
Fraser_Wakefield
Fraser_Wakefield wrote ...

2015/4/27

Looking to do a reload system, 6 Chamber revolver.

Fraser_Wakefield Fraser_Wakefield

2015/4/27

#
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
   }
davmac davmac

2015/4/27

#
i want it to reload automatically, no reload between chamber empties.
Do you mean you want it to reload automatically when the chamber becomes empty? Or something else? The bit I quoted above doesn't make sense. :)
azazeel azazeel

2015/4/27

#
line 101 if(timer==0 && Greenfoot.isKeyDown("space")) change into if(timer<=0 && Greenfoot.isKeyDown("space"))
Fraser_Wakefield Fraser_Wakefield

2015/4/27

#
Aye, when the chamber empties sorry
Super_Hippo Super_Hippo

2015/4/27

#
azazeel wrote...
line 101 if(timer==0 && Greenfoot.isKeyDown("space")) change into if(timer<=0 && Greenfoot.isKeyDown("space"))
Why? 'timer' is only decreased by 1 and only if it is greater than 0. So it will never become negative.
Fraser_Wakefield Fraser_Wakefield

2015/4/27

#
azazeel wrote...
line 101 if(timer==0 && Greenfoot.isKeyDown("space")) change into if(timer<=0 && Greenfoot.isKeyDown("space"))
I seems to work both ways mate :)
Fraser_Wakefield Fraser_Wakefield

2015/4/28

#
I think that I would need to have a counter within the counter right?
danpost danpost

2015/4/28

#
Fraser_Wakefield wrote...
I think that I would need to have a counter within the counter right?
You will need at least a counter to count the number of shots either fired or remaining. Depending on what needs done during reloading, you may need either another counter (a delay time counter) or a boolean (to signify reloading and use the shot counter as a delay counter also).
Fraser_Wakefield Fraser_Wakefield

2015/4/28

#
Thanks, any chance you could show me an example of that please?
danpost danpost

2015/4/28

#
You just need to add an int field to hold the current number of available shots. When a shot is fired, decrement the field. Then when you shoot the last shot, set the timer (to 30, 60, 120, whatever). You seem to already have the running of the timer and the condition that the timer be zero to shoot in place, so I think that is all you need to do. If you are not displaying the number of shots, you can set the value of shots back to size immediately (the timer will still cause the delay for reloading). If you are displaying it, you need to set it to six and adjust the display when both the timer and the shot counter are zero.
Fraser_Wakefield Fraser_Wakefield

2015/4/28

#
Ok thanks :)
You need to login to post a reply.