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

2019/11/18

Spawning character on key press & sound playing

MLGwarfare MLGwarfare

2019/11/18

#
I am making a game where you have to dodge bullets to win. To start the game, you have to press the run button and then space bar to start any of the code. I want to spawn a car, a karen, and a kid. I have the isKeyDown command in the world and they do not spawn when I press the spacebar. The sound does not play when I press the spacebar either. Here is my code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
public class tile extends World
{
    /**
     * Constructor for objects of class tile.
     * 
     */
    public tile()
    {   
       super(1280, 720, 1);
       addObject(new spacetostart(), 640, 350);
       if (Greenfoot.isKeyDown("space"))
       {
           Greenfoot.playSound("earrapeflamingo1.mp3");
       }
       if (Greenfoot.isKeyDown("space"))
       {
           addObject(new karen(), 646, 77);
           addObject(new kid(), 650, 77);
           addObject(new car(), 650, 350);
       }
    }
}   
Any help on how to make them spawn and for the sound to play correctly?
albertlai431 albertlai431

2019/11/18

#
You'll have to put those if statements in the act method of the world, since the constructor is only called once when the world is declared. You'll want to be continuously checking if the space button is pressed, not only once.
You need to login to post a reply.