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

2016/3/26

Why isn't this code working?

IanWasHere IanWasHere

2016/3/26

#
I'm trying to change the colour of and actor. However the code I put in doesn't work even though Greenfoot says nothing wrong... private int COME_DOWN = 300; private int grn = 6; private GreenfootImage image; /** * Act - do whatever the Block wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { moveDown(); COME_DOWN --; setColor(); } private void moveDown() { Greenfoot.getRandomNumber(grn); setRotation(90); move(grn); } private void setColor() { image = getImage(); image.setColor(Color.BLACK); } Can someone please help me?!
xFabi xFabi

2016/3/26

#
Don't really see whats wrong, but if it's only going to be changed once, why not make it black by default?
danpost danpost

2016/3/27

#
xFabi wrote...
Don't really see whats wrong, but if it's only going to be changed once, why not make it black by default?
Actually, there are a couple of problems with the given code. (a) you are getting a random number; but not doing anything with it; you are asking for a random value between zero and the current value stored in the 'grn' field; this does not assign the returned random value to the 'grn' field; the line has the same effect as a 'no-op' command; (b) using 'setColor' does not produce any immediate change to the image; it only sets the drawing color for future drawing method calls; You should use the constructor of the class to set the value of 'image', set the drawing color an set the rotation of the actor, although the 'image' field is not needed (the Actor class supplies a private field for the image and it is returned by calling the getImage method); this would make your class look something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import greenfoot.*;
import java.awt.Color;
 
public class Block extends Actor
{
    private int actCounter;
 
    public Block()
    {
        getImage().setColor(Color.BLACK);
        setRotation(90);
    }
     
    public void act()
    {
        moveDown();
        actCounter++;
    }
     
    private void moveDown()
    {
        move(Greenoot.getRandomNumber(< maximum distance to move in a single act >));
    }
}
I change the name of your 'COME_DOWN' field to 'actCounter' since you are changing its value by one each cycle; however, I do not, with the code given, see the use for the field. Field names by convention, unless defined as 'final static' should be in lowercase.
IanWasHere IanWasHere

2016/3/27

#
Thanks so much! Everything is better except for one thing... The colour isn't changing... Thanks anyway!
danpost danpost

2016/3/27

#
IanWasHere wrote...
Everything is better except for one thing... The colour isn't changing...
When do you want the color to change? what conditions are required for that action to take place?
IanWasHere IanWasHere

2016/3/27

#
danpost wrote...
IanWasHere wrote...
Everything is better except for one thing... The colour isn't changing...
When do you want the color to change? what conditions are required for that action to take place?
RIGHT AWAY! As soon as the game starts. I'm a noob.
IanWasHere IanWasHere

2016/3/27

#
ohhhh... My constructor...
IanWasHere IanWasHere

2016/3/27

#
danpost wrote...
IanWasHere wrote...
Everything is better except for one thing... The colour isn't changing...
When do you want the color to change? what conditions are required for that action to take place?
It's not working....
danpost danpost

2016/3/27

#
IanWasHere wrote...
It's not working...
Show your revised constructor (use 'code tags').
IanWasHere IanWasHere

2016/3/27

#
public Block() { setRotation(90); getImage().setColor(Color.BLACK); }
danpost danpost

2016/3/27

#
Where is the method call to draw or fill the image with black? The 'setColor' method is similar to picking a crayon out of the box. It does not take the crayon to the paper. Or, like putting paint of the brush from the pallet; not placing the brush on the easel.
IanWasHere IanWasHere

2016/3/28

#
danpost wrote...
Where is the method call to draw or fill the image with black? The 'setColor' method is similar to picking a crayon out of the box. It does not take the crayon to the paper. Or, like putting paint of the brush from the pallet; not placing the brush on the easel.
public Block() { setRotation(90); getImage().setColor(Color.BLACK); } public void act() { moveDown(); fillBlock(); } public void fillBlock() { GreenfootImage.fill(this); }
danpost danpost

2016/3/28

#
Several problems with the code that was given: (1) if you want the image to be black from the start, you want to fill the image in the constructor of the class -- not in the act method; (2) the 'fill' method does requires zero parameters -- and 'this' refers to the Block object, not its image; (3) the 'fill' method is an instance method (not a 'static', or class, method) -- meaning you cannot call the method on the class,'GreenfootImage' -- you need to call it on the image you want to fill:
1
< image reference >.fill();
(you can get the reference with the 'getImage' method)
IanWasHere IanWasHere

2016/3/28

#
Thank You SO Much! You've taught me SO much!!!
You need to login to post a reply.