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

2019/2/8

How to use mousePressed?

Fargesia Fargesia

2019/2/8

#
Hi guys! I'm a beginner in programmation and that's a pretty dumb question but I don't understand how to use the Greenfoot.mousePressed command. Like what do I have to put in the brackets? I know that I have to put the object but how do I write it?
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class pionBlanc here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class pionBlanc extends Actor
{
    /**
     * Act - do whatever the pionBlanc wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        GreenfootImage image = new GreenfootImage("pionBlanc.png");
        image.scale(35, 55);
        setImage(image);
        
        if(Greenfoot.mousePressed(pionBlanc.class)==true){
            setLocation(getX()+50, getY()+50);
        }
        
    }    
}
Here I just want to move it whenever I click on it but it won't do anything.
danpost danpost

2019/2/8

#
Fargesia wrote...
I don't understand how to use the Greenfoot.mousePressed command. Like what do I have to put in the brackets? I know that I have to put the object but how do I write it?
The Greenfoot class documentation says this:
Parameters: obj - Typically one of Actor, World or null
Most of the time, it will be 'null', meaning any click anywhere, or 'this', referring to the object, created from that class or one of its subclasses, that the current block of code is being executed on (or for). If not one of those, it will be a named reference to another Actor or World object.
You need to login to post a reply.