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

2017/3/27

Help!

RaymondSun RaymondSun

2017/3/27

#
I have a question on Greenfoot Game. Actually I have finish the game but when I run it, it always pause automatically. Especially when the thief touch the moneybag. Can anyone solve it? import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class road here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */

public class road extends World
{

    /**
     * Constructor for objects of class road.
     * 
     */
    static Counter counter = new Counter();
    Counter counter2 = new Counter();
    
    SimpleTimer st = new SimpleTimer();
    
    public road()
    {  
        //set the world size and the location of thief and police
        super(900, 600, 1); 
        
        thief t1 = new thief();
        addObject(t1, 300, 400);
        
        counter.setValue(0);
        st.mark();
        prepare();
    }
    
    public void act()
    { //count for counter
        if(Greenfoot.getRandomNumber(200)<3)
            addObject(new moneybag(), Greenfoot.getRandomNumber(801), 0);
        
             counter2.setValue(st.millisElapsed()/1000);
        
        if(counter2.getValue()>=40)
        {
            showText("Time is up", 450, 300);
            Greenfoot.setWorld(new prison());
            Greenfoot.stop();
        }
    }
    
    private void prepare()
    {   
        police p1=new police();//location for counters
        addObject(p1, 800, 540);
        showText("Score", 40, 20);
        
        addObject(counter, 40, 50);
        showText("Timer", 870, 20);
        
        addObject(counter2, 870, 50);
    }
}import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class road here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */

public class road extends World
{

    /**
     * Constructor for objects of class road.
     * 
     */
    static Counter counter = new Counter();
    Counter counter2 = new Counter();
    
    SimpleTimer st = new SimpleTimer();
    
    public road()
    {  
        //set the world size and the location of thief and police
        super(900, 600, 1); 
        
        thief t1 = new thief();
        addObject(t1, 300, 400);
        
        counter.setValue(0);
        st.mark();
        prepare();
    }
    
    public void act()
    { //count for counter
        if(Greenfoot.getRandomNumber(200)<3)
            addObject(new moneybag(), Greenfoot.getRandomNumber(801), 0);
        
             counter2.setValue(st.millisElapsed()/1000);
        
        if(counter2.getValue()>=40)
        {
            showText("Time is up", 450, 300);
            Greenfoot.setWorld(new prison());
            Greenfoot.stop();
        }
    }
    
    private void prepare()
    {   
        police p1=new police();//location for counters
        addObject(p1, 800, 540);
        showText("Score", 40, 20);
        
        addObject(counter, 40, 50);
        showText("Timer", 870, 20);
        
        addObject(counter2, 870, 50);
    }
}
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class moneybag here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class moneybag extends Actor
{
    /**
     * Act - do whatever the moneybag wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        // to get random number of moneybag
        setLocation(getX(), getY()+5);
        
        if(getWorld().getHeight()-getY()<20)
           getWorld().removeObject(this);
        if(isTouching(thief.class))
        {
           getWorld().removeObject(this);
        }
        if(getY() >= 600)
        {
            getWorld().removeObject(this);
        }
    }
}
Super_Hippo Super_Hippo

2017/3/27

#
Change the if's in lines 22 and 26 in the moneybad class to 'else if'. It shouldn't just stop but show you an error message (NullPointer Exception).
danpost danpost

2017/3/27

#
Super_Hippo wrote...
Change the if's in lines 22 and 26 in the moneybad class to 'else if'. It shouldn't just stop but show you an error message (NullPointer Exception).
Actually, it would be an IllegalStateException. When moneybag touches thief (line 22) and moneybag is removed (line 24), the next thing attempted is 'getY()' (line 26) which cannot be 'null', but will cause an issue if the actor is not in the world (the illegal state of the actor when calling that method).
You need to login to post a reply.