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

2013/2/20

HELP! Is it possible to blend classes?

sopi sopi

2013/2/20

#
Hey Guys I got a problem with my Breakout scenario. I created 3 levels and i want to switch between them with pressing "next" Button. My problem is that this buttons should only be blend if the counter gets a specific value (in my cause 38). Do you have a hint for me how to blend them in? I'm a newbie in Greenfoot and Java so don't be mad if I'm too stupid for this. PS: Sorry for my english I'm no native speaker.
Gevater_Tod4711 Gevater_Tod4711

2013/2/20

#
I think the easyest way to do this would be to add the button at the beginning of the game and activate it if yours score is reached. Therefor you should make a button without an image and a public method activate() (or any other name you want). Also you need a boolean to check whether the button is clickable. Then if the score is reached you call the activate method of your button and it's visible and you can click it. The code for this would look like this:
private boolean clickable;//check whether the button can be clicked;

public Button() {//the constructor of your button class;
    setImage(new GreenfootImage(1, 1));
    clickable = false;
}

public void act() {
    if (clickable && Greenfoot.mouseClicked(this)) {
        //button clicked -> next level;
    }
}

public void activate() {
    clickable = true;
    setImage("theImagesFileName.jpg");
}
Then you just have to call this method if you reach the target score.
sopi sopi

2013/2/20

#
Thanks for your answer but the problem is if I only Set the background image I don't get the bricks in the World. My 3 Levels are each subclasses of World and in everyone I put the bricks in. Have you another idea how i can do it without doing everything New?
Gevater_Tod4711 Gevater_Tod4711

2013/2/21

#
Your button is a subclass of Actor, or not? Because the setImage is not the image of the world but the image of the actor Button. You just change the image of the button so you can see the button. And if It's clicked you just change the world calling Greenfoot.setWorld(new World2()); So you don't have to do everything new. It will still set the world so that your levels can be the same as they are now.
sopi sopi

2013/2/25

#
I tried to do it like your way but it doesn't works. I pasted my Sourcecode maybe you find my mistake.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Weiter1 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Weiter1 extends Objekte
{
    private boolean clickable;    //Überprüfen ob der Button anklickbar ist  
    private Counter counter;
         
    public Weiter1() 
    {  
        setImage(new GreenfootImage(69,377));  
        clickable = false;  
    }  
      
    public void act()
    {  
        if (clickable && Greenfoot.mouseClicked(this))
          {  
            Greenfoot.setWorld(new Level2());   // Erstellt das neue Level 
         }  
    }  
      
    public void activate()
    {  
        if (counter.getValue()==3)
        {
        clickable = true;  
        setImage("weiter.jpg");
        }
    }    
 
  
}
danpost danpost

2013/2/25

#
What I see is a class with a Counter object called 'counter' that is never anything but 'null'; and, if the public method 'activate' is ever called, will cause a NullPointerException.
You need to login to post a reply.