I'm very new to Greenfoot and I've been working on a project that somewhat resembles a board game. I've been working in a act method of an over arching class in the scenario. I wanted this act method to detect the mouse hovering over any of the piece objects (i.e. objects on the board) and enlarge that object. The code below I believe will enlarge the image I am using for the objects. I have two main problems. The first is that, as far as I know, the actions inside the 'Green' option (when coinFlip is equal to zero) if statement is not being executed. The code in it I modified from another example of someone wanting the same detection as me. But I do not know if I did this correctly. Furthermore if there is a more efficient way of doing this I would love to hear it. Secondly I need something that will change the individuals object's (or class'es) image not the whole image of every single overarching's 'Piece' class that has this image. Any help is appreciated, the full Piece class code is down below. If I seemed to have missed any details please feel free to let me know.
import greenfoot.*;
public class Piece extends Actor
{
GreenfootImage GreenPpl = new GreenfootImage("ppl2.png");
boolean mouseOver = false;
int coinFlip = Greenfoot.getRandomNumber(2);
public void act()
{
//0 is Green
if (coinFlip == 0) {
System.out.println("Green goes first!");
for (int i = 0; i == 1;) {
if (!mouseOver && Greenfoot.mouseMoved(this)){
GreenPpl.scale(GreenPpl.getWidth() + 1, GreenPpl.getHeight() + 1);
mouseOver = true;
}
if (mouseOver && Greenfoot.mouseMoved(null) && ! Greenfoot.mouseMoved(this)) {
GreenPpl.scale(GreenPpl.getWidth() - 1, GreenPpl.getHeight() - 1);
mouseOver = false;
}
if (Greenfoot.mouseClicked(this)) {
i = i + 1;
}
}
}else if (coinFlip == 1) {
System.out.println("Orange goes first!");
//stuff that I haven't Programmed
}else{
System.out.println("Something went wrong");
//stuff that I haven't Programmed
}
}
}
