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

2014/5/22

Add one to a variable from another class.

Crystic Crystic

2014/5/22

#
Yes, this has been answered many times before, however I cannot seem to apply it to my code. I have a variable "letterNum" in "GameWorld" class. I wish to add to this variable from an actor class (obj_letterBox). I want it to do this as it collides with another object (Which works perfectly). I have used the code: GameWorld.letterNum++; Which to me should work, saying go to GameWorld, and in there is letterNum, add one to that. But I get the error: "non-static variable letterNum cannot be referenced from a static context" But I have not declared a static anywhere. Could anyone give me a hand?
davmac davmac

2014/5/22

#
GameWorld is a class; you need to access the variable in the actual instance of the class - the world object.
((GameWorld) getWorld()).letterNum++;
You need to login to post a reply.