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

2018/7/24

Can't Switch Levels

jjboy jjboy

2018/7/24

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class MyWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Space extends World
{
    counter counter = new counter();
    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    public Space()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(420, 780, 1); 
        
        
        prepare();
       
    }

    public counter getCounter()
    {
        return counter;
    }
    
    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    private void prepare()
    {
          block0 block0 = new block0();
        addObject(block0,218,765);
        block0.setLocation(211,750);
        block1 block1 = new block1();
        addObject(block1,211,704);
        block1.setLocation(211,692);

        
        block1.setLocation(91,691);
        block1.setLocation(270,690);
        block1.setLocation(271,693);
        block1.setLocation(211,690);
        block1.setLocation(270,691);
        block1.setLocation(271,689);
        
        
    }
}
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class MyWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class world2 extends World
{
    
    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    public world2()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(420, 780, 1); 
        
        
        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()
    {

        block0 block0 = new block0();
        addObject(block0,218,765);
        block0.setLocation(211,750);
        block1 block1 = new block1();
        addObject(block1,211,704);
        block1.setLocation(211,692);

        
        block1.setLocation(91,691);
        block1.setLocation(270,690);
        block1.setLocation(271,693);
        block1.setLocation(211,690);
        block1.setLocation(270,691);
        block1.setLocation(271,689);
        block1.setLocation(211,689);
        block1 block12 = new block1();
        addObject(block12,210,488);
        block1.setLocation(231,682);
        removeObject(block1);
        block0 block02 = new block0();
        addObject(block02,217,697);
        block02.setLocation(211,688);
        block12.setLocation(211,631);
        block12.setLocation(271,632);
    }
}
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class counter here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class counter extends Actor
{
    int score = 0;
    /**
     * Act - do whatever the counter wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        setImage(new GreenfootImage("Score : " + score, 24, Color.GREEN, Color.BLACK));
    }    
    public void addScore()
    {
        score++;
        
        if (score == 1) Greenfoot.setWorld(new world2());
        if (score == 2) Greenfoot.setWorld(new world3());
        
    }
    
}
Agent40 Agent40

2018/7/26

#
addScore isn't called upon? (Line 16-20)
jjboy jjboy

2018/7/26

#
oh, I didn't even see that, thanks for your help!
danpost danpost

2018/7/26

#
jjboy wrote...
oh, I didn't even see that, thanks for your help!
I do not suspect that addScore would be called from any of the classes given. At any rate, there is a more general issue here in that once you leave the Space world, the counter object it created goes with it. However, I do not see a need for a counter that causes a world change upon each increment of its value -- that is, you can just change worlds (it should be obvious which world to proceed to determined by which world is currently active).
You need to login to post a reply.