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

2011/12/4

Referencing actors from a non-actor

darkmist255 darkmist255

2011/12/4

#
Right now my workaround for this is to make my class "Stats" which holds the players stats an actor and put it in the world at (-20, -20). But I think eventually I will want to avoid doing that. How can I make a reference to an actor from a class that isn't in the world? All of my references so far are like "someActor actor = getWorld().someActor;" then for an internal variable "int PotatoesMade = actor.potatoesMade;" How can I access it without the getWorld()?
bourne bourne

2011/12/4

#
I have found something like the following to work, Say your World is named "Screen". Inside of there have a public static variable of type "Screen" called "main" and in the constructor, set "main" to "this".
Public class Screen extends World
{
public static Screen main;

public Screen()
{
super(600, 400);
main = this;
}
}
Then from a non-actor class, instead of "getWorld()" you would use "Screen.main"
darkmist255 darkmist255

2011/12/5

#
I think I'll keep it as I have it now (since I already coded most parts), but I'll look at this again next time I want to do this. Thanks :D!
You need to login to post a reply.