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

2013/1/13

spawning at location problems

KierTulp KierTulp

2013/1/13

#
hello. I have made a button which turns on a magnet, which should then spawn a beam under the magnet pulling the player up. the problem is that the beam is not getting spawned under the magnet, but on the beam. here is the code I am using:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)  
  
public class Knop extends Magnet  
{  
    public boolean knopAan;  
    public int counter = 0;  
    public void act()   
    {  
        move(7);  
        spawn();  
  
    }     
      
    public boolean isUmanNear()  
    {  
        if(!getObjectsInRange(40, Uman.class).isEmpty())   
        {    
       return true;    
        }    
      else {    
        return false;    
        }    
    }  
      
    public void isXPressed()  
    {  
        if(isUmanNear())  
        {  
            if(Greenfoot.isKeyDown("x"))  
            {  
               setImage("knop2.png");  
               knopAan = true;  
            }  
        else   
        knopAan = false;  
        }  
    }  
      
    public void spawn()  
    {  
        {  
           if(Greenfoot.isKeyDown("x"))  
           {  
            counter++;  
           }  
           if(counter == 1 && isUmanNear())  
           {  
            ding();  
           }  
       }  
    }  
}  


import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)  
  
public class Magnet extends Scroll  
{  
    public void act()   
    {  
        move(7);  
          
    }    
      
    public void ding()  
    {  
        on();  
    }  
      
     public void on()  
    {  
          GravityUp GravityUp = new GravityUp();  
          getWorld().addObject(GravityUp, getX() ,getY());   
    }    
}  
as you see I am using getX() and getY() in the magnet class, but he is getting the x and y of the knop class. do you know a way of fixing this?
vonmeth vonmeth

2013/1/13

#
The Knop class inherits those methods from the Magnet class. If you wish to get the 'x' and 'y' of the Magnet object, you need to get a reference to the object, then call the getX and getY. You can have multiple Magnet objects, it needs to know which Magnet class object you are talking about.
davmac davmac

2013/1/13

#
You have "Knop extends Magnet". This seems wrong; a Knop probably isn't a Magnet, so Knop should not extend Magnet. It seems like the only reason you do this is so you can call the ding() method of Magnet from Knop; what you should do instead, is have a reference to a Magnet and call ding() on that reference i.e.: magnetRef.ding(); You should probably read tutorial #6.
KierTulp KierTulp

2013/1/31

#
I have read the tutorial about 10 times right now, and tried to do it but I cant seem to understand it :(. I tried doing what the tutorial says, but it now says that it cant find the variable knop.
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);
        knop = new Knop();
    }
    
    public Knop getKnop()
    {
        return knop;
    }
this part below will probably also error, but I think it makes clear what I am trying to do. When the knop is true, it spawns the gravityup under the magnet.
public void spawn()
    {
       Level1 level1 = (Level1) getWorld();
        Knop Knop = level1.getKnop();
        {
             GravityUp GravityUp = new GravityUp();
          getWorld().addObject(GravityUp, getX() ,getY()); 
        }
    }
thanks!
davmac davmac

2013/1/31

#
Looks like you haven't declared knop?
private Knop knop;
Make sure the declaration goes outside the methods but inside the class. As it says in the tutorial: Notice that we added a declaration for a variable called "theCounter". This is an instance variable, meaning that we want the keep the value for as long as the world exists, and so it is declared outside the SpaceWorld constructor (but inside the SpaceWorld class).
KierTulp KierTulp

2013/1/31

#
Ah... that was a stupid mistake :D but now I still have a different problem. In your tutorial you activate a method in another class, but I need to know if a variable that is from the other class is true or false. do you know any ways of doing that?
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class Magnet extends Scroll
{
    public void act() 
    {
        move(7);
        spawn();
    }  
   
    public void spawn()  
    {  
       if(knopThing())
        {  
             GravityUp GravityUp = new GravityUp();  
          getWorld().addObject(GravityUp, getX() ,getY());   
        }  
    }  
    
    public boolean knopThing()
    {
        Level1 level1 = (Level1) getWorld();  
        Knop Knop = level1.getKnop(); 
        Knop.knopAan;
    }
}
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class Knop extends Scroll
{
    public boolean knopAan;
    public int counter = 0;
    public void act() 
    {
        move(7);
    }   
    
    public boolean isUmanNear()
    {
        if(!getObjectsInRange(40, Uman.class).isEmpty()) 
        {  
       return true;  
        }  
      else {  
        return false;  
        }  
    }
    
    public void isXPressed()
    {
        if(isUmanNear())
        {
            if(Greenfoot.isKeyDown("x"))
            {
               setImage("knop2.png");
               knopAan = true;
            }
        else 
        knopAan = false;
        }
    }
}
I am trying to know if the knopAan is true or false and if true, the magnet will spawn a beam. thanks!
danpost danpost

2013/1/31

#
Change line 24 in the Magnet class to:
return Knop.knopAan;
You may have to change the name from Knop Knop to Knop knop as having the same exact name may confuse the compiler.
KierTulp KierTulp

2013/2/1

#
thanks for the awnsers! I just tried changing that, but it gives me this error: "non-static method cannot be referenced from a static context". I also thought of another way of making this work. I now reference to magnet from inside of knop.
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 void isXPressed()  
    {  
        if(isUmanNear()  || Greenfoot.isKeyDown("x"))
        {  
        Level1 level1 = (Level1) getWorld();    
        Magnet magnet = level1.getMagnet();   
        Magnet.spawn();  
        }  
    }  
}  
public class Level1 extends World
{
   private Magnet magnet;
    /**
     * 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);  
        magnet = new Magnet();  
    }  
      
    public Magnet getMagnet()  
    {  
        return magnet;  
    }  
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)  
  
public class Magnet extends Scroll  
{  
    public void act()   
    {  
        move(7);    
    }    
    
    public void spawn()
    {
        
            GravityUp GravityUp = new GravityUp();    
            getWorld().addObject(GravityUp, getX() ,getY());   
         
    }
}  
I still get the static error, but I believe that this would work when that is fixed. thanks!
davmac davmac

2013/2/1

#
In what class and on what line is the error?
danpost danpost

2013/2/1

#
Line 29 in Knop class should be
magnet.spawn();
And lines 13 and 14 in your Magnet class have the name of the object the same as the name of its class. Please try to avoid naming things with exactly the same name. Conventionally, object (and variable) names should begin with lowercase letters, as below (with few minor exceptions, only Class names should begin with uppercase letters):
GravityUp gravityUp = new GravityUp();
getWorld().addObject(gravityUp, getX(), getY());
KierTulp KierTulp

2013/2/1

#
Thanks for all the help, I believe it is really close to working now, but I can't spawn my gravity beam because it doesn't exist in the world yet. "java.lang.IllegalStateException: Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed." I thought of spawning a few of them out of sight, and then moving them to the location when asked, but I thought that there should be an easier and better way of doing this. Do you know a better way?
You need to login to post a reply.