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

2022/12/19

Error cannot find symbol

Egg Egg

2022/12/19

#
Hi, I'm trying to code a small game for a school project, but I'm sort of stuck. I want the game to be about a character dodging vines coming from the ceiling, and everything works except for the last part of the vines. What I'm trying to achieve is that the vines will come down from the ceiling, go back up and disappear, sp that another vine can come down to do the same thing over and over. But the vines either won't disappear. or give me an error about the line of code that kills my character when it touches a vine not working anymore. Does anyone know how I could fix this? here is my code by the way: class code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Vine1 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Vine1 extends Actor
{
    GifImage myGif = new GifImage("maybe this works.gif");
    /**
     * Act - do whatever the Vine1 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
   
    public void act() 
    {
        // Add your action code here.
        setImage( myGif.getCurrentImage() );
        
        runVineDownTimer();
        
        Actor tommy;
        tommy = getOneObjectAtOffset(50,150, Tommy.class);
        if (tommy != null)
        {
            World world;
            world = getWorld();
            world.removeObject(tommy);
        }
        
        //if (Greenfoot.getRandomNumber(100) < 10)
        //{
          //if(Greenfoot.isKeyDown("right")) setLocation(getX()+3, getY());
          //if(Greenfoot.isKeyDown("left")) setLocation(getX()-3, getY());
          //if(Greenfoot.isKeyDown("down")) setLocation(getX(), getY()+3);
          //if(Greenfoot.isKeyDown("up")) setLocation(getX(), getY()-3);
          //moveDown();
        //}
    }
    
    private int VineDownTimer;
    
    private void runVineDownTimer()
{
    VineDownTimer = (VineDownTimer+1)%300;
    if (VineDownTimer < 200) moveUp();
    if (VineDownTimer >  200) moveDown();
    if (VineDownTimer == 0) getWorld().removeObject(this);
}
    public void moveDown()
    {
     setLocation(getX(), getY()+2); 
    }
       private void moveUp()
{
    setLocation(getX(), getY()-6);
}

}
world code:
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 World
{
    
    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    public MyWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(400, 400, 1); 
        prepare();
    }

    public void act()
    {
    setPaintOrder(Tommy.class,Vine1.class);
    // in act method of world class
runVineSpawnTimer();
}

// instance field in world class
private int VineSpawnTimer;

// new method in world class
private void runVineSpawnTimer()
{
    VineSpawnTimer = (VineSpawnTimer+1)%300; // adjust '300' as desired
    if (VineSpawnTimer == 0) spawnVine();
}
 

    //e
      private void spawnVine()
{
    // add code spawning vine here
    int x = Greenfoot.getRandomNumber(400);
    
    addObject(new Vine1(), x, -50);
    
}
    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    private void prepare()
    {
        Tommy tommy = new Tommy();
        addObject(tommy,190,350);
    }
}
Spock47 Spock47

2022/12/19

#
=== The problem: The act method calls the method "runVineDownTimer". In the last line of "runVineDownTimer", the vine is potentially removed from the world. After that, the act method continues with the Tommy check - especially asking for the world of the vine and calling a method on it. But since the Vine is already removed from the world, the world is null. Therefore calling a method on the world (now null), is throwing a NullPointerException. === The solution: If the vine has been removed from the world, skip the other steps of the act method:
public void act() {
    // Add your action code here.
    setImage( myGif.getCurrentImage() );
    
    runVineDownTimer();
     
    final World world = getWorld();
    if (world == null) {
        return;
    }
    final Actor tommy = getOneObjectAtOffset(50, 150, Tommy.class);
    if (tommy != null) {
        world.removeObject(tommy);
    }
}
Live long and prosper, Spock47
Egg Egg

2022/12/20

#
Thank you! It works now!
You need to login to post a reply.