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:
This is for the subclass(bullet)
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());
}
}
}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);
}
}
