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

2013/2/3

references and spawning problems

KierTulp KierTulp

2013/2/3

#
hello everybody! I am trying to make a magnet working with a button, but I can't spawn the gravitybeam because it is not in the world yet. I thought that just placing one out of view and then moving it to the magnet when I wanted it to would fix the problem, but when I try to move it it wont work, and when I move it by stopping the scenario, rightclicking the button and clicking spawn, it gives me this error: "java.lang.NullPointerException at Magnet.spawn(Magnet.java:13) at Knop.spawn(Knop.java:40) " here's my code.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)  
  
public class Knop extends Scroll  
{  
    public int counter = 0;  
    
    public void act()   
    {  
        move(7);  
    }     
      
    public boolean isUmanNear()  
    {  
        if(!getObjectsInRange(40, Uman.class).isEmpty())   
        {    
       return true;    
        }    
      else {    
        return false;    
        }    
    }  
      
    public boolean isXPressed()  
    {
         if(Greenfoot.isKeyDown("x"))  
        {    
       return true;    
        }    
      else {    
        return false;    
        }    
    }
    
    public void spawn()  
    {  
        if(isUmanNear() || isXPressed())
        {  
        Level1 level1 = (Level1) getWorld();    
        Magnet magnet = level1.getMagnet();   
        magnet.spawn();  
        }  
    }  
    
    
}  
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)  
  
public class Magnet extends Scroll  
{  
    public void act()   
    {  
        move(7);    
    }    
    
     public void spawn()
    {
        Level1 level1 = (Level1) getWorld();      
        GravityUp gu = level1.getGravityUp(); 
        gu.spawn();
    }
}  
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class achtergrond here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Level1 extends World
{
   private Magnet mag;
   private GravityUp gu;
    /**
     * Constructor for objects of class achtergrond.
     * 
     */
   public Level1()  
    {      
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.  
        super(600, 400, 1, false);  
        prepare();  
        setPaintOrder(ZwarteBalk.class);  
        mag = new Magnet();  
        gu = new GravityUp();
    }  
      
    public GravityUp getGravityUp()
    {
        return gu;
    }
    
    public Magnet getMagnet()  
    {  
        return mag;  
    }  
    
public void prepare()

    {   GravityUp gu = new GravityUp();
        addObject(gu, 0, 10000);
    } 
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class GravityUp here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class GravityUp extends Scroll
{

    /**
     * Act - do whatever the groundblock wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
         move(7);
    }    
    
    public void spawn()
    {
        Level1 level1 = (Level1) getWorld();      
        GravityUp gu = level1.getGravityUp();     
        setLocation(getX(), getY());    
    }
}
I am not good with references so please awnser in lamens terms.
danpost danpost

2013/2/4

#
It seems you are trying to make things so much more difficult than they have to be. All the 'spawn' methods and references are probably confusing you to death. You need to keep things simple to start. Stick with the constructors and act methods. You can always refactor the code later (moving lengthy duplicated code into methods; and breaking down the basic steps taken in the act method into sub-methods, only if the 'act' method is excessively long). I want to tell you to just delete all the 'spawn' methods and references; but, then we would be starting almost from scratch again. One thing I saw immediately was that nowhere in your world class are you adding the 'mag' object into the world. The next thing I saw (and this is major), is that in your world class code, you have your 'gu' field being set within the constructor with 'new GravityUp()' , but in the 'prepare' method, you create a second GravityUp object and set it to a local variable 'gu' and add it to the world. At this point, you have one GravityUp object saved in a field in the world class and a different one in the Level1 world. Truthfully, I think that you should remove all the 'spawn' methods and all the references from your scenario. Then, make sure to add a Magnet object into the world. Since the GravityUp object is 'spawned' from the Magnet object, give the Magnet class the reference to the GravityUp object and create the GravityUp object in the Magnet constructor. Now, you can control the GravityUp object from the Magnet class (adding and removing it from the world as neccessary; and easily using the Magnet object's location (and rotation) to orient the GravityUp object appropriately). The check for the 'x' keypress should also be in with the Magnet class code (not the Knop class code). Another condition should probably also be checked along the the 'isKeyDown' check: getWorld(getObjects(GravityUp.class).isEmpty() (though, I do not think that it is absolutely neccessary in this case).
You need to login to post a reply.