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

2012/3/12

mouseClicked reset?

TheNightStrider TheNightStrider

2012/3/12

#
I am having this problem. When i click, i want it to navigate somewhere. I have got that working, but i want it so that when i click again, the loop inside stops - so i naturally used if(Greenfoot.mouseClicked(null)){clicked=true;} and then if(clicked){stop();break;} That doesn't work. Using println, Greenfoot.mouseClicked(null) continusouly reads as true while I am moving. However, if(Greenfoot.isKeyDown("s")){clicked=true;} if(clicked){stop();break;} works completely fine. Anyway i can force mouseClicked to reset. Ps, already tried using mousePressed
davmac davmac

2012/3/12

#
If this is all in the space of one act() cycle, then no; the mouse info is gathered per cycle, so it always returns the same thing until you let the act() method return and the next cycle begin.
danpost danpost

2012/3/12

#
You can use
if (!betweenClicks && Greenfoot.mouseClicked(null))
{
    betweenClicks = true;
    // whatever actions you want to do when first click occurs
    return;
}
if (betweenClicks && Greenfoot.mouseClicked(null))
{
    betweenClicks = false;
    // whatever actions you want to do when second click occurs
    return;
}
This code will allow you to keep tabs on the clicking without maintaining a tight loop. During each act cycle, it will only check the mouseClick in the code where the betweenClicks check return true.
TheNightStrider TheNightStrider

2012/3/12

#
davmac wrote...
If this is all in the space of one act() cycle, then no; the mouse info is gathered per cycle, so it always returns the same thing until you let the act() method return and the next cycle begin.
Okay, thanks alot davmac! Appreicate it. Didn't know that, but thanks again!
TheNightStrider TheNightStrider

2012/3/12

#
danpost wrote...
You can use
if (!betweenClicks && Greenfoot.mouseClicked(null))
{
    betweenClicks = true;
    // whatever actions you want to do when first click occurs
    return;
}
if (betweenClicks && Greenfoot.mouseClicked(null))
{
    betweenClicks = false;
    // whatever actions you want to do when second click occurs
    return;
}
This code will allow you to keep tabs on the clicking without maintaining a tight loop. During each act cycle, it will only check the mouseClick in the code where the betweenClicks check return true.
Thanks for suggestion!
TheNightStrider TheNightStrider

2012/3/12

#
danpost wrote...
You can use
if (!betweenClicks && Greenfoot.mouseClicked(null))
{
    betweenClicks = true;
    // whatever actions you want to do when first click occurs
    return;
}
if (betweenClicks && Greenfoot.mouseClicked(null))
{
    betweenClicks = false;
    // whatever actions you want to do when second click occurs
    return;
}
This code will allow you to keep tabs on the clicking without maintaining a tight loop. During each act cycle, it will only check the mouseClick in the code where the betweenClicks check return true.
Tried out something very similar to that - it doesn't work, as both the if statements will read true due to the mouseClick only refreshing each act - i think changed the second one to an else if will do the trick
danpost danpost

2012/3/12

#
That cannot be the problem as betweenClicks must be true for one and false for the other. Therefore, it will only execute one of the Greenfoot.mouseClicked(null)s, because if the first one is executed, the return; statement ensures that the second part will not execute on that act cycle. (the 'else-if' is totally unneccessary). Post the code you tried.
danpost danpost

2012/3/12

#
Though, if you wanted to, you could change it to something like
if (Greenfoot.mouseClicked(null))
{
    if (!betweenClicks)
    {
        betweenClicks = true;
        // action to take on first click
    }
    else
    {
        betweenClicks = false;
        // action to take on second click
    }
}
TheNightStrider TheNightStrider

2012/3/13

#
danpost wrote...
Though, if you wanted to, you could change it to something like
if (Greenfoot.mouseClicked(null))
{
    if (!betweenClicks)
    {
        betweenClicks = true;
        // action to take on first click
    }
    else
    {
        betweenClicks = false;
        // action to take on second click
    }
}
that would work, but your original code wouldn't. You see, if betweenClicks was true, it would make it false and then also do the second click action. It HAS to be replaced with an else if
davmac davmac

2012/3/13

#
that would work, but your original code wouldn't. You see, if betweenClicks was true, it would make it false and then also do the second click action. It HAS to be replaced with an else if
The original code has a 'return' statement inside the conditional block, so it couldn't also do the second click action.
You need to login to post a reply.