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

2017/1/4

Adding a button

1
2
danpost danpost

2017/1/6

#
jacquelinereilly wrote...
it works.. i just had to fix some minor errors :) I do have another question though. Once the user presses play, the next screen appears, which has an image for it, that looks like squares with the numbers for the different levels. is it possible to get the user to click just one of the squares?
As long as the squares are arrange in some regular fashion, you can programmatically determine where a click occurs and proceed to the appropriate level as needed. Otherwise, you can add some transparent actor objects at the location of the numbers and treat them as buttons.
jacquelinereilly jacquelinereilly

2017/1/6

#
if I do the second option, the code would be similar to what I did for the play button?
danpost danpost

2017/1/6

#
jacquelinereilly wrote...
it just keeps saying no fields when I click inspect, unless I am doing it wrong
The header line is along the top edge of the frame. It usually contains the class name of the object.
jacquelinereilly jacquelinereilly

2017/1/6

#
oh yes I see it now. Thank you for all your help !
danpost danpost

2017/1/6

#
jacquelinereilly wrote...
if I do the second option, the code would be similar to what I did for the play button?
Provided you do not have two many levels. Otherwise, that would be a lot of individual classes for buttons which does not take advantage of the fact that you can create a bunch of buttons from a single class. It would be better, then, to have a reference field to each button, than to have a class for each one (there are other, more advanced, ways, also). You can have the fields, like so:
Actor btnLevel1 = new LevelButton();
Actor btnLevel2 = new LevelButton();
// etc.
Add the transparent buttons into the world in the constructor or prepare method. Then in the act method:
public void act()
{
    if (Greenfoot.mouseClicked(btnLevel1)) Greenfoot.setWorld(new Level1());
    if (Greenfoot.mouseClicked(btnLevel2)) Greenfoot.setWorld(new Level2());
    // etc.
}
The LevelButton class could simply be this:
import greenfoot.*;

public class LevelButton extends Actor
{
    public LevelButton()
    {
        setImage(new GreenfootImage(100, 100)); // adjust size as needed
    }
}
jacquelinereilly jacquelinereilly

2017/1/6

#
Okay, I will try and use that, I'll play around with it a bit. Thank you for all the help
You need to login to post a reply.
1
2