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

2014/12/15

Want to remove Buttons

Blackdow97 Blackdow97

2014/12/15

#
Hey guys! I have a really big problem....for school i have to write a programm in greenfoot. I wanted to set up a Startscreen, which worked very well. I have a Background(SetBackground in World class) and 3 Buttons(Actors) for the 3 different Scenarios. Now i want to start the first Scenario i dont want to change the world, so just the Background must change and the Buttons must disapper. This should happen no matter which button i select. After this each button create different actors but this is another topic. Can someone say me pls who i can do this? I tried in World class something like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public Hauptwelt()
{   
    // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
    super(600, 400, 1);
    Bild();
    addObject(new Nbutton(),300,50);
    addObject(new Bbutton(),300,200);
    addObject(new Mbutton(),300,350);
}
public void Bild()
{
    setBackground("brick.jpg");
}
public void populate1()
{
    if (Greenfoot.mouseClicked(Nbutton.class)) {
   removeObject(Nbutton.class);  
}
}
and for every Button this:
1
2
3
4
5
6
7
8
public void act()
{
    Bild();
}
public void Bild()
{
    setImage("Mittelalter.png");
}
This don't worked well...I have no Idea last lesson i tried something with my teacher, but he can't help me very much...I tried many other things but they didnt worked aswell.... Hopefully you can help me.... Sorry for my really bad english, it isn't my native language
danpost danpost

2014/12/15

#
Line 16 of your Hauptwelt class is trying to use a Class as a parameter in the 'mouseClicked' method where the method requires an Actor object. The same goes for line 17 with the 'removeObject' method; it requires an Actor object to remove, not a Class. You can save references to the buttons in the Hauptwelt class and check clicks on each one individually in the act method -- something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
private Actor nButton, bButton, mButton;
 
public Hauptwelt()
{
    super(600, 400, 1);
    // set background image
    setBackground("brick.jpg");
    // create and save references to new buttons
    nButton = new Nbutton();
    bButton = new Bbutton();
    mButton = new Mbutton();
    // add button into world
    addObject(nButton, 300, 50);
    addObject(bButton, 300, 200);
    addObject(mButton, 300, 350);
}
 
// detect and act on button clicks
public void act()
{
    if (Greenfoot.mouseClicked(nButton)) { ... }
    if (Greenfoot.mouseClicked(bButton)) { ... }
    if (Greenfoot.mouseClicked(mButton)) { ... }
}
with the references you can use 'removeObject(nButton)' to remove one of the buttons and similar to that for the others. I do not think that the 'Bild' methods are needed. Oh, and by convention, method names should begin with a lowercase letter -- so, 'Bild' should be 'bild'. As is, the code in your button classes will not do much of anything because it would be continuously setting the same image to the button.
Blackdow97 Blackdow97

2014/12/17

#
Wow thanks this helped me a lot! :) But now i got an other question... I would like to have 3 classes 1: Pmikrobe 2: Rat 3: People I want to remove the Pmikrobe after Touching the Rat this worked very well:
1
removeTouching(Pmikrobe.class);
Very simple! Now i tried something like this:
1
2
3
4
5
6
7
public void essen()
{
    if (removeTouching(Pmikrobe))
     {
         setImage("Mittelalter.png");
      }
    }
I know i need a reference to the class Pmikrobe but i don't know how i get this, never learned this in our lessons.... Furthermore i want to remove the Rat after 30 Seconds after he got touched by Pmikrobe, well for that i have no idea.... Hopefully someone can help me :)
danpost danpost

2014/12/17

#
The 'removeTouching' method is void of a return value. Ask if 'isTouching' (which returns a Boolean) before executing 'removeTouching' (which does not).
Blackdow97 Blackdow97

2014/12/17

#
Well it worked very well but.... This is my Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
public void act()
{
    essen();
    setImage("Ratte.png");
}
public void essen()
{
    if (isTouching(Pmikrobe.class))
    {
        setImage("Mittelalter.png");
        removeTouching(Pmikrobe.class);
}
}
When the Pmikrobe touches the Rat they disapper perfectly but the Image didn't change...If i remove the removeTouching(Pmikrobe.class); method the image changes but only aslong as the Pmikrobe touches the Rat after that the image changes back.. How i can fix that? I believe i need something with boolean but how i do this.... :(
danpost danpost

2014/12/17

#
Yeah. Having 'setImage' as the last unconditional statement in the act method will cause the "Ratte.png" image to continuously be set and displayed as the image of the actor. You do not want the image to be set continuously anyway. You want it to be initially set to that image. So, either put line 4 in the constructor of the class or set the "Ratte.png" image as the default image for objects created from the class.
Blackdow97 Blackdow97

2014/12/17

#
@Danpost Dude for real you help me so much! Sry for my stupid questions, i'm a real noob in Greenfoot....But i make much fun to programm my game. Maybe later i got more questions hopefully someone will still help me^^ :)
You need to login to post a reply.