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

2017/6/16

Mouse Clicked on Actor?

Katht99 Katht99

2017/6/16

#
Here's my code. For some parts it works fine but for other's when it I say if(Greenfoot.mouseClicked(continueButton)) nothing happens. Am I missing something in it?
public class ContinueButton extends Actor
{
    private GreenfootImage continueButton;
    public void ContinueButton()
    {
        continueButton = new GreenfootImage("ContinueButton.png");
        this.setImage(continueButton);
    }

    public void act() 
    {
        if(Greenfoot.mouseClicked(this))
        {
            if(getWorld() instanceof ReadLetter)
            {
                //if the user is in world ReadLetter and the mouse is clicked on the continue button
                //it sets the world to the next world
                Greenfoot.setWorld(new BedRoom());
            }
            else if(getWorld() instanceof BedRoom)
            {
                /*If the user is in world BedRoom and the mouse is clicked on the continue button
                 *it removes the continue button and sets a new background. The game will wait until
                 *the user clicks on the note on the bed and sets the world to the next world*/
                getWorld().setBackground("Dialogue2.png");
                if(getWorld().getBackground().equals("Dialogue2.png"))
                {
                    ContinueButton continueButton = new ContinueButton();
                    getWorld().removeObject(continueButton);
                    //If background is Dialogue2.png then remove the continue button
                }
                Note note = new Note();
                getWorld().addObject(note,518,310);
                if(Greenfoot.mouseClicked(note))
                {
                    Greenfoot.setWorld(new NoteZoom());
                }
            }
            else if(getWorld() instanceof NoteZoom)
            {
                Greenfoot.setWorld(new BedRoomTwo());
            }
            else if(getWorld() instanceof BedRoomTwo)
            {
                if(getWorld().getBackground().equals("PicturesZoomDialogue.png"))
                {
                    if(Greenfoot.mouseClicked(this))
                    {
                        Greenfoot.setWorld(new River());
                    }
                }
            }
            else if(getWorld() instanceof SuitCase)
            {
                getWorld().setBackground("SuitcaseDialogue2.png");
                if(Greenfoot.mouseClicked(continueButton))
                {
                    getWorld().setBackground("Suitcase.png");
                    getWorld().removeObject(this);
                }
            }
        }
    }
}
Yehuda Yehuda

2017/6/16

#
You say that nothing happens but is there perhaps a(n unmentioned) compiling error? (You can't call mouseClicked on a GreenfootImage, and even if the argument was an actor it probably still wouldn't work because that code only runs when you click on 'this' meaning that if 'this' is clicked on nothing else can also be.)
danpost danpost

2017/6/16

#
You cannot compare an image to a String (lines 26 and 45). Lines 34 through 37 should be in the act method of the Note class. Lines 26 through 31 should be removed and the following should be added after line 33:
getWorld().removeObject(this);
return;
Lines 47, 48 and 50 can be removed; but I do not know what you can replace line 45 with. Lines 56, 57 and 60 can also be removed.
You need to login to post a reply.