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

2017/12/28

How to make something happen the 1st time the world spawns in

Bandito Bandito

2017/12/28

#
So I have a shop that I am trying to make work, I can switch between worlds just fine but I realized that when I would switch back to the main world it would reset everything (the characters' positions and all the variables). I can't figure out a way to make all the stuff spawn in and all the variables reset only the 1st time the world is "spawned in". Any help would be greatly appreciated.
import greenfoot.*;
import javax.swing.JOptionPane;
/**
 * You are the Human, the last survivor of the zombie apocalypse. In This level you are at your home camp and you are under attack! Fend off the impending horde for as long as possible and mabye survive for another day 
 * 
 * @author Eric Faltermeier
 * @date 12/4/2017
 * 
 * I got the picure of grass from Textures101.com
 */
public class ZombieWorld extends World
{
    public ZombieWorld()
    {    
        super(1600, 900, 1);
        showInstructions();
        prepare();
    }

    public void prepare()
    // This is where all the objects are added, a human, a test variable, 5 zombies on the left side, and 5 zombies on the right side
    {
        addObject(new TestVariable(), 800, 450);
        addObject(new Human(), 800, 450);
        addObject(new BulletCount(), 1530, 20);
        addObject(new Money(), 1417, 20); 
        for(int c = 0; c < 5; c++) 
        {
            int x = getWidth();
            int y = Greenfoot.getRandomNumber(getHeight());
            addObject(new Zombie(), x, y);
        }
        for(int c = 0; c < 5; c++) 
        {
            int x = 0;
            int y = Greenfoot.getRandomNumber(getHeight());
            addObject(new Zombie(), x, y);
        }

    }

    public void showInstructions()
    // At the beginning of the game this shows up, it tells the player the goal and how to play
    {
        JOptionPane.showMessageDialog(null, "You are the last Human alive and you have to survive!\nUse WSAD to move around and space bar to shoot");
    }
}
Bandito Bandito

2017/12/28

#
What I was thinking of doing was not changing the world at all but just removing everything, changing the background, and adding the buttons, but I don't know how to save the positions of all the different characters to do so.
danpost danpost

2017/12/28

#
It is okay to change worlds. What you cannot do however is lose all references to the original world. You can do this by passing a reference to the second world and use that reference to resume that original world as the active world. Also, you probably want to create only one Shop so its inventory can be maintained properly. Create the Shop world in your ZombieWorld class and keep a reference to it. Also, add a getter method to the class to return the Shop object:
// field in ZombieWorld class
private Shop shop;

// in ZombieWorld constructor
shop = new Shop(this);

// in ZombieWorld class
public Shop getShop()
{
    return shop;
}
To activate shop from an Actor subclass:
Greenfoot.setWorld(((ZombieWorld)getWorld()).getShop());
In Shop class:
// field for ZombieWorld object
private World gameWorld;

// constructor
public  Shop(World zombieWorld)
{
    gameWorld = zombieWorld;
    // rest of world building code
}

// method to resume game
public void resumeGame()
{
    Greenfoot.setWorld(gameWorld);
}
To return to game, call the 'resumeGame' method:
// from an Actor subclass
((Shop)getWorld()).resumeGame();
Bandito Bandito

2017/12/28

#
That seemed to work thanks for your help as always!
You need to login to post a reply.