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

2018/4/2

Problem with static/non-static methods

101929ha 101929ha

2018/4/2

#
I am making a jigsaw game, and am having trouble with when the jigsaw is completed. I want my game so that when all the pieces are in the right place, it runs
Greenfoot.stop();
. The problem is that the world cannot read the boolean (that is in the piece) because that 'cannot be referenced from static context'. I have tried making my piece's inPlace() method static, but then I have the same problem with getX() and getY(). Here is my piece's inPlace() method:
    public boolean inPlace()
    {
        if(getX() == 150 && getY() == 100)
        {
            return(true);
        }
        else
        {
            return(false);
        }
    }
and here is my world's jigsawFinished() method:
    public void jigsawFinished()
    {
        if(A1.inPlace() == true)
        {
            Greenfoot.stop();
        }
    }
(note that I have made it so that only one piece has to be in the right place, this is proof-of-concept for before I use every piece. Also note A1 is the name of the piece) Thanks.
Vercility Vercility

2018/4/2

#
Please post the complete class code. Also. you can shorten your inplace method to this:
return (getX() == 150 && getY() == 100);
the "== true" is unnecessary as well
101929ha 101929ha

2018/4/2

#
By class, do you mean the A1 class? If so,
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class A1 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class A1 extends A
{
    boolean drag = false;
    public void act()
    {
        gotomouse();
        snap();
        inPlace();
        if(Greenfoot.mousePressed(this))
        {
            drag = true;
        }
        if(Greenfoot.mouseClicked(null))
        {
            drag = false;
        }
    }
    public boolean inPlace()
    {
        if(getX() == 150 && getY() == 100)
        {
            return(true);
        }
        else
        {
            return(false);
        }
    }
    public void gotomouse()
    {
        if(drag)
        {
            MouseInfo aonemouse = Greenfoot.getMouseInfo();
            int aonemousex = aonemouse.getX();
            int aonemousey = aonemouse.getY();
            setLocation(aonemousex,aonemousey);
        }
    }
    public void snap()
    {
        if(getX() < 200 && getX() > 100)
        {
            setLocation(150,getY());
        }
        if(getX() < 300 && getX() > 200)
        {
            setLocation(250,getY());
        }
        if(getX() < 400 && getX() > 300)
        {
            setLocation(350,getY());
        }
        if(getX() < 500 && getX() > 400)
        {
            setLocation(450,getY());
        }
        if(getY() < 150 && getY() > 50)
        {
            setLocation(getX(),100);
        }
        if(getY() < 250 && getY() > 150)
        {
            setLocation(getX(),200);
        }
        if(getY() < 350 && getY() > 250)
        {
            setLocation(getX(),300);
        }
    }
}
danpost danpost

2018/4/3

#
If I am not mistaken, you are creating a class for each piece. That is 12 classes more than you need as the A class can be made to do what is needed for all the pieces in your game. I would build the game with all pieces properly placed (in solved position) so that each can save its original location in the world (its correct position). Then the pieces can be shuffled around. A boolean field can be added to retain the last state of being in its correct position or not. Then when the piece is moved, or when it is snapped into a location, it can tell if the state of correctness has changed. This field, in turn, will allow the game state to be tracked (the total completeness). Oh, and A1 is not the name of any piece. It is a name of a class that you create a piece from.
Vercility Vercility

2018/4/3

#
That's what I guessed, u can't use the method because you're trying to call it on a class and not on an object. You'd need to create an instance of A1 first and then call inPlace on that
101929ha 101929ha

2018/4/3

#
Thank you, Danpost, I figured I could put all the code in one class, but I don't know how to.
danpost wrote...
you are creating a class for each piece. That is 12 classes more than you need as the A class can be made to do what is needed for all the pieces in your game.
Are you are saying that I just need the 'A' class, put my code for the pieces in it and that can work? How do I set the images for each piece? Thanks
danpost danpost

2018/4/3

#
Yes -- it can work. Set the images as you create the objects in your world constructor.
101929ha 101929ha

2018/4/4

#
YAY! Thank you so much danpost, the puzzle works very well now!
nolttr21 nolttr21

2018/4/4

#
please post it so we know how it turned out.
You need to login to post a reply.