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

2014/4/1

Can't find variable in either class

Napler Napler

2014/4/1

#
I am trying to set my MM_Main class's hasMoved to true using one of the moveSquares class's clickCheck() methods Here is the code to the classes
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class MM_Main here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class MM_Main extends Master_Mind_Pieces
{
    private boolean hasMoved = false;
    
    public void act() 
    {
        moveCheck();
    }
    
    private void moveCheck() {
        MouseInfo m = Greenfoot.getMouseInfo();
        if(m != null) {
            if(m.getButton() == 1 && m.getActor() == this && m.getClickCount() == 1) {
                spawnMS();
            }
        }
    }
    
    private void spawnMS() {
        if(getWorld() instanceof wd_back && !hasMoved) {
            wd_back b = (wd_back) getWorld();
            b.removeObjects(b.getObjects(obj_moveSquare.class));
            
            b.addObject(new obj_moveSquare(this), getX() + 1, getY() + 1);
            b.addObject(new obj_moveSquare(this), getX(), getY() - 1);
            b.addObject(new obj_moveSquare(this), getX() - 1, getY());
            b.addObject(new obj_moveSquare(this), getX() - 1, getY() - 1);
            b.addObject(new obj_moveSquare(this), getX() - 1, getY() + 1);
            b.addObject(new obj_moveSquare(this), getX() + 1, getY() - 1);
            b.addObject(new obj_moveSquare(this), getX() + 1, getY());
            b.addObject(new obj_moveSquare(this), getX(), getY() + 1);
        }
    }
}
and
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class obj_moveSquares here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class obj_moveSquare extends objects
{
    Actor a;

    public obj_moveSquare(Actor aa) {
        a = aa;
    }

    public void act() 
    {
        checkToMove();
    }    

    private void checkToMove() {
        if(getWorld() instanceof wd_back) {
            MouseInfo m = Greenfoot.getMouseInfo();
            if(m != null) {
                if(m.getButton() == 1 && m.getActor() == this  && m.getClickCount() == 1) {
                    a.setLocation(getX(), getY());
                    a.hasMoved = true;
                    getWorld().removeObjects(getWorld().getObjects(obj_moveSquare.class));
                }
            }
        }
    }
}
the problem is a.hasMoved = true; can't find the variable hasMoved. please help quickly. Thanks
erdelf erdelf

2014/4/1

#
you are trying to reach the MM main's hasMoved variable ( which is private btw ) but your code tries it with the variable a, which is an actor object reference and not a MM_Main object reference (basically, change line 11-13 of the obj_movesquare to
MM_Main a;
public obj_moveSquare(MM_Main aa)
)
danpost danpost

2014/4/1

#
There are a couple this that need attention here. First, 'a' is an Actor object (as per the field declaration statement) and therefore 'hasMoved' will not be found because the field will not be looked for in the MM_Main class. Second, the 'hasMoved' field in the MM_Main class has private access and therefore cannot be reached from outside the class. You either need to make it public access or add a public void method that will accept a value to set the field to:
public void setHasMoved(boolean moved)
{
    hasMoved = moved;
}
Even with the method, 'a' will have to be typed as a MM_Main object with '(MM_Main)a' so that a search for the method will include looking in the MM_Main class.
Napler Napler

2014/4/1

#
thanks, but my problem is that I need to set "hasMoved" in more than the MM_Main, I also have a few classes that use the same moveSquare class. And the problem exists when i change hasMoved to public.
Napler Napler

2014/4/1

#
I found a result that works. If I use a different variable for each class type, then I can pass in null or the type (depending on which one calls it).
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class obj_moveSquares here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class obj_moveSquare extends objects
{
    MM_Main MainM = null;
    MM_Minion MinionM = null;
    PB_Main MainP = null;
    PB_Minion MinionP = null;

    public obj_moveSquare(MM_Main aa, MM_Minion aaa, PB_Main aaaa, PB_Minion aaaaa) {
        if(aa != null) {
            MainM = aa;
        }
        if(aaa != null) {
            MinionM = aaa;
        }
        if(aaaa != null) {
            MainP = aaaa;
        }
        if(aaaaa != null) {
            MinionP = aaaaa;
        }
    }

    public void act() 
    {
        checkToMove();
    }

    private void checkToMove() {
        if(getWorld() instanceof wd_back) {
            MouseInfo m = Greenfoot.getMouseInfo();
            if(m != null) {
                if(m.getButton() == 1 && m.getActor() == this  && m.getClickCount() == 1) {
                    setLocation(getX(), getY());
                    if(MainM != null) {
                        MainM.hasMoved = true;
                    }
                    if(MinionM != null) {
                        MinionM.hasMoved = true;
                    }
                    if(MainP != null) {
                        MainP.hasMoved = true;
                    }
                    if(MinionP != null) {
                        MinionP.hasMoved = true;
                    }
                    getWorld().removeObjects(getWorld().getObjects(obj_moveSquare.class));
                }
            }
        }
    }
}
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class MM_Main here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class MM_Main extends Master_Mind_Pieces
{
    public boolean hasMoved = false;

    public void act() 
    {
        moveCheck();
    }
    
    private void moveCheck() {
        MouseInfo m = Greenfoot.getMouseInfo();
        if(m != null) {
            if(m.getButton() == 1 && m.getActor() == this && m.getClickCount() == 1) {
                spawnMS();
            }
        }
    }

    private void spawnMS() {
        if(getWorld() instanceof wd_back && !hasMoved) {
            wd_back b = (wd_back) getWorld();
            b.removeObjects(b.getObjects(obj_moveSquare.class));

            b.addObject(new obj_moveSquare(this, null, null, null), getX() + 1, getY() + 1);
            b.addObject(new obj_moveSquare(this, null, null, null), getX(), getY() - 1);
            b.addObject(new obj_moveSquare(this, null, null, null), getX() - 1, getY());
            b.addObject(new obj_moveSquare(this, null, null, null), getX() - 1, getY() - 1);
            b.addObject(new obj_moveSquare(this, null, null, null), getX() - 1, getY() + 1);
            b.addObject(new obj_moveSquare(this, null, null, null), getX() + 1, getY() - 1);
            b.addObject(new obj_moveSquare(this, null, null, null), getX() + 1, getY());
            b.addObject(new obj_moveSquare(this, null, null, null), getX(), getY() + 1);
        }
    }
}
Napler Napler

2014/4/1

#
One more thing. how could I set/check a list of actors's "haveMoved" = to true or false?
danpost danpost

2014/4/1

#
Napler wrote...
One more thing. how could I set/check a list of actors's "haveMoved" = to true or false?
You could use this for setting:
public void setHasMoved(java.util.List list, boolean value)
{
    for (Object obj : list) ((MM_Main)obj).hasMoved = value;
}
I am not sure how you mean as far as check a list for its values for 'hasMoved'. That is, I am not sure what you would be looking for as far as a return value. Unless you mean to do something to each one in the list that has a specific value. Then
for (Object obj : list) if (((MM_Main)obj).hasMoved == true) ... else ...;
Napler Napler

2014/4/2

#
thanks, i got what i need
You need to login to post a reply.