This site requires JavaScript, please enable it in your browser!
Greenfoot back
zachary.c.pratt@gmail.com
zachary.c.pratt@gmail.com wrote ...

2017/2/14

Greenfoot World Transition

This is the next arrow code. When you click it, it should always go to the next world, yet it does not do this. The idea is that every time the next arrow button is clicked, it will go to the next world. Clicking NextArrow in Map1 will send it to Map2. Clicking NextArrow in Map2 will send it to Map3 and so on.
public class NextArrow extends Actor
{
    public int worldVar;
    public NextArrow(int worldVar)
    {
        
    }
    /**
     * Act - do whatever the NextArrow wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    
    public void act() 
    {
        //int number = 0;
        //int number2 = 1;
        
        if(Greenfoot.mouseClicked(this))
        {
            
            //Greenfoot.setWorld(new ("Map"+number()) );
            //Greenfoot.setWorld(new Map2());
            System.out.println("before "+worldVar);
            World world = getWorld(); 
            
            if(worldVar == 2)
            {
                Greenfoot.setWorld(new Map3());
                System.out.println(worldVar);
            }
            
            if(worldVar == 1) 
            {
                Greenfoot.setWorld(new Map2());
                System.out.println(worldVar);
            }
            
        }  
    }
}
This is the Map1 code. It is the same as the other maps besides the parameters for the next arrow which is set to the number of the world it is in.
public class Map1 extends World
{

    /**
     * Constructor for objects of class Map1.
     * 
     */
    public Map1()
    {    
        super(700, 500, 1); 
        Actor actor = new Bulbasaur();
        addObject(actor,100,100);
        prepare();
    }

    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    private void prepare()
    {
        NextArrow nextarrow = new NextArrow(1);
        addObject(nextarrow,698,547);
        nextarrow.setLocation(604,454);
    }

}
Thanks in advance for any advice and suggestions!
Super_Hippo Super_Hippo

2017/2/14

#
You will see that 'worldVar' in the nextArrow class will always be 0. Try to add this line in its constructor:
this.worldVar = worldVar;
You could also just use something like this:
if (getWorld() instanceof Map1) Greenfoot.setWorld(new Map2());
else if (getWorld() instanceof Map2) Greenfoot.setWorld(new Map3());
//...
Hey! Thanks, it worked perfectly! I appreciate the help.
You need to login to post a reply.