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

2019/4/9

mouseClicked(this) not working

TheFormalCorgi TheFormalCorgi

2019/4/9

#
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Turn here. * * @author (your name) * @version (a version number or a date) */ public class Turn extends Actor { private boolean P1Turn=true; public Turn() { setImage("Counter.png"); } /** * Act - do whatever the Turn wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { clickedOn(); } private void clickedOn() { if (Greenfoot.mouseClicked(this)) { if (P1Turn) { P1Turn=false; } else { P1Turn=true; } } } } So this should change the variable P1Turn from true to false or false to true when it is clicked but the variable doesn't change. Anyone know what I'm doing wrong?
danpost danpost

2019/4/9

#
First, pause the scenario and right-click on the actor and select 'Inspect' to see if you are actually clicking on the correct actor (verify type of actor by title line of inspect frame). Next, make sure you are not adding multiple Turn actors in the world (drag one while scenario is paused). If you think neither of these are the case, show your World subclass codes for reviewing.
TheFormalCorgi TheFormalCorgi

2019/4/9

#
Well I ran it again and realized it is changing just on every click, not just the ones on the object so it was changing once when I clicked it and once when I clicked pause (giving it the illusion that it wasn't changing). So does anyone know how to fix this?
TheFormalCorgi TheFormalCorgi

2019/4/9

#
Okay another update: Turns out it isn't changing every click but when I click it and when I click pause for some reason.
Super_Hippo Super_Hippo

2019/4/9

#
Try this out just to see it without inspecting. So no need to press pause:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
if (Greenfoot.mouseClicked(this))
{
    if (P1Turn)
    {
        P1Turn=false;
        getImage().setColor(Color.RED);
        getImage().fill();
    }
    else
    {
        P1Turn=true;
        getImage().setColor(Color.GREEN);
        getImage().fill();
    }
}
danpost danpost

2019/4/9

#
I wrote:
If you think neither of these are the case, show your World subclass codes for reviewing.
TheFormalCorgi TheFormalCorgi

2019/4/10

#
Thanks for the code Super_Hippo! That helped my figure out that when I was clicking inspect sometimes I was also clicking on the object (I feel dumb now) and that's why it seemed like the Pause button was changing it because when I clicked play it would register that click.
You need to login to post a reply.