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

2022/9/28

How do I get if the mouse is clicked in any part of the screen

SliceOfCheese SliceOfCheese

2022/9/28

#
I already have a way to check if the mouse is clickes, but if I click on another object, it doesnt work, since it only looks if the world is clicked. Im trying to make so that every time I click, the character shoots, but it can only shoot one shot per click.
protected void Attack()
    {
        if(Greenfoot.mouseClicked(getWorld()))
        {
            if(canAttack)
            {
                getWorld().addObject(new Bala(getRotation()), getX(), getY());
                Greenfoot.playSound("gunshotSoundEffect.mp3");
                canAttack= false;
            }
        }
        else
        {
            canAttack= true;
        }
    }
I already tried using the
mouse.getButton() == 1
way, but it turns out even if I put the limitant if it can shoot or not, it basically becomes a laser, since I leave it clicked, and keeps shooting. Thanks @danpost in advance, jajaja
SliceOfCheese SliceOfCheese

2022/9/28

#
I already solved it, I readed the documentation and turns out that if for the funcion Greenfoot.mouseClicked(), you give a null parameter every click is registered, and by that, every time you click it return a true, doesnt matter where, as long as it is on screen obviously. Also other thing I didnt realize at first, is that by the way the function works, I do not have to check if the click has been released, since the function checks it. Basically what the description says, is that the function returns true, if the mouse has been pressed and subsequently released, so it does not return a True, until the release of the click. For this, me code ended up like this:
protected void Attack()
    {
        if(Greenfoot.mouseClicked(null))
        {
            getWorld().addObject(new Bala(getRotation()), getX(), getY());
                Greenfoot.playSound("gunshotSoundEffect.mp3");
        }
    }
It works just as I wanted to
akbar202e akbar202e

2022/9/29

#
thanks for the solution buddy, i just have the same problem :D
You need to login to post a reply.