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

2013/11/4

AddObject by mouseclicking

1
2
Tioma7 Tioma7

2013/11/4

#
public void shoot(){ if(shootDelay > 0){ shootDelay--; } else{ if(Greenfoot.getMouseInfo() != null){ if(Greenfoot.getMouseInfo().getButton() != 0){ getWorld().addObject(new Bullet(),getX(),getY()); shootDelay = 30; } } } } i got this code for shooting by clicking the mouse but uf my mouse stays in one possition and hold mouse button it addObject only once, how to change to addobject rapidly?
danpost danpost

2013/11/4

#
You should probably use the 'mousePressed' and 'mouseClicked' methods, plus an int field to keep count of the number of buttons that are down. Allow shooting only when the value of the int field is not zero.
Tioma7 Tioma7

2013/11/4

#
danpost wrote...
You should probably use the 'mousePressed' and 'mouseClicked' methods, plus an int field to keep count of the number of buttons that are down. Allow shooting only when the value of the int field is not zero.
Sounds easy but i still dont get the right result :D
danpost danpost

2013/11/4

#
What did you try?
Tioma7 Tioma7

2013/11/4

#
if(shootDelay > 0){ shootDelay--; } else{ if(Greenfoot.getMouseInfo() != null){ if(Greenfoot.mousePressed(null) == true){ getWorld().addObject(new Bullet(),getX(),getY()); } } } i think its wrong because it shoots only 1 time, im bad at this :(
danpost danpost

2013/11/4

#
I do not see the int field or the use of the 'mouseClicked' method. All you did was change 'getMouseInfo' to 'mousePressed'. You need to incorporate what I suggested above. Let us call the instance int field that tracks the number of buttons down 'buttonsDown'. Then to control its value, increment it when 'mousePressed' is true and decrement it when 'mouseClicked' is true. Finally, only when its value is not zero should a new bullet be introduced into the world.
Tioma7 Tioma7

2013/11/4

#
danpost wrote...
I do not see the int field or the use of the 'mouseClicked' method. All you did was change 'getMouseInfo' to 'mousePressed'. You need to incorporate what I suggested above.
is there method mouseButtonIsDown? :D
danpost danpost

2013/11/4

#
Tioma7 wrote...
is there method mouseButtonIsDown? :D
No. That is why you need to keep track of presses and clicks. Presses are triggered at the time a mouse button is pressed and clicks are triggered when a mouse button is released.
Tioma7 Tioma7

2013/11/4

#
danpost wrote...
Tioma7 wrote...
is there method mouseButtonIsDown? :D
No. That is why you need to keep track of presses and clicks. Presses are triggered at the time a mouse button is pressed and clicks are triggered when a mouse button is released.
if(Greenfoot.mousePressed(null) == true){ if(Greenfoot.mouseClicke(null) == false){ getWorld().addObject(new Bullet(),getX(),getY()); } }
danpost danpost

2013/11/4

#
Where are you keeping track of the number of mouse buttons pressed? Where is the int field to hold that number? Review my last few posts, as you may have not seen an edited update of one of them.
Tioma7 Tioma7

2013/11/4

#
danpost wrote...
Where are you keeping track of the number of mouse buttons pressed? Where is the int field to hold that number? Review my last few posts, as you may have not seen an edited update of one of them.
how to check the number of mouse buttons pressed?>
danpost danpost

2013/11/4

#
danpost wrote...
Let us call the instance int field that tracks the number of buttons down 'buttonsDown'. Then to control its value, increment it when 'mousePressed' is true and decrement it when 'mouseClicked' is true. Finally, only when its value is not zero should a new bullet be introduced into the world.
This was in a previous posting.
Tioma7 Tioma7

2013/11/4

#
danpost wrote...
danpost wrote...
Let us call the instance int field that tracks the number of buttons down 'buttonsDown'. Then to control its value, increment it when 'mousePressed' is true and decrement it when 'mouseClicked' is true. Finally, only when its value is not zero should a new bullet be introduced into the world.
if i write this: if(Greenfoot.mousePressed(null) = true){ buttonsDown++; } buttonsDown wil increase by 1 only
danpost danpost

2013/11/4

#
Tioma7 wrote...
if(Greenfoot.mousePressed(null)=true){
For conditional checking, you need to use a double equal sign ( '==' ), like this:
if (Greenfoot.mousePressed(null) == true)
However, since the 'mousePressed' method returns a boolean value, you can just use:
if (Greenfoot.mousePressed(null))
If you write:
if (Greenfoot.mousePressed(null)) buttonsDown++;
if (Greenfoot.mouseClicked(null)) buttonsDown--;
Then buttonsDown will always hold a value that represents the number of mouse buttons that are down. If you use:
if (buttonsDown != 0 && shootDelay == 0)
for the conditions to shoot a bullet, it should do what you want.
Tioma7 Tioma7

2013/11/4

#
danpost wrote...
Tioma7 wrote...
if(Greenfoot.mousePressed(null)=true){
For conditional checking, you need to use a double equal sign ( '==' ), like this:
if (Greenfoot.mousePressed(null) == true)
However, since the 'mousePressed' method returns a boolean value, you can just use:
if (Greenfoot.mousePressed(null))
If you write:
if (Greenfoot.mousePressed(null)) buttonsDown++;
if (Greenfoot.mouseClicked(null)) buttonsDown--;
Then buttonsDown will always hold a value that represents the number of mouse buttons that are down. If you use:
if (buttonsDown != 0 && shootDelay == 0)
for the conditions to shoot a bullet, it should do what you want.
int buttonsDown = 0; if(Greenfoot.mousePressed(null) == true){ buttonsDown++; } if(Greenfoot.mouseClicked(null) == true){ buttonsDown--; } if(buttonsDown != 0){ getWorld().addObject(new Bullet(),getX(),getY()); } it still shoots only 1 bullet per button click
There are more replies on the next page.
1
2