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

2017/8/3

Terminal Window - Need Help!

tMsuperhero tMsuperhero

2017/8/3

#
I want my actor (Mand2) to be able to fire a bullet (ildkugle). I've tried this, but this error appeared after trying to compile: "java.lang.StackOverflowError at greenfoot.Actor.getClassImage(Actor.java:676) at greenfoot.Actor.<init>(Actor.java:131) at MAND2.<init>(MAND2.java:9) at Ildkugle.<init>(Ildkugle.java:9) at MAND2.<init>(MAND2.java:16) at Ildkugle.<init>(Ildkugle.java:9) at MAND2.<init>(MAND2.java:16) at Ildkugle.<init>(Ildkugle.java:9)...................." The code I've tried is: This is for the actor:
    Ildkugle ildkugle = new Ildkugle ();
    
    public void act() 
    {
      if(Greenfoot.isKeyDown("up"))
        {
            setLocation(getX(), getY()-4);
        }
      if(Greenfoot.isKeyDown("Down"))
        {
            setLocation(getX(), getY()+4);
        } 
      fireOnCommand();  
    }   
    public void fireOnCommand()
    {
        if(Greenfoot.isKeyDown("left"))
        {
            World myWorld = getWorld();
            myWorld.addObject(ildkugle, 0, 0);
            ildkugle.setLocation(getX(), getY());
        } 
    }
}
This is for the subclass(bullet)
public class Ildkugle extends MAND2
{
    /**
     * Act - do whatever the Ildkugle wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        move(10);
    }    
}
danpost danpost

2017/8/3

#
Because you have the Ildkugle class extending the MAND2 class, every time you create an Ildkugle object, another one is instantiated (line 1 of MAND2 class code above). A bullet is not a MAND2 type object and therefore, its class should not extend MAND2. Make the following change:
public class Ildkugle extends MAND2
// to
public class Ildkugle extends Actor
tMsuperhero tMsuperhero

2017/8/3

#
Much thanks! :)
You need to login to post a reply.