@Peach, actually I have two World subclasses: Hangman and TextFileViewer (to display the text files of the code). The only Actor subclass is the Letter class which I have as an inner class of Hangman and one Object subclass (the Words class). I used the Letter class only for the 26 Letter object that can be clicked on (the ones that turn gray after being chosen). The guessed letter (and the letter that appear in red after a lose) as well as the images used to show the drawing of the one who is being hanged are also Actor objects; but of no particular class (un-named Actor objects). I have a method in the Hangman class that is used to created and return an un-named actor object (getGenericActor).
sorry i don't really understand, so you don't have different classes for each body part of the hangman? eg. (right arm, right leg, head, etc)? instead, that is under the letter class?
@Peach, I do not have separate classes for each body part; however, even though they are created from within the Letter class, they are not Letter actors. In fact. they are not of any particular type of Actor (they are un-named, or generic). I felt no need to place them in separate classes as once they are drawn, their type is un-important (no collision checking or anything would need to know what class they are) and there is no further action for them to take (no act, fields or methods). Their only purpose is for image display and there was only a need for Actor class methods to accomplish that.
Look at the 'getGenericActor' method, which is used to return a new generic Actor object. Also, look at some of my other recent uploads and view the codes within them for more examples of creating these un-named Actor object; as well as some that are named, but coded within another class (inner classes).
i don't understand this part of the code:
static String[] words = {< WordsOmiited>}
static java.util.Random r and = new java.util.Random(System.currentTimeMillis());
@Peach, they are just field declaration statements for the array of words and the random number generator which is used to produce a random index for the word in the array to use in the puzzle. Getting the current time produces a 'random' seed for the random number generator. Check the java.util.Random API documentation for additional information on this type of Object. Having these field as 'static' causes them to be set prior to any Objects being created from the class (actually, they are created during compilation; and resetting does not alter them); and as these two fields will not change throughout the running of the scenario, they should probably be made 'final' as well.
@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)?
2014/4/24
2014/4/29
2014/4/29
2014/4/30
2014/4/30
2014/4/30
2014/4/30
2014/5/1
2014/5/1
2014/5/5
2014/5/5
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