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

2015/4/27

How can I make the World use a variable from an Actor?

therealMV therealMV

2015/4/27

#
Hello everyone, I have a problem: I declared a double variable "room" in the World of my project, which is on 1.1 at the beginning of the game. Whenever the main character (an Actor) is at a certain point, I want him to change the variable. Furthermore I want the World to react to the change of "room". What I typed is: (in the world)
double room;    

    public double getRoom() {
        return room;
    }

public fearmine()
    {   
        super(1280, 719, 1); 

        room = 1.1;

        ...

        doorManagement();
}

(in the Actor)
World w;
    fearmine f;
    double room;
    
    public double getJRoom() {
        return room;
    }
    
    public void act() {                     
        w = getWorld(); 
        f = (fearmine) w; 
        room = f.getRoom(); 
        checkLocation();
    } 

public void checkLocation () {
        int xpos = getX();
        int ypos = getY();
        if (xpos <= 123) {            
            if (ypos < 366 && ypos > 230) {
                setLocation(1159, getY());  
                if (room == 1.1) { 
                    room = 1.2;                   
                }
                if (room == 1.2) {
                    room = 1.4;
                }
                if (room == 1.4) {
                    room = 1.6;
                }
            } else {
                setLocation(124, getY());
            }
        }
        if (xpos >= 1160) {            
            if (ypos < 366 && ypos > 230) {
                setLocation(124, getY());
                if (room == 1.2) {    
                    room = 1.1;
                } 
                if (room == 1.4) {
                    room = 1.2;
                } 
                if (room == 1.6) {
                    room = 1.4;
                }                    
            } else {
                setLocation(1159, getY());
            }
        }
}

(back in the World)

public void doorManagement() {        
        leftDoor = new Door();        
        rightDoor = new Door();
        upDoor = new Door();
        downDoor = new Door();  

        leftDoor.setImage(doorleft);
        rightDoor.setImage(doorright);
        upDoor.setImage(doorup);  
        downDoor.setImage(doordown);

        
            if (room == 1.1) {                     
                addObject(leftDoor, 61, 359);           
                removeObject(upDoor);
                removeObject(rightDoor);
                removeObject(downDoor);
            }
            if (room == 1.2) {                     
                addObject(leftDoor, 61, 359);
                addObject(upDoor, 640, 61);
                addObject(rightDoor, 1219, 359);
                removeObject(downDoor);
            }
            if (room == 1.3) {                     
                removeObject(leftDoor);
                removeObject(upDoor);
                removeObject(rightDoor);
                addObject(downDoor, 640, 658);
            }
            if (room == 1.4) {                     
                addObject(leftDoor, 61, 359);
                removeObject(upDoor);
                addObject(rightDoor, 1219, 359);
                addObject(downDoor, 640, 658);
            }
            if (room == 1.5) {                     
                removeObject(leftDoor);
                addObject(upDoor, 640, 61);
                removeObject(rightDoor);
                removeObject(downDoor);
            }
            if (room == 1.6) {                     
                removeObject(leftDoor);
                removeObject(upDoor);
                addObject(rightDoor, 1219, 359);
                removeObject(downDoor);
            }
        
    }
But the problem is, that the Actor (Joe1) doesn't change the variable. What did I do wrong? Please help!
danpost danpost

2015/4/27

#
Maybe you should add a setter method in the world class to set the value of the room variable:
public void setRoom(double roomVal)
{
    room = roomVal;
    // act on change in value here
}
Then, in your Joe1 class, you can (instead of 'room = 1.4', for example) use:
f.setRoom(1.4);
Using just 'room = 1.4;' you are only changing the local variable in the method, not the value of the field in the 'fearmine' class.
You need to login to post a reply.