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

2013/1/17

reaction to left mouseclick

bonana bonana

2013/1/17

#
Hey, could someone please tell me how I call a method when the player clicks at a text in my world? Thanks
danpost danpost

2013/1/17

#
If the text is drawn on the background of the world then in the world class 'act' method or a method it calls:
if(Greenfoot.mouseClicked(this))
{
    MouseInfo mouse=Greenfoot.getMouseInfo();
    int mX=mouse.getX(), mY=mouse.getY();
    // with text top at 80, bottom at 100, left at 350, and right at 450
    if(mX>=350 && mX<=450 && mY>=80 && mY<=100) methodName();
}
If the text is on a button object, then in the 'act' method of the actor class:
if(Greenfoot.mouseClicked(this)) methodName();
bonana bonana

2013/1/17

#
Okay... And how do I set my text onto a button? :D How do actually make a button? :O
danpost danpost

2013/1/17

#
A simple Button class is very similar to a simple Text class (with the exception of having some extra methods, one being the 'act' method, and an instance boolean field). I provided code for a simple Text object here. Below is the code for a simple Button object.
import greenfoot.*;
import java.awt.Color;

public class Button extends Actor
{
    private boolean clicked;
    String buttonText = "";

    public Button()
    {
        this("");
    }

    public Button(String text)
    {
        setText(text);
    }

    public void setText(String text)
    {
        buttonText=text;
        GreenfootImage textImg=new GreenfootImage(" "+text+" ", 24, Color.black, new Color(0, 0, 0, 0));
        GreenfootImage image=new GreenfootImage(textImg.getWidth()+8, textImg.getHeight()+8);
        image.setColor(Color.darkGray);
        image.fill();
        image.setColor(Color.lightGray);
        image.fillRect(3, 3, image.getWidth()-6, image.getHeight()-6);
        image.setColor(Color.black);
        image.drawImage(textImg, (image.getWidth()-textImg.getWidth())/2, (image.getHeight()-textImg.getHeight())/2);
        setImage(image);
    }

    public void act()
    {
        if(Greenfoot.mouseClicked(this)) clicked=true;
    }

    public boolean gotClicked()
    {
        boolean wasClicked=clicked;
        clicked=false;
        return wasClicked;
    }

    public String getText()
    {
        return buttonText;
    }
}
In the world class, you will need
// an instance Button field
private Button textButton=null;
// creating the button
Button textButton= new Button("Click here to run method");
addObject(textButton, 500, 30);
// in the act to catch button click
if(textButton.getWorld() != null && textButton.gotClicked()) methodName();
Only use the first condition in the final 'if' statement if you plan on adding and removing the button from the world. I added the 'getText' method in case you wish to change the text after it was clicked on to say something else; but you will need to know which text is showing to determine what action to perform.
bonana bonana

2013/1/18

#
Thanks a lot Danpost! But I still got a problem. A part of my scenario is controlled by keyboard and a part by buttons and muse clicks. Every time I run my scenario only one of both controls works. When my keyboard control works the mouse control doesn't and vice versa. Do you have an idea of what may be wrong?
bonana bonana

2013/1/18

#
//EDIT
bonana bonana

2013/1/22

#
But I still got a problem. A part of my scenario is controlled by keyboard and a part by buttons and muse clicks. Every time I run my scenario only one of both controls works. When my keyboard control works the mouse control doesn't and vice versa. Do you have an idea of what may be wrong?
Can anybody help me please? Don't know what is wrong...
danpost danpost

2013/1/22

#
Could you upload your scenario so I could take a look (I will let you know when I have downloaded it so you can delete it off the site, if you wish).
bonana bonana

2013/1/22

#
doesn't upload... :O I'll try again later
danpost danpost

2013/1/22

#
You will need to upload it again -- this time check the 'Publish source code' checkbox.
bonana bonana

2013/1/22

#
fixed the problem myself :-)
You need to login to post a reply.