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
and the code for the world they are in is
it keeps giving me the error incompatible types greenfoot.GreenfootImage and java.lang.String
can anyone help me solve this?
thank you
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(); } |
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(); } |