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

2020/10/7

Picture Switching Problem

ItzLukeStorm ItzLukeStorm

2020/10/7

#
So I have this problem where I am trying to change the main menu into a how to menu by changing the picture but it won't change, it just stays the main menu picture instead of changing into the how to picture, even though I have the pictures in the images folder of my scenario. Please help.
import greenfoot.*;

public class MainMenu extends Actor
{
    private int menu = 0;
    
    private GreenfootImage Main = new GreenfootImage("main.png");
    private GreenfootImage How = new GreenfootImage("how.png");
    
    public void act() 
    {
        if(menu == 0)
        {
            setImage(Main);
        }
        
        if(Greenfoot.mouseClicked(HowToPlay.class))
            {
                menu = 1;
            }
            
        if(menu == 1)
        {
            setImage(How);
        }
    }    
}
danpost danpost

2020/10/7

#
Line 17 is the problem. The mouseClicked method has an Object type parameter. It is as such to accommodate both Actor and World type objects. The documentation says as much. If you provide anything other than null where you have "HowToPlay.class", then it needs to be a specific instance of Actor or World. What actor is to be clicked, here? If it is the MainMenu actor, then use "this".
ItzLukeStorm ItzLukeStorm

2020/10/8

#
All this code is in my MainMenu actor. There is a separate actor that is being clicked that changes that menu variable.
danpost danpost

2020/10/8

#
ItzLukeStorm wrote...
All this code is in my MainMenu actor. There is a separate actor that is being clicked that changes that menu variable.
Then going the long way:
if (Greenfoot.mouseClicked(null))
{
    Actor clickedOn = Greenfoot.getMouseInfo().getActor();
    if (clickedOn != null && (clickedOn instanceof HowToPlay))
    {
        menu = 1;
    }
}
ItzLukeStorm ItzLukeStorm

2020/10/8

#
Thanks so much. It worked!
You need to login to post a reply.