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

2013/8/19

Adding an object when a button is pressed

a_quiet_person a_quiet_person

2013/8/19

#
I am trying to get an actor ("punchline") to appear when the enter button is pressed, at the moment I have the following code in my world file for this actor; Punchline punchline = new Punchline(); if (Greenfoot.isKeyDown("Enter")) { addObject (punchline, 300, 210); } This doesn't seem to work at all. What am I doing wrong? Thanks
Gevater_Tod4711 Gevater_Tod4711

2013/8/19

#
I'm not quite shure if the isKeyDown method is case sensitive. You could try using "if (Greenfoot.isKeyDown("enter")) {". If this doesn't work the object maybe is added in a part of the world where you can't see it. So you should make shure that you coordinates are right.
a_quiet_person a_quiet_person

2013/8/19

#
That didn't work and if I remove the if function it is visible, so the coordinates are right. :/
Gevater_Tod4711 Gevater_Tod4711

2013/8/19

#
Did you write this code int the act method of the world? (or in a method that is called from the act method) If you have written this code e.g. in the constructor of the world the programm will only once check whether the enter key is pressed and not when the programm is running.
a_quiet_person a_quiet_person

2013/8/19

#
I accidently put it in the constructor, it works now. Thank you!
You need to login to post a reply.