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

2018/9/11

Can't call a function from World in an actor.

Joxev Joxev

2018/9/11

#
MyWorld:
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 MyWorld extends greenfoot.World
{

    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    
    
    public MyWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(1080, 720, 1); 
        Bread bread = new Bread();
        Player player = new Player();
        placeBread();
        addObject(player, getWidth() / 2, getHeight() / 2);
    }
    public void placeBread() {
       Bread bread = new Bread();
       addObject(bread, getRandomNumber(1, 1080), getRandomNumber(1, 720));
    }
    public int getRandomNumber(int start,int end)
    {
       int normal = Greenfoot.getRandomNumber(end-start+1);
       return normal+start;
    }
}
Bread
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class bread here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Bread extends Actor
{
    /**
     * Act - do whatever the bread wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    
    public int getRandomNumber(int start,int end)
    {
       int normal = Greenfoot.getRandomNumber(end-start+1);
       return normal+start;
    }
    public void act() 
    {
        if (isTouching(Player.class)) {
            getWorld().removeObject(this);
            ((MyWorld)getWorld()).placeBread();
        }
    }
    // GREENFOOTWORK
    // Take this current project, and expand on it!
    // When you pick up a piece of bread, spawn a new piece of bread,
    // some where else on the screen (maybe a random location)
    // Make sure to reference the documentation if you need to
    // BONUS:
    // Make it so that more bread spawns each time (up to a limit)
    // e.g., the first time i pick up a piece a bread, 2 other bread spawn.
    // Every time afterwards, 3, then 4, then so on.
    
}
Player
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class player here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Player extends Actor
{
    /**
     * Act - do whatever the player wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    
    int speed = 1;
   
    
    public void act() 
    {
        speed = 1;
        if (Greenfoot.isKeyDown("shift")) {
            speed = 3;
        }
        if (Greenfoot.isKeyDown("right")) {
            setLocation(getX() + speed, getY());
        }
        if (Greenfoot.isKeyDown("left")) {
            setLocation(getX() - speed, getY());
        }
        if (Greenfoot.isKeyDown("up")) {
            setLocation(getX(), getY() - speed);
        }
        if (Greenfoot.isKeyDown("down")) {
            setLocation(getX(), getY() + speed);
        }
    }    
}
When calling the placeBread(); function in Bread, the error:
java.lang.NullPointerException
	at Bread.act(Bread.java:26)
	at greenfoot.core.Simulation.actActor(Simulation.java:567)
	at greenfoot.core.Simulation.runOneLoop(Simulation.java:530)
	at greenfoot.core.Simulation.runContent(Simulation.java:193)
	at greenfoot.core.Simulation.run(Simulation.java:183)
java.lang.NullPointerException
	at Bread.act(Bread.java:25)
	at greenfoot.core.Simulation.actActor(Simulation.java:567)
	at greenfoot.core.Simulation.runOneLoop(Simulation.java:530)
	at greenfoot.core.Simulation.runContent(Simulation.java:193)
	at greenfoot.core.Simulation.run(Simulation.java:183)
java.lang.NullPointerException
	at Bread.act(Bread.java:25)
	at greenfoot.core.Simulation.actActor(Simulation.java:567)
	at greenfoot.core.Simulation.runOneLoop(Simulation.java:530)
	at greenfoot.core.Simulation.runContent(Simulation.java:193)
	at greenfoot.core.Simulation.run(Simulation.java:183)
shows up. Can anyone help me fix this?
Super_Hippo Super_Hippo

2018/9/12

#
Swap lines 24 and 25 in the Bread class.
You need to login to post a reply.