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

2014/7/12

Differentiating between mouseMoved and mouseClicked

MontclairState MontclairState

2014/7/12

#
Hello I'm trying to have my actor shoot when I click the mouse. If I hold down the left click it won't keep shooting unless my mouse is moving. If the mouse stays in one spot with the left click held down nothing happens. How can I fix this? The code is as follows.
public class Player extends Actor
{
    int yVelocity;
    int xVelocity;
    MouseInfo mouse;
    
    public Player(){
        getImage().scale(50, 50);
    }
    
    
    public void act() 
    {
        mouse = Greenfoot.getMouseInfo();
        turnTowardsMouse();
        calcMotion(2, 1, 10);
        
        checkForFire();
    }    
    
    private void calcMotion(int accel, int decel, int speedCap){
        boolean isYMotion = false;
        boolean isXMotion = false;
        
        if(Greenfoot.isKeyDown("w") || Greenfoot.isKeyDown("up")){
            if(yVelocity > (speedCap * -1)){
                yVelocity -= accel;
            }
            isYMotion = true;
        }
        if(Greenfoot.isKeyDown("s") || Greenfoot.isKeyDown("down")){
            if(yVelocity < speedCap){
                yVelocity += accel;
            }
            isYMotion = true;
        }
        if(Greenfoot.isKeyDown("a") || Greenfoot.isKeyDown("left")){
            if(xVelocity > (speedCap * -1)){
               xVelocity -= accel;
            }
            isXMotion = true;
        }
        if(Greenfoot.isKeyDown("d") || Greenfoot.isKeyDown("right")){
            if(xVelocity < speedCap){
                xVelocity += accel;
            }
            isXMotion = true;
        }
        
        
        if(yVelocity < 0 && !isYMotion){
            yVelocity += decel;
        }else if(yVelocity > 0 && !isYMotion){
            yVelocity -= decel;
        }
        if(xVelocity < 0 && !isXMotion){
            xVelocity += decel;
        }else if(xVelocity > 0 && !isXMotion){
            xVelocity -= decel;
        }
    
        
        setLocation(getX() + xVelocity, getY() + yVelocity);
    }
    
    private void turnTowardsMouse(){
        if(mouse != null){
            turnTowards(mouse.getX(), mouse.getY());
        }
    }
    
    private void checkForFire(){
        boolean isMouseDown;
        
        /*
        if(mouse != null){
            isMouseDown = mouse.getButton() == 1;
        }else{
            isMouseDown = false;
        }
        */
       isMouseDown = Greenfoot.mouseDragged(null);
        
        if(Greenfoot.isKeyDown("space") || isMouseDown){
            getWorld().addObject(new Bullet(getRotation(), 1, 5), getX(), getY());
        }
    }
}
I'll also put the bullet's code even though it doesn't matter since the bullet is being created in the Player's code.
public class Bullet extends Actor
{
    int velocity = 1;
    int accel = 1;
    int maxSpeed = 10;
    
    public Bullet(int rotation, int x, int y){
        setRotation(rotation);
    }
    public void act() 
    {
        move(velocity);
        if(velocity < maxSpeed){
            velocity += accel;
        }
        int x = getX();
        int y = getY();
        int width = getWorld().getWidth();
        int height = getWorld().getHeight();
        if(x == 0 || x == width - 1 || y == 0 || y == height - 1){
            getWorld().removeObject(this);
        }
        
    }    
}
MontclairState MontclairState

2014/7/12

#
Is there a way to have something keep happening if a mouse button is held down? Like if I held down my space bar the shoots keep firing but not if I hold down a mouse button.
MusicPenguin MusicPenguin

2014/7/12

#
Try using this instead of Greenfoot.mouseDragged();
if (Greenfoot.mousePressed(null)){
            isMouseDown = true;
        }
        if (Greenfoot.mouseClicked(null)){
            isMouseDown = false;
        }
        if (isMouseDown || Greenfoot.isKeyDown("space")){
           getWorld().addObject(new Bullet(getRotation(), 1, 5), getX(), getY());
        }
MontclairState MontclairState

2014/7/12

#
MusicPenguin it still doesn't work. It fires when I click the mouse button but won't keep firing when I hold it down.
MontclairState MontclairState

2014/7/13

#
I feel like the problem with the code is
isMouseDown = Greenfoot.mouseDragged(null);
So, it returns false unless I'm dragging and if I made it mouseClicked instead it would return false unless I make a click. But, I don't understand why mousePressed doesn't make it true if I hold down the mouse button.
MontclairState MontclairState

2014/7/13

#
Sorry guys I just realized what was wrong. I was making the isMouseDown boolean false unless I clicked. Fixed it in the following.
public class Player extends Actor  
{  
    int yVelocity;  
    int xVelocity;  
    MouseInfo mouse;  
    boolean isMouseDown; 
      
    public Player(){  
        getImage().scale(50, 50);  
    }  
      
      
    public void act()   
    {  
        mouse = Greenfoot.getMouseInfo();  
        turnTowardsMouse();  
        calcMotion(2, 1, 10);  
          
        checkForFire();  
    }      
      
    private void calcMotion(int accel, int decel, int speedCap){  
        boolean isYMotion = false;  
        boolean isXMotion = false;  
          
        if(Greenfoot.isKeyDown("w") || Greenfoot.isKeyDown("up")){  
            if(yVelocity > (speedCap * -1)){  
                yVelocity -= accel;  
            }  
            isYMotion = true;  
        }  
        if(Greenfoot.isKeyDown("s") || Greenfoot.isKeyDown("down")){  
            if(yVelocity < speedCap){  
                yVelocity += accel;  
            }  
            isYMotion = true;  
        }  
        if(Greenfoot.isKeyDown("a") || Greenfoot.isKeyDown("left")){  
            if(xVelocity > (speedCap * -1)){  
               xVelocity -= accel;  
            }  
            isXMotion = true;  
        }  
        if(Greenfoot.isKeyDown("d") || Greenfoot.isKeyDown("right")){  
            if(xVelocity < speedCap){  
                xVelocity += accel;  
            }  
            isXMotion = true;  
        }  
          
          
        if(yVelocity < 0 && !isYMotion){  
            yVelocity += decel;  
        }else if(yVelocity > 0 && !isYMotion){  
            yVelocity -= decel;  
        }  
        if(xVelocity < 0 && !isXMotion){  
            xVelocity += decel;  
        }else if(xVelocity > 0 && !isXMotion){  
            xVelocity -= decel;  
        }  
      
          
        setLocation(getX() + xVelocity, getY() + yVelocity);  
    }  
      
    private void turnTowardsMouse(){  
        if(mouse != null){  
            turnTowards(mouse.getX(), mouse.getY());  
        }  
    }  
      
    private void checkForFire(){  
        if(Greenfoot.mousePressed(null))
        { 
            isMouseDown = true; 
        }
        if(Greenfoot.mouseClicked(null))
        {
            isMouseDown = false;
        }
        if(Greenfoot.isKeyDown("space") || isMouseDown){  
            getWorld().addObject(new Bullet(getRotation()), getX(), getY());  
        }  
    }  
} 
Moved the boolean isMouseDown out of the function and created a check to make the boolean true when the mouse is pressed and when I let go(click) it will make the boolean false.
You need to login to post a reply.