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

2017/6/23

Don't understand why this doesn't work

Kevroa Kevroa

2017/6/23

#
Basically I have this that runs at the start of the game:
public void act() 
    {
        if (gameStart == 0)
        {
            loadHome();
            gameStart = 1;
        }
    }  
for whatever reason it doesn't create these objects:
public void loadHome()
    {
        Background_1 Background_1 = new Background_1();
        getWorld().addObject(Background_1,800,450);
        Title Title = new Title();
        getWorld().addObject(Title,800,185);
        Play Play = new Play();
        getWorld().addObject(Play,800,400);
        Options Options = new Options();
        getWorld().addObject(Options,800,600);
        Exit Exit = new Exit();
        getWorld().addObject(Exit,800,800);
        Selection Selection = new Selection();
        getWorld().addObject(Selection,800,400);
        homeLoaded = 1;
    }
The world is big enough to see all of them in those positions (1600x900) This worked before in a different file but doesn't now.. I don't understand what changed.
danpost danpost

2017/6/23

#
Need more of the code. Please show the entire class code and explain how, or why, it does not work.
Kevroa Kevroa

2017/6/23

#
Here is the entire thing:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Game here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Game extends Actor
{
    public int gameStart = 0;
    public int homeLoaded = 0;
    public int optionsLoaded = 0;
    public int levelSelectLoaded = 0;
    public int pauseMenuLoaded = 0;
    public int pauseOptionsLoaded = 0;
    public int levelLoaded = 0;
    public int homePos = 0;
    public int optionsPos = 0;
    public int levelSelectPos = 0;
    public int pauseMenuPos = 0;
    public int pauseOptionsPos = 0;
    public int waitTime = 0;
    /**
     * Act - do whatever the Game wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        if (gameStart == 0)
        {
            loadHome();
            gameStart = 1;
        }
    }  
    
    /**
     * The methods below are for handling the button in menus or in a level. 
     */
    public void homeButtons()
    {
        
    }
    public void optionsButtons()
    {
        
    }
    public void levelSelectButtons()
    {
        
    }
    public void pauseMenuButtons()
    {
        
    }
    public void pauseOptionsButtons()
    {
        
    }
    public void playerButtons()
    {
        
    }
    
    /**
     * The methods below load and unload specific screens.
     * When a load is called, it creates a new set of objects for the scene.
     * When an unload is called, it deletes the objects specific to its screen. 
     */
    public void loadHome()
    {
        Background_1 Background_1 = new Background_1();
        getWorld().addObject(Background_1,800,450);
        Title Title = new Title();
        getWorld().addObject(Title,800,185);
        Play Play = new Play();
        getWorld().addObject(Play,800,400);
        Options Options = new Options();
        getWorld().addObject(Options,800,600);
        Exit Exit = new Exit();
        getWorld().addObject(Exit,800,800);
        Selection Selection = new Selection();
        getWorld().addObject(Selection,800,400);
        homeLoaded = 1;
    }
    public void unloadHome()
    {
        getWorld().removeObjects(getWorld().getObjects(Background_1.class));
        getWorld().removeObjects(getWorld().getObjects(Title.class));
        getWorld().removeObjects(getWorld().getObjects(Play.class));
        getWorld().removeObjects(getWorld().getObjects(Options.class));
        getWorld().removeObjects(getWorld().getObjects(Exit.class));
        getWorld().removeObjects(getWorld().getObjects(Selection.class));
        homeLoaded = 0;
    }
    public void loadOptions()
    {
        
    }
    public void unloadOptions()
    {
        
    }
    public void loadLevelSelect()
    {
        
    }
    public void unloadLevelSelect()
    {
        
    }
    public void loadPauseMenu()
    {
        
    }
    public void unloadPauseMenu()
    {
        
    }
    public void loadPauseOptions()
    {
        
    }
    public void unloadPauseOptions()
    {
        
    }
}

I only have this code so far becuase I decided to create a new file (My old one became cluttered and unorganized) All of the objects have images set to them aswell.
danpost danpost

2017/6/24

#
Are you adding a Game object into the world from your World subclass constructor or prepare method?
Kevroa Kevroa

2017/6/24

#
Yes, I did. Since this post though, I've restarted my computer and everything seems to work now... I dont really know what happened but hey, if it works I'm fine with it lol.
You need to login to post a reply.