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

2018/10/28

Basic Button

Gaming_100 Gaming_100

2018/10/28

#
Can someone just tell me a basic button code? Like you click the actor and it switches worlds? I've been trying this but when I click it nothing happens.
    if (Greenfoot.mousePressed(this))
    {
        World LevelSelect = getWorld();
        Greenfoot.setWorld(new LevelSelect()); 
    }
danpost danpost

2018/10/28

#
Gaming_100 wrote...
Can someone just tell me a basic button code? Like you click the actor and it switches worlds? I've been trying this but when I click it nothing happens. << Code Omitted >>
You do not need line 3; however, the removal of that line should not change anything. If you cannot drag the actor around when the scenario is paused, then that would be the problem.
Gaming_100 Gaming_100

2018/10/28

#
Still not working.. what am I missing?
import greenfoot.*;
 
public class PlayButton extends Actor
{
    public PlayButton()
    {    
        GreenfootImage image = getImage();  
        image.scale(256, 70);
        setImage(image);
    if (Greenfoot.mousePressed(this))
    {
        Greenfoot.setWorld(new LevelSelect());
    }
}
    }


danpost danpost

2018/10/28

#
Gaming_100 wrote...
Still not working.. what am I missing?
Lines 10 through 13 will not work in the constructor of the class. The constructor is only executed once when the actor is created and before it is in any world to click on. Make use of the act method for doing stuff while the actor is in a world.
Gaming_100 Gaming_100

2018/10/29

#
danpost wrote...
Gaming_100 wrote...
Still not working.. what am I missing?
Lines 10 through 13 will not work in the constructor of the class. The constructor is only executed once when the actor is created and before it is in any world to click on. Make use of the act method for doing stuff while the actor is in a world.
So what should my code look like?
danpost danpost

2018/10/29

#
Gaming_100 wrote...
So what should my code look like?
Just move (cut and paste) lines 10 through 13 into an act method within the class.
You need to login to post a reply.