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

2019/11/25

Keep Object in New World

HEISENBERG HEISENBERG

2019/11/25

#
I have a player class and I'm trying to keep the same object when I go to the next level. How would I do this? This is my code for the moment: (the code I want to keep in all player classes)
private void collect () {
        Actor coin = getOneIntersectingObject(Coin.class);

        if (coin != null) {
            getWorld().removeObject(coin);
            coinsCollected++;
        }
        if (coinsCollected == 5 && levelCounter == 0) {
        	getWorld().addObject(new door_temp(), 157, 162);
        	levelCounter = 1;
        	coinsCollected = 0;
        	secondLevel();
        }

        if (coinsCollected == 5 && levelCounter == 1) {
        	getWorld().addObject(new door_temp(), 961, 170);
        	levelCounter = 2;
        	coinsCollected = 0;
        	thirdLevel();
        }
        
        if (coinsCollected == 5 && levelCounter == 2) {
        	getWorld().addObject(new door_temp(), 961, 170);
        	levelCounter += 1;
        	coinsCollected = 0;
        	fourthLevel();
        }
    }
(the code in each world class)
// Player
        player player = new player();
        addObject(player, 900, 466);
        player.setLocation(900, 466);
danpost danpost

2019/11/25

#
HEISENBERG wrote...
I have a player class and I'm trying to keep the same object when I go to the next level. How would I do this?
What are the names of the worlds you are between and what classes are they extending from? What code do you have for changing between the levels and where is it located?
HEISENBERG HEISENBERG

2019/11/30

#
public void secondLevel ()  {
        Actor secondlvl = getOneIntersectingObject(door_temp.class);

        if (secondlvl != null) {
        	levelCounter += 1;
            Greenfoot.setWorld(new Level_2());
        }
    }

    public void thirdLevel ()  {
        Actor thirdlvl = getOneIntersectingObject(door_temp.class);

        if (thirdlvl != null) {
        	levelCounter += 1;
            Greenfoot.setWorld(new Level_3());
        }
    }

    public void fourthLevel ()  {
        Actor fourthlvl = getOneIntersectingObject(door_temp.class);

    	if (fourthlvl != null) {
    		levelCounter += 1;
    		Greenfoot.setWorld(new Level_4());
    	}
    }
HEISENBERG HEISENBERG

2019/11/30

#
its located in my player class
danpost danpost

2019/11/30

#
I can see a need for a level counter when the same world is used for different levels; but, you are using different worlds. What reason do you have for needing the same object in the different levels? It just might not be necessary.
HEISENBERG HEISENBERG

2019/11/30

#
Well for some reason my player gets stuck at level 2 as the counter gets reset every time
danpost danpost

2019/11/30

#
HEISENBERG wrote...
Well for some reason my player gets stuck at level 2 as the counter gets reset every time
Use the current level to determine what level to proceed to. Something like the following to replace all player codes above:
private void collect()
{
    if ( ! isTouching(Coin.class)) return;
    removeTouching(Coin.class);
    if (++coinsCollected < 5) return;
    if ( ! getWorld().getObjects(door_temp.class).isEmpty() ) return;
    Actor door = new door_temp();
    if (getWorld() instanceof Level_1) getWorld().addObject(door, 157, 162);
    else if (getWorld() instanceof Level_2) getWorld().addObject(door, 961, 170);
    else if (getWorld() instanceof Level_3) getWorld().addObject(door, 961, 170);
}

]private void levelUp()
{
    if ( ! isTouching(door_temp.class ) return;
    World world = null;
    if (getWorld() instanceof Level_1) world = new Level_2();
    else if (getWorld() instanceof Level_2) world = new Level_3);
    else if (getWorld() instanceof Level_3) world = new Level_4;
    if (world != null) Greenfoot.setWorld(world);
    // else game completed
}
You need to login to post a reply.