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

2012/6/18

mouse click on actor

gusbus123 gusbus123

2012/6/18

#
Hi i am having trouble getting the world to remove all objects and then adding the game objects to the world if the mouse clicked on this actor. Right now i am failing, this is my overall code for the actor being pressed.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;

public class Play1 extends Buttons
{
    private boolean selected;
    public void act() 
    {
        checkMouse();
    }
    public void checkMouse()
    {
        if (Greenfoot.mouseMoved(null)) {
            if (Greenfoot.mouseMoved(this) && !selected) {
                setImage(new GreenfootImage("1 player selected.png")); selected =true;
                enterGame();
            }
            if (!Greenfoot.mouseMoved(this) && selected) {
                setImage(new GreenfootImage("1 player.png")); selected = false;
            }
        }
    }
    public void enterGame()
    {
        MouseInfo press = Greenfoot.getMouseInfo();
        if(press!=null) {
            int button = press.getButton();
            if(button==1 && Greenfoot.mouseClicked(null)) {
                List all = getWorld().getObjects(Menu.class);
                int game = (Pacman) getWorld().menu;
                getWorld().addObjects(game);
                getWorld().removeObjects(all);
            }
        }
    }
}
Edit: The add object part does not work and removing the objects doesnt work as well. Oh and in this im trying to run a method inside the world and removing all the other items there are. Is this possible? or do i have to create two worlds and make it so it runs one world then removes the other?
gusbus123 gusbus123

2012/6/18

#
Ok iv got it working if the mouse is over the object but the check for button clicking isnt working this time.
private boolean selected;
    public void act() 
    {
        checkMouse();
    }
    public void checkMouse()
    {
        if (Greenfoot.mouseMoved(null)) {
            if (Greenfoot.mouseMoved(this) && !selected) {
                setImage(new GreenfootImage("1 player selected.png")); selected =true;
                enterGame();
            }
            if (!Greenfoot.mouseMoved(this) && selected) {
                setImage(new GreenfootImage("1 player.png")); selected = false;
            }
        }
    }
    public void enterGame()
    {
        MouseInfo press = Greenfoot.getMouseInfo();
        if(press!=null) {
            int button = press.getButton();
            if(button==1 && Greenfoot.mouseClicked(null)) {
                getWorld().removeObject(this);
                Greenfoot.setWorld(new SinglePlayer());
            }
        }
    }
the problem should be the area that checks for which button is pressed "if(button==1...)"
Edit: Dont worry fixed it... before the button clicking only checked it if the mouse was moving, and the mouseClicked only checks if it WASNT moving. so now it checks all the time.
private boolean selected;
    public void act() 
    {
        checkMouse();
    }
    public void checkMouse()
    {
        if (Greenfoot.mouseMoved(null)) {
            if (Greenfoot.mouseMoved(this) && !selected) {
                setImage(new GreenfootImage("1 player selected.png")); selected =true;
            }
            if (!Greenfoot.mouseMoved(this) && selected) {
                setImage(new GreenfootImage("1 player.png")); selected = false;
            }
        }
        enterGame();
    }
    public void enterGame()
    {
        MouseInfo info = Greenfoot.getMouseInfo();
        if(info!=null) {
            int clicked = info.getButton();
            if(Greenfoot.mouseClicked(this) && clicked==1) {
                getWorld().removeObject(this);
                Greenfoot.setWorld(new SinglePlayer());
            }
        }
    }
You need to login to post a reply.