I have reused some code so that a keypress will add an object to the screen. The code has the test isDown to check that the key has been depressed before extra objects are added. This works as expected when placed in a subclass of World. However, it doesn't work when placed into the subclass of Actor that the object is an instance of. Multiple copies are added (at a seemingly exponential rate) even though the key hasn't been depressed.
Can someone explain why the same code produces different results please?
public void openNote()
{
if (!isDown && Greenfoot.isKeyDown("down"))
{
addObject(new Note(), Greenfoot.getRandomNumber(400), Greenfoot.getRandomNumber(400));
isDown = true;
}
if (!Greenfoot.isKeyDown("down"))
{
isDown = false;
}
}
The boolean isDown was decalared in both attempts.
Many thanks,
Steven.