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

2012/6/7

Using the getKey method

DonaldDuck DonaldDuck

2012/6/7

#
Hey everyone, I've been using the getKey method a lot and I know that it only returns anything since the last time the method was called, but it is a bit frustrating that it carries between classes... If I call the getKey method in my World subclass and simultaneously in any Actor subclass, only the most recent one to execute will return. I've tried putting the method in private voids, protected voids, I even tried making this:
class myWorld()
{
     public String lastKey = "";

     private void returnKey()
     {
          lastKey = Greenfoot.getKey();
     }
     public void act()
     {
          returnKey();
     }
}
so I could have only the one method calling the getKey function where all my classes could access the output of it and that didn't even work... Any suggestions/solutions? Thanks
davmac davmac

2012/6/7

#
so I could have only the one method calling the getKey function where all my classes could access the output of it and that didn't even work...
What do you mean it didn't work? Are you sure the problem isn't in your code - perhaps in the actor classes?
DonaldDuck DonaldDuck

2012/6/7

#
It only returns for the most recent actor to execute it. If Actor A executes it and then Actor B executes it, Actor A will return null and Actor B will return the key...
Osidy Osidy

2014/2/26

#
Has anybody found a fix for this?
danpost danpost

2014/2/26

#
You could do this in your world class:
// add static field
static String mostRecentKey;
// in act
mostRecentKey = Greenfoot.getKey();
Then, anytime a key is released and the method returns a string, it will be available to all classes for one act cycle by using 'myWorld.mostRecentKey' to get it.
You need to login to post a reply.