Changing Variable value from another class
Asked Answered
A

4

6

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.

Altis answered 20/9, 2013 at 7:7 Comment(0)
Q
12

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.

Qulllon answered 20/9, 2013 at 7:9 Comment(0)
I
3

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.

Incognizant answered 20/9, 2013 at 7:14 Comment(0)
B
2

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.

Balls answered 20/9, 2013 at 7:12 Comment(0)
E
2

You can access your variable as a public static.

Definition:

public  static Boolean isPremium;

Access (do not forget import) :

your_class.isPremium
Electrostatic answered 30/12, 2018 at 10:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.