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

2013/4/13

Concatenate

welleid welleid

2013/4/13

#
Hello, I know you can concatenate some String, but I wanted to know, is there a way to concatenate a method ? For example, I have a game with many levels. When you finish a level, I set in the world :
addObject(new nextLevel("Level2"), 50, 100);
Then in the nextLevel code :
public class nextLevel extends Bouton
{
    private String thisL;
    
    public nextLevel(String whichL)
    {
        thisL = whichL;
        beBouton("Good job ! here for the next level");
    }
    
    public void act() 
    {
        if(Greenfoot.mouseClicked(this))
        {
            Btn.setTransparency(255);
            Greenfoot.setWorld(new ...());
        }
    }    
}
I want to know if I set in a String with the name of the next level, can I put it in the act method to go on the next ? I dont really want to create so many class for different buttons with only that little change. Thanks a lot !
danpost danpost

2013/4/13

#
The short answer is yes. However, you will still have to code each new world creation seperately.
public void act()
{
  if(Greenfoot.mouseClicked(this))
  {
    Btn.setTransparency(255);
    if("Level2".equals(thisL)) Greenfoot.setWorld(new Level2());
    if("Level3".equals(thisL)) Greenfoot.setWorld(new Level3());
    // etc.
  }
}
welleid welleid

2013/4/13

#
Thanks a lot, this works perfectly ! But for my personal curiosity, is there a way to concatenate such a method itself ? something like : Greenfoot.setWorld(new Level '+ thisL'() ); Thanks
danpost danpost

2013/4/13

#
Sorry for the delayed response. In short, no; there is no way to take two parts of the name of a class and combine them like that.
welleid welleid

2013/4/13

#
Okay, thanks a lot for your help !
You need to login to post a reply.