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

2017/6/10

Help with mouseClicked().

EthanLettuce EthanLettuce

2017/6/10

#
I'm trying to use Greenfoot.mouseClicked and itisnt working. I have a boolean that is set to false by default, and when you click on the object it should set the start boolean to true. All of this is in an actor called ball. I have added action to the world same with ball. I'm not sure why it isn't working. This is a simple idea.
        if(Greenfoot.mouseClicked(Action.class))
            start = true;
Super_Hippo Super_Hippo

2017/6/10

#
It needs an object as the parameter, so you need a reference to the action object. Or, you would do it the other way around:
//in Action
public void act()
{
    if (Greenfoot.mousePressed(this)) //I usually use mousePressed, try it yourself
    {
        ((Ball) getWorld().getObjects(Ball.class).get(0)).setStart(true); //works if there is always exactly one ball in the world when this action object can be clicked
    }
}
//in Ball
public void setStart(boolean newStart)
{
    start = newStart;
}
EthanLettuce EthanLettuce

2017/6/10

#
It worked. Thanks.
EthanLettuce EthanLettuce

2017/6/10

#
On another note, some of the colours of the images I'm setting aren't showing up, and are just staying black. For example I have a rectangle named play that I want to be dark gray. Here's it's constructor and its addition to the world.
    public Play(int width, int height, Color color){
        GreenfootImage play = new GreenfootImage(width, height);
        play.fillRect(0, 0, width, height);
        play.setColor(color);
        setImage(play);
    }
        Play button = new Play(300, 150, (new Color(64, 64, 64, 255)));
        addObject(button, 535, 550);
I have tried using (Color.DARK_GRAY) and that hasn't worked either.
Super_Hippo Super_Hippo

2017/6/10

#
You have to set the color before you want to draw something in that color. It's the same as in real life. You take the red pen before you want to draw red in your image, not the other way around ;)
EthanLettuce EthanLettuce

2017/6/10

#
That makes so much sense, thank you lord and saviour Super_Hippo.
You need to login to post a reply.