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

2014/12/9

Problem with Mouse Held Down

Arbitrage Arbitrage

2014/12/9

#
Hi, I'm creating a game where there is a top down player who yields a machine gun, I want it so that if the mouse is HELD down then it will fire, I don't want to use click since I don't want the player to click for every time that they want to shoot. My current code is:
1
2
3
4
5
6
7
8
9
10
11
12
boolean mouseIsDown;
if(mouse != null){
            if(Greenfoot.mousePressed(this)) 
                mouseIsDown = true
            else if(Greenfoot.mouseClicked(this)) 
                mouseIsDown = false
            if(mouseIsDown){
                /** make a bullet **/
                    weapCounter = 0;
                }
            }
        }
Any help will be greatly appreciated
danpost danpost

2014/12/9

#
If you are declaring your boolean field inside the method, then it will always be false going into the following code. And 'mousePressed' will only make it true when a mouse button goes from an up state to a down state (it does not continuously return true while the button is held down). The check for 'mouse != null' is not needed unless you actually need to execute a method on 'mouse' within the code (like getting the location of the click, determining which mouse button was used or determining what actor the mouse action was directed at).
Arbitrage Arbitrage

2014/12/9

#
danpost wrote...
If you are declaring your boolean field inside the method, then it will always be false going into the following code. And 'mousePressed' will only make it true when a mouse button goes from an up state to a down state (it does not continuously return true while the button is held down). The check for 'mouse != null' is not needed unless you actually need to execute a method on 'mouse' within the code (like getting the location of the click, determining which mouse button was used or determining what actor the mouse action was directed at).
Thank you for the quick reply, although it was quite informative, I still don't know more or less how to right the code, do I just move the boolean variable to the top of the class? Also could you provide some code for quick copy and pasting :P thanks
danpost danpost

2014/12/9

#
Just move line 1 outside the method brackets that it is currently enclosed in. If the code given was inside the act method then:
1
2
3
4
5
6
// outside any method would be like here;
boolean mouseIsDown;
// with respect to this method
public void act()
{
    if (Greenfoot.mousePressed(this)) ... // etc.
Arbitrage Arbitrage

2014/12/10

#
1
2
3
4
5
6
7
8
9
10
11
12
13
boolean mouseIsDown = false
    public void shoot(){
        if(Greenfoot.mousePressed(this)) 
            mouseIsDown = true
        else if(Greenfoot.mouseClicked(this)) 
            mouseIsDown = false
 
        if(mouseIsDown){
            if(weapCounter > reloadTime)
                    getWorld().addObject(new MyMissile(), getGlobalX(), getGlobalY());
            weapCounter = 0;           
        }
    }
Again Thank you for the quick response, i'm using the above code and it still doesn't work, is there anything wrong with it? Thank you for all your assistance.
danpost danpost

2014/12/10

#
What looks wrong is that you are resetting 'weapCounter' to zero on line 11, which as is, will be reset to zero any time the mouse button is down for as long as the mouse button is down. In other words, it does not look like it will ever increase to the value of 'reloadTime' until it is too late (if it is being incremented at all anywhere). You need to set it to zero when a new MyMissile is created; but because you did not enclose line 10 in curly brackets, line 11 is treated as outside the realm of the 'if' block that would be. Replace lines 9 through 11 with the following:
1
2
3
4
if  (weapCounter > reloadTime) {
    getWorld().addObject(new MyMissile(), getGlobalX(), getGlobalY());
    weapCounter = 0;
}
That may fix your dilemma (provided that 'weapCounter' is incremented somewhere).
Arbitrage Arbitrage

2014/12/10

#
Many Thanks danpost, however, Im encountering a new problem, it only shoots when I click on the barrel and when I move it out and let go, it shoots continuously, even when I let go Do I change the "This" in line 3 to something else? Thank you for your responses
danpost danpost

2014/12/10

#
Arbitrage wrote...
Do I change the "This" in line 3 to something else?
No. You probably need to change line 5 to this:
1
else if(Greenfoot.mouseClicked(this) || Greenfoot.mouseDragEnded(this))
You need to login to post a reply.