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

2016/4/19

Greenfoot.isKeyDown not working?

Hunter1471 Hunter1471

2016/4/19

#
Hello all, I'm new to Greenfoot and one of my games I am doing for a school project isn't working. This following snippet of code is from my actor, however the Greenfoot.isKeyDown isn't working. public void act() { if (Greenfoot.isKeyDown("d")) { move(1); } if (Greenfoot.isKeyDown("a")) { move(-1); } GreenfootImage image = getImage(); image.scale(30, 30); } I also have another actor which randomly spawns in copies of itself and I'm beginning to think that it might be the problem as my character moves fine without it.
public void act()

{
    Greenfoot.delay(50);
      getWorld().addObject(new Apple1(), Greenfoot.getRandomNumber(600), 0);
    Greenfoot.delay(50);
      getWorld().addObject(new Apple1(), Greenfoot.getRandomNumber(600), 225);
}
danpost danpost

2016/4/19

#
I think the problem comes from your use of the 'Greenfoot.Delay(int)' calls. These calls stop your entire program for about a second each time it is called using '50' as the parameter. I do not think that is what you are wanting. If I am not mistaken, you want to add apples on alternate sides of the world about one apple per second. This would require an iinstance int field to count the act methods between spawnings. You can have it count to 100 and repeat and when it is evenly divisible by 50, then add an apple. Use the quotient (the value of the counter divided by 50) to determine which side of the world to spawn at.
You need to login to post a reply.