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

2017/5/6

Automatic Gun

Kamichalik Kamichalik

2017/5/6

#
I am having issues with making an automatic weapon which is activated when the mouse is pressed. I have used the "while" code and other codes/ways to program this but either the gun is semi-automatic no matter what (when I hold the mouse button only 1 bullet is shot) or greenfoot just freezes and I have to restart it. And I will add a delay so there isn't a billion balls flying once this works.
1
2
3
4
5
6
7
if (Greenfoot.mouseClicked(null))
 var MouseInfo mouse = Greenfoot.getMouseInfo()
 int x = mouse.getX()
 int y = mouse.getY()
 Bullet bullet = new Bullet()
 getWorld().addObject(bullet,getX(),getY())
 bullet.turnTowards(x,y)
Super_Hippo Super_Hippo

2017/5/6

#
Try to set a toggle a variable whenever the mouse was pressed or released and add a bullet depending on the state of the variable.
Kamichalik Kamichalik

2017/5/6

#
Thanks, I did it.
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
Fields
 
private Boolean shoot = false
private int delay = 10
 
Act
 
delay = delay - 1
 
if (Greenfoot.mousePressed(null))
  shoot = true
end
 
if shoot == true
 if delay <= 0
  var MouseInfo mouse = Greenfoot.getMouseInfo()
  int x = mouse.getX()
  int y = mouse.getY()
  Bullet bullet = new Bullet()
  getWorld().addObject(bullet,getX(),getY())
  bullet.turnTowards(x,y)
  delay = 20
  end
 
if (Greenfoot.mouseClicked(null))
 shoot = false
end
You need to login to post a reply.