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

2019/3/29

Making buttons change worlds depending on the image

Erasedsword Erasedsword

2019/3/29

#
Hello I am trying to make a button change worlds (i.e. Levels), however I'm creating the buttons in a single class, so i was thinking of trying to use the image as one of the factors of when to change the world. it currently changes it to level one for both buttons pressed. the code for this class is as fallows
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import greenfoot.*;
 
/**
 * Write a description of class level1Button here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class levelButton extends Actor
{
     private String levelImage;
     private GreenfootImage ButtonImage;
            /**
             * creates the image with its set size
             */
    public levelButton(String lvImage)
    {
        levelImage = lvImage;
        GreenfootImage image = getImage();
        image.scale(image.getWidth() +10, image.getHeight() +10);
        setImage(lvImage);
    }
 
    /**
     * changes the world if the mouse is released
     */
    private void startLevel()
    {
        Levels levels = (Levels)getWorld();
        Level1 level1 = new Level1();
        if (levels.LevelCounter() >= 0 && Greenfoot.mouseClicked(this) && ButtonImage == "Level1.png")
        {
            Greenfoot.setWorld(level1);
        }
    }
     
 
    /**
     * Act - do whatever the level1Button wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        startLevel();
    }
and the code for the world they are in is
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import greenfoot.*;
 
/**
 * Write a description of class Levels here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Levels extends World
{
    private String [] Image= {"Level1.png","Level2.png","Level3.png"};
    private int LevelCount = 1;
    /**
     * Constructor for objects of class Levels.
     *
     */
    public Levels()
    {   
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1);
        prepare();
    }
 
    /**
     * Creates text
     */
    private void prepare()
    {
        showText("Please select A Level", 300,50);;
    }
 
    /**
     * returns levels beaten to spawn in other level buttons
     */
    public int LevelCounter()
    {
        return LevelCount;
    }
 
    private void LevelSpawner()
    {
        addObject(new levelButton(Image[0]), 100,135);
        if (LevelCount >= 1)
        {
        addObject(new levelButton(Image[1]), 200,175);
        }
    }
     
    public void act()
    {
        LevelSpawner();
    }
it keeps giving me the error incompatible types greenfoot.GreenfootImage and java.lang.String can anyone help me solve this? thank you
danpost danpost

2019/3/29

#
In the levelButton class, change "ButtonImage" in line 31 to "levelImage".
Erasedsword Erasedsword

2019/3/30

#
thank you sorry i didn't respond till so late, but is there any way you can think of so that a new button appears when level one is beaten, level 2, act…, however if you replay an earlier level it doesn't remove the new one? I'm sorry for coming to you for all my issues.
danpost danpost

2019/3/31

#
I think the problem is that the level counter is always initially one every time you create a new Levels object. I do not think it to be a very simple fix. Maybe you should take a look at my Super Level Support Class Demo scenario. It might lead you in the right direction.
You need to login to post a reply.