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

2015/11/6

I'm new to coding an I was wondering if I can get some coding help.

1
2
Jason21 Jason21

2015/11/6

#
i'm using Newtons-Lab-3 and i need help adding control keys, for exapmple having the “up” arrow change all the colors of all the bodies. Thanks!
danpost danpost

2015/11/6

#
Track the state of the key you are using for changing the colors. When the state changes and the key is found in the down position, iterate through a list of all bodies in the world and have their colors change. This should be done in your world class act method (or a method it calls). Your attempted code should be shown for further help, if needed.
Jason21 Jason21

2015/11/6

#
okay thanks danpost! I'll give it shot!
Jason21 Jason21

2015/11/6

#
This is what I have danpost
public void act()
{
   
          if (Greenfoot.isKeyUp("Body") && BodyUp == false)
          addnewColor (new Color(),400,300);
     }
danpost danpost

2015/11/6

#
Jason21 wrote...
This is what I have danpost
public void act()
{
    if (Greenfoot.isKeyUp("Body") && BodyUp == false)
        addnewColor (new Color(),400,300);
}
The Greenfoot class does not contain a method called 'isKeyUp' -- only an 'isKeyDown' method and "Body" is not a valid key name.
Jason21 Jason21

2015/11/6

#
I was able to change that first part, but I don't know the key name that would take the place of "Body" can you please show me?
Jason21 Jason21

2015/11/6

#
if (Greenfoot.isKeyDown() && Up == false)
  addnewColor (new Color(),400,300);
danpost danpost

2015/11/6

#
For the "up" arrow key:
if (Greenfoot.isKeyDown("up") && !Up)
Jason21 Jason21

2015/11/6

#
if (Greenfoot.isKeyDown("up") && !Up)
          addnewColor (new Color(),400,300);
That's the code I have in place but it's saying "cannot find symbol - up variable"
danpost danpost

2015/11/6

#
You had shown 'Up' in your code above -- this appears to be something you change from your prior code, which used 'BodyUp'.
Jason21 Jason21

2015/11/6

#
okay. Thanks for all the help danpost... but I'm still getting a syntax error with this code too!
if (Greenfoot.isKeyDown("up") && BodyUp == false)
          addnewColor (new Color(),400,300);
danpost danpost

2015/11/6

#
Jason21 wrote...
okay. Thanks for all the help danpost... but I'm still getting a syntax error with this code too!
if (Greenfoot.isKeyDown("up") && BodyUp == false)
          addnewColor (new Color(),400,300);
Well, there is no Color constructor that has zero parameters. See the java.awt.Color class API documentation to see what constructors are available.
Jason21 Jason21

2015/11/7

#
They problem right now is not the parameters (I know that'll have to be added) but the issue is it keeps telling me "cannot find symbol - BodyUp variable" what does that mean?
danpost danpost

2015/11/7

#
Jason21 wrote...
They problem right now is not the parameters (I know that'll have to be added) but the issue is it keeps telling me "cannot find symbol - BodyUp variable" what does that mean?
It means that the variable has not been declared. I presume that is used to track the state of the "up" key and it should be declared as a instance field within the class:
private boolean BodyUp;
As such, its value should be changed anytime the state of the "up" key is changed:
if (Greenfoot.isKeyDown("up") != BodyUp)
{
    BodyUp = !BodyUp;
    if (BodyUp) addnewColor(new Color(Greenfoot.getRandomNumber(256*256*256)), 400, 300);
}
Jason21 Jason21

2015/11/7

#
Okay I got that part now. But if it says "cannot find symbol - method addnewColor (java.awt.Color,int,int) would I just declare the variable like this?
privat boolean addnewColor; 

if (BodyUp) addnewColor(new Color(Greenfoot.getRandomNumber(256*256*256)), 400, 300);
}
There are more replies on the next page.
1
2