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

2014/12/18

Keeping variables when a new world is created

Space0fAids Space0fAids

2014/12/18

#
Hey, posted a few weeks ago with collision help. Now I've been doing some 'level' stuff. Basically, I want the screen to change to a different level when you hit a certain point on the screen. I have this working, by doing this
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;
/**
 * Write a description of class Menu here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Screen1 extends OverallWorld
{
    /**
     * Constructor for objects of class Background.
     * 
     */
    public Screen1()
    {    
        //super(700, 600, 1, false); 
        setWorld();
        setBackground("Sky.png");
        setPaintOrder(Player.class, Brick.class, Platform.class);
    }
    public void act()
    {
        level();
    }
    public void setWorld()
    {
        addObject(new Player(), 100, 530);
        //Ground
        addObject(new Platform(), 100, 580);
        addObject(new Platform(), 300, 580);
        addObject(new Platform(), 500, 580);
        addObject(new Platform(), 700, 580);
        //Left wall
        addObject(new Brick(), 25, 479);
        addObject(new Brick(), 25, 429);
        addObject(new Brick(), 25, 379);
        addObject(new Brick(), 25, 329);
        addObject(new Brick(), 25, 279);
        addObject(new Brick(), 25, 229);
        addObject(new Brick(), 25, 179);
        addObject(new Brick(), 25, 129);
        addObject(new Brick(), 25, 79);
        addObject(new Brick(), 25, 29);
        addObject(new Brick(), 25, -21);
        //Right wall
        addObject(new Brick(), 675, 479);
        addObject(new Brick(), 675, 429);
        addObject(new Brick(), 675, 379);
        addObject(new Brick(), 675, 329);
        addObject(new Brick(), 675, 279);
        addObject(new Brick(), 675, 229);
        addObject(new Brick(), 675, 179);
        addObject(new Brick(), 675, 129);
        addObject(new Brick(), 675, 79);
        addObject(new Brick(), 675, 29);
        addObject(new Brick(), 675, -21);
    }
    public void level()
    {
        List levelSelect = getObjectsAt(0, 535, Player.class);
        if(levelSelect.isEmpty())
        {
            
        }
        else
        {
            Greenfoot.setWorld(new Screen2());
        }
    }
}
(Before I forget, what is the opposite to the getEmpty() method? !getEmpty()?) But, anyways, that works all well and dandy, but it makes backtracking impossible. So I'm thinking of adding something in each world like
if(screen=1)
{
addObject( new Player(), different x, different y);
}
But that would require keeping an integer that tracks what screen you're on, which I have not been able to do. SO, basically, my question is "How do you track integers when a new world is created?" }
danpost danpost

2014/12/18

#
You may not have to track an integer between worlds. You can determine the world you are in by using the 'instanceof' conditional operator. For example, from an actor class:
if (getWorld() instanceof Screen1)
or from your OverallWorld class:
if (this instanceof Screen1)
You can have multiple checks, one for each type world. There is no 'getEmpty() method. You probably mean 'isEmpty()', whose opposite is '! isEmpty()' (the opposite of any boolean method is pretty much always '!' the same method; it would kind of seem redundant to create a method 'containsAtLeastOneElement()' or 'isNotEmpty()'; there is also a 'size()' method which returns an int that can be compared to zero using either '==' or '!=' for the same results:
if (getObjectsAt(0, 535, Player.class).size() == 0)
Even better than any of the above for level changing, you can add empty methods in your OverallWorld class called 'nextLevel' and resetLevel' and override them in each subclass to go to the appropriate level. Then, the world you are in will automatically go to the level it is supposed to go to if you use them when you need to change or reset the levels (just cast the world to an OverallWorld type to access the method). My Super Level Support Class is an example of this.
Space0fAids Space0fAids

2014/12/18

#
danpost wrote...
I think I understand what you're saying, but I didn't ask a the right question. I want to be able to change where your player is spawned on the screen depending on the level you were just in. So, if I spawn in position X in Screen1, then move to the teleporter where I go to Screen2, I spawn in position Y. But, if I go through that teleporter again, I want to spawn in Screen1 in position Z, instead of X. How would I do THAT?
danpost danpost

2014/12/18

#
One way is to have each teleporter object store what world to go to and where the player should be placed in that world. Another way, if you are creating new worlds when the teleporter object is touched, is to have a second constructor in your worlds that have either a boolean parameter so it knows when to spawn the player at a different location or two int parameters for the coordinates to place the player.
You need to login to post a reply.