Reputation: 1858
I made a boolean called endGame, and when I click a button it will be set to false
, and then on another class I made an object for the class where my boolean is. And when something happen the endGame will be set to true
:
if(condition==true){ //the endGame variable will be equal to true only on this class
classObj.endGame=true;
}
//on the other class where the endGame is Located it is still false.
//button class
public boolean endGame;
public void create(){
endGame=false;
playButton.addListener(new InputListener(){
@Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
endGame=false;
System.out.println(endGame);
return super.touchDown(event, x, y, pointer, button);
}
});
}
//second class
if(sprite.getY()>=700){
buttonObj.endGame=true;
enemyIterator.remove();
enemies.remove(sprite);
}
Upvotes: 0
Views: 500
Reputation: 21688
you have several ways to fix that, maybe I'll say this is not the best, but without knowing anything of their code. because if the classes do not inherit from each other, or you can use a singleton pattern ?, I think this example may be worth it for you observer:
public class WraControlEndGame {
private ArrayList<EndGameOBJ> endGameOBJ = new ArrayList<EndGameOBJ>();
public void addEndGameOBJ(EndGameOBJ actor){
endGameOBJ.add(actor);
}
public void removeEndGameOBJ(EndGameOBJ actor){
endGameOBJ.remove(actor);
}
public void endGameOBJ_ChangeValue(boolean value){
for(int a = 0; a < endGameOBJ.size(); a++){
endGameOBJ.get(a).setEndGame(value);
}
}
}
.
public interface EndGameOBJ {
public void setEndGame(boolean value);
public boolean getEndGame();
}
.
public class YourClassThatNeedEndGameVariable implements EndGameOBJ{
..// other code
private boolean endGame = false;
..// other code Construct ect
@Override
public void setEndGame(boolean value) {
endGame = value;
}
@Override
public boolean getEndGame() {
return endGame;
}
}
.
in your code for example, this a pseudo code, you implements EndGameOBJ in your class you need, you view example in public class YourClassThatNeedEndGameVariable.
someClass buttonObj = new ....;//now this class implements EndGameOBJ
someClass classObj = new ....;//now this class implements EndGameOBJ
WraControlEndGame wraControlEndGame = new WraControlEndGame();
wraControlEndGame.addEndGameOBJ(buttonObj);
wraControlEndGame.addEndGameOBJ(classObj);
//bla bla bla
if(condition){
wraControlEndGame.endGameOBJ_ChangeValue(true);
}
I hope it will help and apologize for my English.
Upvotes: 0
Reputation: 393841
and then on another class I made an object for the class where my boolean is
I assume the endGame
variable is not static. Otherwise you wouldn't need to create an object of the class where the boolean is in order to access it.
This means that if you set endGame
to true in one object of the relevant class, it wouldn't update the value of endGame
in different objects of that class.
Upvotes: 1