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

2011/12/13

quick question

suushi suushi

2011/12/13

#
How do i create a method to do something continuous when i press a key and make it stop when i press a different key? thanks
mjrb4 mjrb4

2011/12/14

#
The way that jumps to mind would be to have a boolean field in your actor that you want to do something when the key is pressed, say "action", and then have an if statement in your act method that does the continuous action if "action" is set to true. You then need a couple more bits of logic with regards to key presses, Greenfoot.isKeyDown() will be useful here - if you detect the correct key press, set action to true (and then set it to false on the other key press.)
danpost danpost

2011/12/14

#
isKeyDown needs a parameter for which key you are checking (that might take some coding to check all other keys to stop the action). I suggest a String field to hold the last keystroke (String lastKeyPressed). In act, set String myKey to what is returned with Greenfoot.getKey(). Then if myKey is not null, set lastKeyPressed to myKey. Then, check to see if lastKeyPressed is the right key to act on and perform the action if it is.
suushi suushi

2011/12/14

#
String lastKeyPressed "(the last key that was pressed)"?
danpost danpost

2011/12/14

#
Greenfoot.getKey() returns the last key pressed and released, but only once. Once it returns a key, if called again, it will return null until another key is pressed (and released). Because it only returns each key once, it is important to immediately set the value returned to a String variable. In other words, you cannot say "If Greenfoot.getKey is not null, set variable to Greenfoot.getKey"; you must say "set variable to Greenfoot.getKey, if variable is not null, ..."
suushi suushi

2011/12/14

#
hmmm, im just having trouble figuring out how to set them to each other
danpost danpost

2011/12/14

#
What do you have so far?
suushi suushi

2011/12/14

#
if ( Greenfoot.isKeyDown("4")){ String myKey = Greenfoot.getKey(); if (myKey != null) lastKeyPressed = myKey; if ("4".equals(lastKeyPressed)){ java.awt.Color a; int r = Greenfoot.getRandomNumber(255); int g = Greenfoot.getRandomNumber(255); int b = Greenfoot.getRandomNumber(255); int x = Greenfoot.getRandomNumber(this.getWidth()); int y = Greenfoot.getRandomNumber(this.getHeight()); int x1 = Greenfoot.getRandomNumber(this.getWidth()); int y2 = Greenfoot.getRandomNumber(this.getHeight()); a = new java.awt.Color(r,g,b); background.clear(); background.setColor(a); background.setColorAt(x, y, a); background.drawRect(x, y, x1, y2); }
danpost danpost

2011/12/14

#
Is this in your world class?
suushi suushi

2011/12/14

#
yes
suushi suushi

2011/12/14

#
yes
danpost danpost

2011/12/14

#
String myKey = Greenfoot.getKey();
if (myKey != null) lastKeyPressed = myKey;
if ("4".equals(lastKeyPressed))
{
    Color a;
    int r = Greenfoot.getRandomNumber(255);
    int g = Greenfoot.getRandomNumber(255);
    int b = Greenfoot.getRandomNumber(255);
    int x = Greenfoot.getRandomNumber(this.getWidth());
    int y = Greenfoot.getRandomNumber(this.getHeight());
    int x1 = Greenfoot.getRandomNumber(this.getWidth() - x);
    int y2 = Greenfoot.getRandomNumber(this.getHeight() - y);
    a = new Color(r, g, b, 255);
    background.clear();
    background.setColor(a);
    background.drawRect(x, y, x1, y2);
}
Replace ALL that you posted with the above (including the initial 'if' statement). I adjusted 'x1' and 'y1' so that the width and height of the shape does not extent past the end of the world. I also eliminated a 'setColorAt' statement (not sure what it was doing in there). The only other thing I did was changed all your 'java.awt.Color's to 'Color', and changed the method new Color used to include the transparency field (255). Also, make sure you add the String variable lastKeyPressed to your world class with 'public String lastKeyPressed = "";' UPDATE: I am guessing that background is already declared as a GreenfootImage in your world class. If not, you need to put 'GreenfootImage background = getBackground();' before any references to it.
suushi suushi

2011/12/14

#
whew! ok. thank you very much!
You need to login to post a reply.