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

2018/5/15

Help With Maintaining a Variable Please

WarRudd WarRudd

2018/5/15

#
I have a puzzle game where a character has to get a key, open a door, and touch a treasure chest to go onto the next level. I have an int level that i use to keep track of what level I'm on and this also decides what level to go to
public void nextLvl()
    {
        if(
        canSee(TreasureChest.class))
        {
            if(lvl == 1)
            {

                Greenfoot.stop();
                Greenfoot.setWorld(new Tutorial2());

            }            
            else if(lvl == 2)
            {

                Greenfoot.stop();
                Greenfoot.setWorld(new Tutorial3());

            }
        }

    }
However, when it goes to the next level int lvl resets back to one. How can i have it increase each time I go onto a new level? In case you need it i will post all of the code from my "Main Character" class:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class MainChar here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class MainChar extends Objects
{
    public int Keys = 0;
    public String facing = "down";
    public int lvl = 1;
    /**
     * Act - do whatever the MainChar wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        checkRedDoor();
        move(4);
        nextLvl();
        checkReset();
    }

    public void nextLvl()
    {
        if(
        canSee(TreasureChest.class))
        {
            if(lvl == 1)
            {

                Greenfoot.stop();
                Greenfoot.setWorld(new Tutorial2());

            }            
            else if(lvl == 2)
            {

                Greenfoot.stop();
                Greenfoot.setWorld(new Tutorial3());

            }
        }

    }

    public void move(int moveAmt)
    {
        int dy = getY();
        int dx = getX();

        if (Greenfoot.isKeyDown("up"))
        {
            setLocation(dx, dy-3);
            for(int i = 0; i < moveAmt; i++)
            {  
                if (getOneIntersectingObject(Wall.class) != null || getOneIntersectingObject(WallRotated.class) != null || getOneIntersectingObject(WallRed.class) != null ) 
                    setLocation(getX() , getY()+1);
                facing = "up";}

        }
        if (Greenfoot.isKeyDown("down"))
        {
            setLocation(dx, dy+3);
            setImage(new GreenfootImage("MainCharFront.png"));
            for(int i = 0; i < moveAmt; i++)
            {
                if (getOneIntersectingObject(Wall.class) != null || getOneIntersectingObject(WallRotated.class) != null || getOneIntersectingObject(WallRed.class) != null )
                    setLocation(getX() , getY()-1);
                facing = "down";
            }
        }
        if (Greenfoot.isKeyDown("left"))
        {
            setLocation(dx-3, dy);
            for(int i = 0; i < moveAmt; i++)
            {
                if (getOneIntersectingObject(WallRotated.class) != null || getOneIntersectingObject(Wall.class) != null || getOneIntersectingObject(WallRed.class)!= null ) 
                    setLocation(getX()+ 1 , getY());
                facing = "left";
            }
        }
        if (Greenfoot.isKeyDown("right"))
        {
            setLocation(dx+3, dy);
            for(int i = 0; i < moveAmt; i++)
            {

                if (getOneIntersectingObject(WallRotated.class) != null || getOneIntersectingObject(Wall.class) !=null)
                    setLocation(getX()-1 , getY());
                facing = "right";
            }
        }
        if(canSee(Key.class))
        {
            eat(Key.class);
            Keys++;
        }
    }

    public void checkRedDoor()
    {
        if ("x".equals(Greenfoot.getKey()) && Keys>0)

        {
            Key thrownKey = new Key();
            getWorld().addObject(thrownKey,getX(), getY()-10);
            if(facing.equals("up"))
            {
                thrownKey.setLocation(getX()+10, getY()-30);
                for(int x = 10; x > 0; x-- )
                {

                    thrownKey.setLocation(thrownKey.getX(), thrownKey.getY()-1);

                }
                Keys--;
            }

            else if (facing.equals("left")){
                thrownKey.setLocation(getX()-30, getY());
                for(int x = 10; x > 0; x-- )
                {
                    thrownKey.setLocation(thrownKey.getX()-1, thrownKey.getY());
                }
                Keys--;
            }   
            else if(facing.equals("down"))
            {
                thrownKey.setLocation(getX()+10, getY()+40);
                for(int x = 10; x > 0; x--)
                {
                    thrownKey.setLocation(thrownKey.getX(), thrownKey.getY()+1);
                }
                Keys--;
            }   
            else if(facing.equals("right"))
            {
                thrownKey.setLocation(getX()+30, getY());
                for(int x = 10; x > 0; x--)
                {
                    thrownKey.setLocation(thrownKey.getX()+1, thrownKey.getY());
                }
                Keys--;
            }   
        } 
    }

    public void checkReset()
    {
        if((Greenfoot.isKeyDown("r")))
        {
            Greenfoot.setWorld(new Tutorial1());
        }

    }

}

Thanks in advance
danpost danpost

2018/5/15

#
When you set a new world active, you are losing everything in the old world (as the code now stands) including the MainChar object in that old world and any fields that it holds (where one of them is your lvl field). Fortunately, the lvl field does not appear to be necessary as each level is in a world of a specific type. You can use:
World newWorld = null;
if (getWorld() instanceof Tutorial1) newWorld = new Tutorial2();
if (getWorld() instanceof Tutorial2) newWorld = new Tutorial3();
if (newWorld != null) Greenfoot.setWorld(newWorld); else Greenfoot.stop();
WarRudd WarRudd

2018/5/15

#
And where exactly does this code go? In Main Char?
danpost danpost

2018/5/15

#
WarRudd wrote...
And where exactly does this code go? In Main Char?
Yes -- it would replace lines 31 through 44.
WarRudd WarRudd

2018/5/15

#
I'll try this out and let you know how it goes. Thanks!
WarRudd WarRudd

2018/5/16

#
Thanks Danpost it worked great
You need to login to post a reply.