The current problem I am facing is getting values from my activity class. This is where im picking up my user input and passing it to my player to decide what should happen.
I am wondering if it makes sense to have a constructor in the activity class as well as the onCreate? I don't really understand the difference however I know that if I create a constructor in the class and set the value of the variable in the constructor it passes that variable. If I don't have a constructor the class is returning 0(null) to my Player class. I will include as little as possible and try to explain what I mean as I find it quite hard to explain without an example.
Game.java | Activity class
public class Game extends Activity implements SensorEventListener{
private int yDirection, xDirection;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new GameView(this));
}
@Override
public boolean onTouchEvent(MotionEvent motionEvent) {
switch (motionEvent.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
yDirection = 1;
break;
case MotionEvent.ACTION_UP:
yDirection = 2;
break;
}
return true;
}
public int getyDirection() {
return yDirection;
}
}
Player.java
public class Player {
Game userInput;
public Player(Context context){
userInput = new Game();
}
}
I havent included the part im calling it from however if I call the value from Game.java in the Player class here it will return 0(null) However if I log the variable name and view it from the Game.java class it will be the right value.
If I keep the player class the same but change the Game.java class as follows I will constantly get the value 5 returning as it is set in the constructor however it doesnt update as it should when called by the player class
Game.java | Activity class
public class Game extends Activity implements SensorEventListener{
private int yDirection, xDirection;
public Game(){
yDirection = 5;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new GameView(this));
}
@Override
public boolean onTouchEvent(MotionEvent motionEvent) {
switch (motionEvent.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
yDirection = 1;
break;
case MotionEvent.ACTION_UP:
yDirection = 2;
break;
}
return true;
}
public int getyDirection() {
return yDirection;
}
}
All help is appreciated!