Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing Variable value from another class

I have an integer(levelstatus) in class A(LevelSelectScene) and I want to change it in class B(GameScene), is it possible?

here is my code:

(Code in GameScene)

public class levelComplete()
{

levelSelectScene.getInstance.levelstatus=1;

}

LevelSelectScene has an public integer levelstatus.

and after an event happens, levelComplete will trigger and will change the value of levelstatus from null to 1.

like image 573
VonnCC Avatar asked Sep 20 '13 07:09

VonnCC


People also ask

How do you use a variable from one class in another class?

You need to make the variables in class aaa as class variables, and then you can use these variables of class aaa in class bbb by using object of class aaa. e.g. aaa obj2=new aaa();

How do I change the value of a class variable in C#?

There's a few ways of updating instance variables in C#: Using properties or setters like in Java or instance methods. Following code shows you a few of those. This is the object-oriented way of doing things. As for global variables in the sense of application-wide variables, you can refer to this.


1 Answers

Yes.

Make your levelstatus variable as static.

Because I guess you need to change that variable in each level class.

That means,you are wanting to access that variable through out the whole Game(All levels).

And also,Declare that variable in a Util class(proposing a name LevelUtil),Since it wont attach to a single level.

like image 85
Suresh Atta Avatar answered Sep 23 '22 02:09

Suresh Atta