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

2011/12/17

Letter string

suushi suushi

2011/12/17

#
fixed
Scaldren Scaldren

2011/12/17

#
background.drawString would work better.
danpost danpost

2011/12/17

#
That seems like a lot of work to get one random letter.
int num = Greenfoot.getRandomNumber(26);
return "ABCDEFGHIJKLMNOPQRSTUVWXYZ".substring(num, num + 1);
does the job.
Scaldren Scaldren

2011/12/17

#
How would you end up drawing the returned letter?
suushi suushi

2011/12/17

#
for some reason its telling me that the result type is void
danpost danpost

2011/12/17

#
If 'getRandomLetter()' was your method name and you called it with 'String randomLetter = getRandomLetter();', then background.drawString(randomLetter, x, y);' would draw the letter at (x, y) in your background.
suushi suushi

2011/12/17

#
its saying it cant find the method. what should i name it so greenfoot can recognize it
Scaldren Scaldren

2011/12/17

#
String alphaBet = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"}; method() { int x = Greenfoot.getRandomNumber(this.getWidth()); int y = Greenfoot.getRandomNumber(this.getHeight()); Greenfoot.getRandomNumber(26) = int int = String background.drawString(String, x, y); }
jackgan jackgan

2011/12/17

#
create array as below: private String ABC = { "A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z" }; then for running it, GreenfootImage background = getBackground(); background.drawString(ABC,x,y);
jackgan jackgan

2011/12/17

#
by the way, do anyone know how can i toggle the fill() method on and off when i press something???
suushi suushi

2011/12/17

#
cant get either of those to work out haha
suushi suushi

2011/12/17

#
nvm got it!
suushi suushi

2011/12/17

#
im not too sure how, i just know how to fill it not toggle.
jackgan jackgan

2011/12/17

#
me either, i can only think of using switch method, but make me confuse
danpost danpost

2011/12/17

#
Just add an instance boolean variable to the class -- call it something like 'fillObject'.
private boolean fillObject = true;
Then, anytime the 'f' is pressed, change the value of 'fillObject'
if ("f".equals(lastKeyPressed)) fillObject = !fillObject;
lastKeyPressed should be set up as a String variable and loaded with a non-null keystroke acquired through Greenfoot.getKey(). Finally, when drawing your objects:
if (fillObject) objectName.fill();
would fill the object if fillObject was true and not fill the object when false ('objectName' would be whatever name you supplied for your object).
You need to login to post a reply.