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

2024/3/31

how do I get it to the next level?

ellisi0n ellisi0n

2024/3/31

#
I have a little maze game. If the main touches eind.class, it is supposed to get teleported to level 2. when i type the code below, it keeps saying the variable level_2 is undeclared. Where am I supposed to declare it?
public class movers extends Actor
{
    /**
     * Act - do whatever the movers wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        moveAround();
        teleportNewLevel();
    }

    public void moveAround() {
        int oldX=getX();
        int oldY=getY();
        int oldRotation=getRotation();
        if (Greenfoot.isKeyDown("left")) {
            setRotation(180); move(1);
        } if (Greenfoot.isKeyDown("right")) {
            setRotation(0); 
            setLocation(getX()+1, getY());
        } if (Greenfoot.isKeyDown("up")) {
            setRotation(270); move(1);
        } if (Greenfoot.isKeyDown("down")) {
            setRotation(90); move(1);
        }
        
        if (isTouching(wall.class))
        {
            setLocation(oldX, oldY);
            setRotation(oldRotation);}
            
        if (isTouching(wall_2.class))
        {
            setLocation(oldX, oldY);
            setRotation(oldRotation);}
        }
    
    public void teleportNewLevel()
    {
        if(isTouching (eind.class))
        {
        Greenfoot.setWorld(level_2);
        }
    }
        
    }

danpost danpost

2024/3/31

#
ellisi0n wrote...
I have a little maze game. If the main touches eind.class, it is supposed to get teleported to level 2. when i type the code below, it keeps saying the variable level_2 is undeclared. Where am I supposed to declare it? << Code Omitted >>
In line 43, the Greenfoot.setWorld(World) method needs a World object to activate. If you need to create one, use something like this:
Greenfoot.setWorld(new Level_2());
You need to login to post a reply.