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

2016/3/2

If <condition = true> DO NOT do the following

Bassusour Bassusour

2016/3/2

#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/** this method does the animation
   */
   public void animation()
   {
    if (//I do not move, do not execute the following")
    //my movement is "left", "right", "up", "down")
       if (getImage() == spredteBen)
       {
       setImage(samledeBen);
       }
      else
       {
       setImage(spredteBen);
       }
}
So, it is werid that my instance does the animation, while standing still. Therefore, I want it not to execute, if I am not moving.
1
if (Greenfoot.getKey == ! "left" || "right" || "up" || "down")
I am not that good yet with greenfoot, and this is the only solution i can think of, but Greenfoot doesn't accept this. So how do i fix this?
danpost danpost

2016/3/3

#
Bassusour wrote...
There are a few things you need to understand. First, 'getKey' is a method and, as such, must be followed by a set a round parenthesis -- 'getKey()'. Second, String object are not necessarily the same object just because their character sequences are similar and comparing them with '==' will only be true when they refer to the same exact object. To compare the contents of the strings, use the 'equals' method of the String class:
1
2
3
4
5
String key = Greenfoot.getKey();
if (key != null)
{
    if ("left".equals(key) || "right".equals(key) || "up".equals(key) || "down".equals(key))
    // etc.
Bassusour Bassusour

2016/3/3

#
Thanks for your reply, danpost If you could also explain me what you are doing, because I honestly do not know what you are doing.
danpost danpost

2016/3/3

#
Bassusour wrote...
If you could also explain me what you are doing, because I honestly do not know what you are doing.
Well, line 1 stores any keystroke made in a String variable. This is done because you will not get the same keystroke by calling 'getKey' a second time and you are comparing the key to multiple values. Now, if no keystroke was finished between calls to 'getKey', it will return a 'null' String reference and you cannot do anything with 'null' references other than check for their existence, which is what line 2 does. Once we are sure the returned value is not 'null', we can compare its character sequence to that of the various keystroke values (line 4).
Bassusour Bassusour

2016/3/3

#
Hi danpost. Thanks again for your response I have searched a lot of what a string variable is, and have not found any information, in what it is. Would you mind explaining me it, or offer a link to where i can learn it?
Super_Hippo Super_Hippo

2016/3/3

#
Java API String
The String class represents character strings. All string literals in Java programs, such as "abc", are implemented as instances of this class.
Bassusour Bassusour

2016/3/3

#
Oh, i think i got it now. But The last part i do not understand, is why is it necesarry to put the word string, in before the local variable? Futhermore, can't I just delete the
1
if (key != null)
?
Super_Hippo Super_Hippo

2016/3/3

#
'key' is the name of the variable or in this case, the String object. You need to specify what it is (a String). I think you could remove this line because you can still compare the other strings to 'null'. The good thing about this line is that it doesn't need to compare the 'key' string with the other strings when no new key was pressed. It probably won't create noticeable lag if you remove it, but just keep it there.
Bassusour Bassusour

2016/3/3

#
Thanks for replying So, I have watched some videoes, explaining the String variable. However, the variable will have an output of a string, "like this". However, in danpost's solution, his variable's output was a method (Greenfoot.getKey();). So, does it matter what output a string variable have, like method or a string?
Super_Hippo Super_Hippo

2016/3/3

#
A variable will never have an output. A method can have a variable as an output (return type)., not the other way around. For example the 'getKey' method has the return type 'String'. This means if you call the method, it will return a String (or null). And this will be saved in the variable 'key'.
Bassusour Bassusour

2016/3/4

#
Thank you, Super_Hippo. I understand it now :)
You need to login to post a reply.