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

2022/10/23

Adding object

N8TH1N6 N8TH1N6

2022/10/23

#
    public void act() 
    {
        if (Greenfoot.isKeyDown("escape"))
        {       
            if (getWorld().getObjects(pauseMenu.class).isEmpty() )
            {
                getWorld().addObject(new pauseMenu(), 300, 300);
                getWorld().addObject(new ingameExitButton(), 304, 428);
            }
                
            else  
            {
                while (!Greenfoot.isKeyDown("r"))
                    {
                        timeDelay(1);
                        Actor actor = getOneIntersectingObject(pauseMenu.class);
                        getWorld().removeObject(actor);
                        
                        Actor eBut = getOneIntersectingObject(ingameExitButton.class);
                        getWorld().removeObject(eBut);
                        
                    }
            }
        }
        
        animation();
        testeTasteGedrueckt();
        suchUndEss();

    }    
Button
public class ingameExitButton extends pauseMenu
{
    private static final GreenfootImage transpar = new GreenfootImage("transpar.png");
    public void act()
    {
        transpar.scale(44, 17);
        ExitButton();
        setImage("crab.png");
    }/* (World, Actor, GreenfootImage, Greenfoot and MouseInfo)*/

    /**
     * Constructor for objects of class playButton.
     */
    public void ExitButton()
    {
        if (Greenfoot.mouseClicked(this)) {
            Greenfoot.setWorld(new  MainMenu());
        }
    }
}
the crab.png is just to see if the button gets added I just add transparent pictures on the text I made on the picture itself, kinda like a hitbox the one with the pauseMenu works totally fine but the button is a problem, it just doesnt get added Best
danpost danpost

2022/10/24

#
N8TH1N6 wrote...
<< Code Omitted >>
What class is the first code posting from? When are objects created from that class and put into the world? (show codes in full blocks)
xxYourMOMxx xxYourMOMxx

2022/10/24

#
YOUR MOM
N8TH1N6 N8TH1N6

2022/10/24

#
danpost wrote...
N8TH1N6 wrote...
<< Code Omitted >>
What class is the first code posting from? When are objects created from that class and put into the world? (show codes in full blocks)
the first one is in the "MeineKrabbe" class wich is in actor
danpost danpost

2022/10/24

#
N8TH1N6 wrote...
the first one is in the "MeineKrabbe" class wich is in actor
Okay, but lines 3 through 24 appears to be game control code which is not something any crab has any business doing. That implementation should be in your game world act method. Also, as coded, it appears that the pause menu and exit button will only be in the world for one act cycle. That is, once placed in the world and the "r" key is found to not be down, they will be removed. You would have to have the "r" key down when pressing the "escape" key to see those actors and when the "r" key is released, the actors will then disappear. Even if that was dealt with, I think the while loop would still prevent any clicks from being detected on the exit button. You might consider using a different world -- a world to be in while the game is paused. Just maintain a reference to the game world so you can return to it if necessary:
import greenfoot.*;

public class PauseWorld extends World
{
    private World gameWorld;
    private Actor exitButton;
    
    public PauseWorld(World world)
    {
        super(600, 400, 1);
        gameWorld = world;
        exitButton = new ingameExitButton();
        addObject(exitButton, 304, 428);
    }
    
    public void act()
    {
        if (Greenfoot.isKeyDown("r")) Greenfoot.setWorld(gameWorld);
        if (Greenfoot.mouseClicked(exitButton)) Greenfoot.setWorld(new MainMenu());
    }
}
With something like that, your game world act method can have the likes of this line:
if (Greenfoot.isKeyDown("escape")) Greenfoot.setWorld(new PauseWorld(this));
N8TH1N6 N8TH1N6

2022/11/14

#
danpost wrote...
N8TH1N6 wrote...
the first one is in the "MeineKrabbe" class wich is in actor
Okay, but lines 3 through 24 appears to be game control code which is not something any crab has any business doing. That implementation should be in your game world act method. Also, as coded, it appears that the pause menu and exit button will only be in the world for one act cycle. That is, once placed in the world and the "r" key is found to not be down, they will be removed. You would have to have the "r" key down when pressing the "escape" key to see those actors and when the "r" key is released, the actors will then disappear. Even if that was dealt with, I think the while loop would still prevent any clicks from being detected on the exit button. You might consider using a different world -- a world to be in while the game is paused. Just maintain a reference to the game world so you can return to it if necessary:
import greenfoot.*;

public class PauseWorld extends World
{
    private World gameWorld;
    private Actor exitButton;
    
    public PauseWorld(World world)
    {
        super(600, 400, 1);
        gameWorld = world;
        exitButton = new ingameExitButton();
        addObject(exitButton, 304, 428);
    }
    
    public void act()
    {
        if (Greenfoot.isKeyDown("r")) Greenfoot.setWorld(gameWorld);
        if (Greenfoot.mouseClicked(exitButton)) Greenfoot.setWorld(new MainMenu());
    }
}
With something like that, your game world act method can have the likes of this line:
if (Greenfoot.isKeyDown("escape")) Greenfoot.setWorld(new PauseWorld(this));
Hey, the code worked for a while, somehow now it gives me a white background and not the world behind it do you know why?
N8TH1N6 N8TH1N6

2022/11/14

#
N8TH1N6 wrote...
danpost wrote...
N8TH1N6 wrote...
the first one is in the "MeineKrabbe" class wich is in actor
Okay, but lines 3 through 24 appears to be game control code which is not something any crab has any business doing. That implementation should be in your game world act method. Also, as coded, it appears that the pause menu and exit button will only be in the world for one act cycle. That is, once placed in the world and the "r" key is found to not be down, they will be removed. You would have to have the "r" key down when pressing the "escape" key to see those actors and when the "r" key is released, the actors will then disappear. Even if that was dealt with, I think the while loop would still prevent any clicks from being detected on the exit button. You might consider using a different world -- a world to be in while the game is paused. Just maintain a reference to the game world so you can return to it if necessary:
import greenfoot.*;

public class PauseWorld extends World
{
    private World gameWorld;
    private Actor exitButton;
    
    public PauseWorld(World world)
    {
        super(600, 400, 1);
        gameWorld = world;
        exitButton = new ingameExitButton();
        addObject(exitButton, 304, 428);
    }
    
    public void act()
    {
        if (Greenfoot.isKeyDown("r")) Greenfoot.setWorld(gameWorld);
        if (Greenfoot.mouseClicked(exitButton)) Greenfoot.setWorld(new MainMenu());
    }
}
With something like that, your game world act method can have the likes of this line:
if (Greenfoot.isKeyDown("escape")) Greenfoot.setWorld(new PauseWorld(this));
Hey, the code worked for a while, somehow now it gives me a white background and not the world behind it do you know why?
Nvm found the error was a problem because I changed the size of the world
You need to login to post a reply.