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

2021/2/27

How to refer to a variable in the World from an Actor

Znarf Znarf

2021/2/27

#
So I've got this code in my world:
public class Snakegame extends World
{
    
    public Snakegame() 
    {
        super(15, 15,32);
        
        GreenfootImage Gras = new GreenfootImage("gras.png");;
        setBackground(Gras);
        
        Snakehead snakehead = new Snakehead();
        addObject(new Snakebody (),7,7);
        addObject(snakehead,7,7);
        addObject(new Snaketail (),5,7);
        
        int SnakeX = snakehead.getX();
        int SnakeY = snakehead.getY();
    }
}
And I wanna refer to SnakeX and SnakeY from an Actor and I don't know how.
Gbasire Gbasire

2021/2/27

#
write this in your Actor class :
SnakeX = ((Snakegame)getWorld()).SnakeX;
SnakeY = ((Snakegame)getWorld()).SnakeY;
or alternatively if you have only one snake in the World write this in your Actor class :
Snakehead snakehead = (Snakehead)getWorld().getObjects(Snakehead.class).get(0);
snakeX = snakehead.getX();
snakeY = snakehead.getY();
danpost danpost

2021/2/27

#
Remove lines 16 and 17. There values (1) will only ever be 7 and 7; and (2) will only exist for an instant. Because all codes here are in your world constructor, they are executed only once. So, if they had any chance, SnakeX and SnakeY would never be updated. And because they are declared within the constructor, they don't even exist once the constructor is done executing (once the world is created). From an actor, you can use:
Actor head = (Actor)getWorld().getObjects(Snakehead.class).get(0);
int snakeX = head.getX(), snakeY = head.getY();
danpost danpost

2021/2/27

#
Gbasire wrote...
write this in your Actor class :
SnakeX = ((Snakegame)getWorld()).SnakeX;
SnakeY = ((Snakegame)getWorld()).SnakeY;
SnakeX and SnakeY will not be found. Those variables were local to the constructor.
or alternatively if you have only one snake in the World write this in your Actor class :
Snakehead snakehead = (Snakehead)getWorld().getObjects(Snakehead.class).get(0);
snakeX = snakehead.getX();
snakeY = snakehead.getY();
Unless snakeX and snakeY were declared variables in the class, they will not be found. Lines 2 and 3 would need to begin with "int ".
Znarf Znarf

2021/2/27

#
It worked thank you. You really have been a big help danpost. Nearly every disscussion I look at you answered. It's really great that your helping at so many people including myself :)
Gbasire Gbasire

2021/2/27

#
danpost wrote...
Gbasire wrote...
write this in your Actor class :
SnakeX = ((Snakegame)getWorld()).SnakeX;
SnakeY = ((Snakegame)getWorld()).SnakeY;
SnakeX and SnakeY will not be found. Those variables were local to the constructor.
or alternatively if you have only one snake in the World write this in your Actor class :
Snakehead snakehead = (Snakehead)getWorld().getObjects(Snakehead.class).get(0);
snakeX = snakehead.getX();
snakeY = snakehead.getY();
Unless snakeX and snakeY were declared variables in the class, they will not be found. Lines 2 and 3 would need to begin with "int ".
Yes that's right, I was thinking it was obvious but I should have wrote it as well.
You need to login to post a reply.