VonnCC
VonnCC

Reputation: 457

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.

Upvotes: 6

Views: 28251

Answers (4)

You can access your variable as a public static.

Definition:

public  static Boolean isPremium;

Access (do not forget import) :

your_class.isPremium

Upvotes: 2

Suresh Atta
Suresh Atta

Reputation: 121998

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.

Upvotes: 12

NCA
NCA

Reputation: 820

It is possible to change the value any where in the project(that is in any class) since the variable is declared as public levelstatus.

If you want to change the value of the variable in many places and the new value should be the modification of previously changed value, the you should declare the variable as public static levelstatus.

Upvotes: 3

gorbos
gorbos

Reputation: 311

I believe it is more appropriate if you look into SharedPrefences of the Android API.

Check it here.

It could be used across the whole project of yours and it is handy in those kind of cases.

Upvotes: 2

Related Questions