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

2015/12/19

Where i spawn an object which i need to test for coordinates?

HB.greenfoot HB.greenfoot

2015/12/19

#
I got a java.lang.StackOverflowError because i spawned my Object (Flag) in my main scenario. I only want to spawn this object one time but i need to test the coordinates of this object. I don't know where i have to put my Flag Flag =new Flag().
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)


public class Main extends Actor
{
   
   
   Flag Flag = new Flag();
    
    
    public void act() 
    {
        
    }  
    
    public boolean activ(Actor anActor)
    {
        return getWorld().getObjects(null).contains(anActor);
    }

    public void Walk()
    {   if (activ(Flag)){
        
        if (getX()<Flag.getX()){
            setLocation(this.getX()+1, this.getY());
        }
        if (getX()>Flag.getX()){
            setLocation(this.getX()-1, this.getY());
        }
        if (getY()<Flag.getY()){
            setLocation(this.getX(), this.getY()+1);
        }
        if (getY()>Flag.getY()){
            setLocation(this.getX(), this.getY()-1);
        }
    }
   
      
    }
 
  
  
}
I hope you can help me!
danpost danpost

2015/12/19

#
A stack overflow error can occur anywhere in your code. The cause is usually code that repeats itself (like an infinite loop). Oftentimes, it will happen due to how the construction of your world is programmed. Start by posting the constructors of your world and your Flag classes.
HB.greenfoot HB.greenfoot

2015/12/20

#
Thank for your help! I fixed the problem!
You need to login to post a reply.