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

2013/1/3

getActor() ?? Doesn't work

AnneMacaroni AnneMacaroni

2013/1/3

#
/**
     * check to see if the game has been stopped
     */
    public boolean isGameStopped()
    {
        if (actor != null)
        {
            if (actor == pause || actor == exit)
                return true;
            else
                return false;
        }
        else
            return false;
    }
    
    public void stopGame()
    {
        if (isGameStopped() == true)
        {
            Greenfoot.stop();
        }
    }
how come this doesn't work? I initialized it....
AnneMacaroni AnneMacaroni

2013/1/3

#
/**
     * get back any actor that the mouse is interacting with
     */
    public void getMouseActor()
    {
        if (mouseInfo != null)
        {
            actor = mouseInfo.getActor();
        }
    }

    /**
     * check to see if the game has been stopped
     */
    public boolean isGameStopped()
    {
        if (actor != null)
        {
            if (actor == pause || actor == exit)
                return true;
            else
                return false;
        }
        else
            return false;
    }
    
    public void stopGame()
    {
        if (isGameStopped() == true)
        {
            Greenfoot.stop();
        }
    }
}
a more detailed one
Gevater_Tod4711 Gevater_Tod4711

2013/1/3

#
It would be easyer to help you if you could tell us what exactly doesn't work and what this code should do.
danpost danpost

2013/1/3

#
If 'actor' is an Actor object, you cannot compare it to a boolean ('pause' or 'exit'), if that is what they are. It would help to know where you placed the code (class type) and what the data type of each variables is.
AnneMacaroni AnneMacaroni

2013/1/3

#
Ok, so basicly, I want that if I click on the EXIT or PAUSE button (objects), the game would stop. This entire code is put in the MyWorld class:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class dskf here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class MyWorld extends World
{
    private MouseInfo mouseInfo;
    private Actor actor;
    private Pause pause;
    private Exit exit;
    
    /**
     * Constructor for objects of class dskf.
     * 
     */
    public MyWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1);
        
        addPauseExitSigns();
        getMouseActor();
        stopGame();
    }
    
    public void addPauseExitSigns()
    {
        pause = new Pause();
        addObject(pause, 80, 30);

        exit = new Exit();
        addObject(exit, 30, 30);
    }

    /**
     * get back any actor that the mouse is interacting with
     */
    public void getMouseActor()
    {
        if (mouseInfo != null)
        {
            actor = mouseInfo.getActor();
        }
    }

    /**
     * check to see if the game has been stopped
     */
    public boolean isGameStopped()
    {
        if (actor != null)
        {
            if (actor == pause || actor == exit)
                return true;
            else
                return false;
        }
        else
            return false;
    }

    public void stopGame()
    {
        if (isGameStopped() == true)
        {
            Greenfoot.stop();
        }
    }
}
I don't get it... If getActor() returns any actor that the mouse interacts with, does it mean when you click on the object or your mouse moves over it? or both
danpost danpost

2013/1/3

#
One problem is that everything is processed in your world constructor. The only thing the world constructor should do is construct the world (initialize the cell and window size, and anything that needs done prior to the scenario being started (add objects, set up variables, and the such). Things like waiting for mouse clicks, keystrokes, etc. should be done in the 'act' method. Lines 26 and 27 should be moved into a 'public void act()' method. The 'act' method is something that is processed repeatedly (sort of like a built-in loop in your scenario) to do those things that continuously need done (you need to continuously check to see if the mouse was clicked, and if so, on which actor). BTW, there is a 'Greenfoot' method 'mouseClicked(Actor)' that you could use:
if(Greenfoot.mouseClicked pause)) { ... }
if(Greenfoot.mouseClicked(exit) { ... }
vonmeth vonmeth

2013/1/3

#
There are a few things you need to do: 1. Create an act method, and call your methods there, so they are executed each cycle. 2. Your Mouseinfo variable needs to be assigned to the current Mouseinfo object. Currently it will always be null. mouseInfo = Greenfoot.getMouseInfo(); will get the current Mouseinfo object. Put that in your getMouseActor method. I've never used getActor(), but I believe it would be both as according to the documentation: "Return the actor (if any) that the current mouse behaviour is related to. If the mouse was clicked or pressed the actor it was clicked on will be returned. If the mouse was dragged or a drag ended, the actor where the drag started will be returned. If the mouse was moved, it will return the actor that the mouse is currently over."
AnneMacaroni AnneMacaroni

2013/1/3

#
Thanks guys!! When I use Greenfoot.mouseClicked(actor) it worked!! it sorta lags a bit before fully stopping.... BUT THANKS :D:D
You need to login to post a reply.