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

2017/3/1

act() Method seemingly not working

gurgi414 gurgi414

2017/3/1

#
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
        }
    }    
}
danpost danpost

2017/3/1

#
For starters, it is the 'for' loop that will not do anything. You have the following in line 16:
for (int i = 0; i == 1;)
which says to initialize 'i' to zero and iterate as long as the value of 'i' is one. If the value is zero, then it is not one and will not execute its code. There are multiple other issues with the code (lines 36 through 41 can be removed as 'coinFlip' can only be '0' or '1'; mouse action will not be detected even if your 'for' loop did iterate; multiple scaling of the image is not recommended; and how to distinguish between a Green and an Orange piece); however, the main thing is that game control should be done by your World subclass -- not by the individual pieces. The pieces should only actively detect mouse hover and clicks. It should then have methods for game actions it is capable of doing (including, but not limited to, moving, capturing, and image changing) -- actions that they are called upon to do.
gurgi414 gurgi414

2017/3/2

#
Thank you very much for the information. I still have one main question which is what is your recommended way of detecting a mouse hovering over a specific class.
Nosson1459 Nosson1459

2017/3/2

#
It looks like using Greenfoot.mouseMoved(this) with the mouseOver boolean in the Piece class is my (/danpost's - that's what he used in his scenarios) recommended way of seeing if the mouse is over that Object.
danpost danpost

2017/3/2

#
The code within the 'for' loop looks quite similar to what I usually use. The only difference is that I would use an Actor reference field to hold the one the mouse is hovering over instead of each one deciding for itself that state. This ensures that only one will ever have a registered mouse-over state at any time:
// Piece class field
private static Piece hoveredPiece = null;

/** in act of Piece class */
// gaining hover 
if ((hoveredPiece == null  || hoveredPiece.getWorld() == null) && Greenfoot.mouseMoved(this))
{
    hoveredPiece = this;
    // change image
}
// losing hover
if (hoveredPiece == this && Greenfoot.mouseMoved(null) && !Greenfoot.mouseMoved(this))
{
    hoveredPiece = null;
    // restore image
}
danpost danpost

2017/3/2

#
Nosson1459 wrote...
It looks like using Greenfoot.mouseMoved(this) with the mouseOver boolean in the Piece class is my (/danpost's - that's what he used in his scenarios) recommended way of seeing if the mouse is over that Object.
I usually use the 'boolean mouseOver' instance field when only one object of that type has mouse hover state. When there are multiple objects of that type, then I try to use something like what I have shown above.
Nosson1459 Nosson1459

2017/3/2

#
Today I saw two scenarios of yours that used the boolean because there was only one Object but I didn't see any scenario with multiple objects of one type so I didn't see you use the class field variable.
danpost danpost

2017/3/2

#
Nosson1459 wrote...
Today I saw two scenarios of yours that used the boolean because there was only one Object
Curious -- which scenarios were they?
Nosson1459 Nosson1459

2017/3/2

#
danpost danpost

2017/3/2

#
Nosson1459 wrote...
Menu Demo and MouseForce.
Okay. Yes, the Menu Demo scenario is an early work of mine that does use a boolean instance field for multiple objects of a class. I think I would do it differently at present. The MouseForce scenario does not have any hovering code at all. It just utilizes the position of the mouse for the objects to turn toward.
Nosson1459 Nosson1459

2017/3/3

#
I don't know what those scenarios have but I think that I got the code of using mouseOver boolean with Greenfoot.mouseMoved from those scenarios.
gurgi414 gurgi414

2017/3/3

#
Thank you this is all extremely helpful :)
You need to login to post a reply.