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

2013/4/9

Adding Actors with the Greenfoot.isKeyDown("") method

Kiara Kiara

2013/4/9

#
I am making a text so that if a text says something certain, it will set that image, such as if a balloon was chosen, a balloon would appear on the screen. The problem is I want to have the text multiple choice so if I pressed the "a" button, a balloon would appear.
JetLennit JetLennit

2013/4/9

#
try something similar to
if(Greenfoot.isKeyDown("a"))
{     
        yourBalloonClass balloon = new yourBalloonClass();
       getWorld().addObject(balloon, theX, theY);
} 
JetLennit JetLennit

2013/4/9

#
Or if you want the statement to be in the world class
if(Greenfoot.isKeyDown("a"))
{     
        yourBalloonClass balloon = new yourBalloonClass();
       addObject(balloon, theX, theY);
} 
danpost danpost

2013/4/9

#
You will probably want to use the 'Greenfoot.getKey()' method instead of using 'Greenfoot.isKeyDown(keyName)'. The main reason is that the key may be down for several cycles and your program will produce multiple objects over-top one another. Since you can only use the 'getKey' method once per act cycle, you will need to store to returned value in a field and then check the value of the field against the possible trigger keys. The code should start something like this:
String key = Greenfoot.getKey();
if ("a".equals(key)) addObject(new Balloon(), theX, theY);
if ("b".equals(key)) addObject(new Plane(), theX, theY);
// etc.
Kiara Kiara

2013/4/9

#
Thank you both!
You need to login to post a reply.