@Peach, you are talking about a parameter in the 'getLetterImage' method. '@param' means that the following word is the name of a parameter. 'alphaChar' is the name I gave the String parameter. 'the letter to display on this new image' tells you that the new image created within the method will contain the glyph of the letter contained in the 'alphaChar' string. The calling method, with the expressions for the arguments already evaluated, would look like this:
getLetterImage("L", java.awt.Color.black);
You were asking about declaring a color variable. You declare one just like you declare any other object.
java.awt.Color color = null; // declare variable to hold a color
color = java.awt.Color.blue; // assignment example one
color = new java.awt.Color(255, 64, 40); // assignment example two
If using 'import java.awt.Color;', the previous would be:
Color color = null;
color = Color.blue;
color = new Color(255, 64, 40);
so I would need 26 getLetterImage methods for all 26 letters of the alphabet?
eg. getLetterImage("A", java.awt.Color.black);
getLetterImage("B", java.awt.Color.black);
getLetterImage("C", java.awt.Color.black);
etc
thanks
when I write this, private GreenfootImage getLetterImage("A",java.awt.Color.black)
a message saying "illegal start of type" keeps popping up. how do I fix it?
also, in the method "return new GreenfootImage("A", 36, black, null);" what does the number 36 represent?
Also,
@Peach, it looks like you are missing a comma between "A" and java.awt.Color.black. And, no, you do not have to have 26 methods, one for each letter. The parameter 'alphaChar' can be any one of the alpha characters that is passed to the 'getLetterImage' method..
As far as the '36', refer to the following link:
http://www.greenfoot.org/files/javadoc/greenfoot/GreenfootImage.html#GreenfootImage(java.lang.String, int, java.awt.Color, java.awt.Color)
@Peach, maybe you are not missing a comma (the comma is so close to the 'j', it appears to be part of the 'j'). Please start a discussion on this issue showing how you are trying to use 'private GreenfootImage getLetterImage("A", java.awt.Color.black)'. Without context it is hard to figure out what you are trying to do.
I don't really understand this part of the code, could you please explain it?
word = words.nextWord();
int baseX = getWidth()/2 - word.length() * 18 + 9;
bg.setColor(Color.black);
for (int i=0; i<word.length(); i++) {
bg.fillRect(baseX+36*i, getHeight()-10, 24, 3);
what is baseX?
@Peach, the code you are showing gets a new random word and draws the underlines on the world background for each letter in the new word. 'baseX' is assigned the calculated x-coordinate for where the underline for the first letter in the new word is to go. Once that value is determined, the 'for' loop can draw all the underlines for all the letters in the new word in the proper positions so the word is centered along the base of the screen.
This is a very nice game! I was just wondering in your method "LetterPressed" what is the parameter? what is a string key?
and what does this part of the code mean:
letterClicked(letters[(int)key.charAt(0)-65])
In your next method, "private void letterClicked(Letter actor)" what is the parameter "letter actor"?
@Watchb, in the middle section of the act method, you will see where I detect a keystroke, make it uppercase and call the 'letterClicked' method if the key is between "A" and "Z" inclusive.
key.charAt(0) returns the ascii code value for the first (and only) character in the 'key' String object ('A' = 65 and 'Z' = 90); subtracting 65 from that gives a range of 0 to 25, which is the index of the Letter actor in the 'letters' array that contains the image of the 'key' letter. The 'letters' array contains the Letter actors that can be clicked on near the bottom of the screen. The Letters class can be found at the end of the Hangman class code.
@Peach, obviously it is a call to 'addObject', a method in the World class that uses the signature 'addObject(actor, int, int)'. The actor is a new Letter object that is being placed within the array of letters declared near the top of the class. This line is found within a 'for' loop that iterates 'i' through the numbers 0 to 25 (for each letter of the alphabet). Notice in the scenario how I split the 26 letters into two rows of 13 letters each. The first int (the x-coordinate of where to place the letter actor) is '24+38*(i%13)'. This says to start at x-coordinate 24 and add 'i' factors of 38 (the distance between each letter). Taking the modulus 13 of 'i' starts the factor back at zero again for the second row. The second int (the y-coordinate of where to place the letter actor) is '510+30*(i/13)'. This says to start at y-coordinate 510 and add a factor of 30 for the second row ('i/13' will return 0 until 'i' reaches 13; then it will return one for the rest of the loop).
@danpost, thank you for the explanation. If I wanted to have all the the 26 letters in 1 row, what would the code be like? do I just delete "(i%13)" and "(i/13)" from the code?
so like this: addObject(letters[i] = new Letter(i), 24+38, 510+30;
thank you!
Danpost, is it possible to not use the getGenericActor method in the game? For example, is it possible to just create actor classes for each body part (head, right arm, right leg, body, etc)?
As my laptop does not run the Java plugin in a browser, I cannot access the scenario. Unfortunately, I am also unable to find the scenarios source code. Where can I find it?
@olenz, the source for this scenario was not provided for download. It is, however, viewable while running the scenario. Did you try using a different browser (maybe IE)?
The words omitted are the Hangman puzzle answers (not given for obvious reasons).
Little difference between the two randoms. In fact, the method Greenfoot.getRandomNumber(int) uses java.util.Random in its implementation.
As far as random, I used it here because I wanted to ensure a new random seed value each and every loading (greenfoot probably does it in a somewhat similar manner, but anyway ...).
I just asked the words to chea....
Whatever it may be, but the main thung is that how u got to know that Greenfoot.getRandomNubmer() is just a implementation of java.util.Random? Is their any api for all the methods of greenfoot(i mean how the methods are created and not the api which shows how to use it)
If yes, plz share the link
From source (comments are mine):
-----------------
// importing class
import java.util.Random;
// creating Random object
private static Random randomGenerator = new Random();
// using Random object
public static int getRandomNumber(int limit)
{
return randomGenerator.nextInt(limit);
}
2014/5/14
2014/5/14
2014/5/14
2014/5/14
2014/5/14
2014/5/14
2014/5/16
2014/5/16
2014/5/16
2014/5/16
2014/5/19
2014/5/19
2014/5/19
2014/5/19
2014/5/20
2014/5/20
2014/5/21
2014/5/21
2014/5/22
2014/5/22
2014/5/23
2014/5/23
2014/5/23
2014/5/23
2014/5/23
2014/5/24
2014/5/24
2014/5/24
2014/5/24
2014/5/24
2014/5/24
2014/5/24
2014/5/24
2014/6/27
2015/6/8
2015/6/8
2015/6/8
2015/6/8
2015/6/8
2021/10/7
2021/10/7
2021/10/7
2021/10/7
2021/10/7
2021/10/7