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

2012/7/20

Help Me Please!!!

benedict3578 benedict3578

2012/7/20

#
This is my code and as you can see I am trying to set the image of the actor background when done does not equal zero. What happens is that I get the error message "non-static method setImage cannot be refered to from static content" Please help!!! public void act() { if (Greenfoot.mouseClicked(this)){onClick();} } public void onClick() { if(Safe.b1 == 0 && Safe.b2 == 0 && Safe.b3 == 1 && Safe.b4 == 0 && Safe.b5 == 1 && Safe.b6 == 0 && Safe.b7 == 1 && Safe.b8 == 0 && Safe.b9 == 1 && Safe.b0 == 0) { Safe.done = 1; } else { Safe.error = 1; } if(Safe.done != 0) { setImage("buttongreen.png"); background.setImage("background2deactivated.jpg"); } if(Safe.error != 0) { setImage("buttonred.png"); } }
SPower SPower

2012/7/20

#
what is background in this line:
background.setImage("background2deactivated.jpg");
And next time, if you insert code, click the code button underneath the text field.
benedict3578 benedict3578

2012/7/20

#
background is the name of an Actor class.
SPower SPower

2012/7/20

#
2 things: -Class names start with a Uppercase, so background should be Background -You can't call the method setImage to a class, you need to send it to an object of a class.
danpost danpost

2012/7/20

#
The error is triggered because you are trying to set the image on a 'class' instead of on an object of that class. You need a reference to the particular object of that class you are trying to set the image to, and use 'setImage' on that.
SPower SPower

2012/7/20

#
O and about this:
benedict3578 wrote...
background is the name of an Actor class.
background is a subclass of Actor :)
danpost danpost

2012/7/20

#
O and about this:
SPower wrote...
You can't call the method setImage to a class, you need to send it to an object of a class.
You can't call the method setImage to a class, you need to send it to an object of a subclass of Actor :+).
SPower SPower

2012/7/20

#
@danpost what can I say? :)
danpost danpost

2012/7/20

#
@SPower Actually, and unfortunately, your 'mistake' is worse than benedict3578's. His could only mean what you corrected his to say, as the Actor class is already named 'Actor' and cannot be renamed. Yours, on the other hand, has other possibilities which do not fit the case. Let's not nittpick semantics, at least in those cases where it is obvious what is being said. :)
You need to login to post a reply.