I want to pass some values from my Game extends Activity to Screen extends SurfaceView using getters, but I always got 0 and I don’t know what’s happening.
This is my code for Game class:
public class Game extends Activity{
Screen screen;
Map map;
int mouseEvent;
private int mouseX;
private int mouseY;
private Bundle extra;
private int tileRows;
private int tileColumns;
private int minBlocks;
private int maxBlocks;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
getWindow().setFlags(LayoutParams.FLAG_FULLSCREEN, LayoutParams.FLAG_FULLSCREEN);
requestWindowFeature(Window.FEATURE_NO_TITLE);
extra = getIntent().getExtras();
tileRows = extra.getInt("numRow");
tileColumns = extra.getInt("numColumn");
minBlocks = extra.getInt("numMinBlock");
maxBlocks = extra.getInt("numMaxBlock");
setContentView(R.layout.game);
}
public boolean onTouchEvent(MotionEvent event){
mouseEvent = event.getAction();
mouseX = (int) event.getX();
mouseY = (int) event.getY();
return super.onTouchEvent(event);
}
public int getMouseX() {
return mouseX;
}
public int getMouseY() {
return mouseY;
}
public int getTileRows() {
return tileRows;
}
public int getTileColumns() {
return tileColumns;
}
public int getMinBlocks() {
return minBlocks;
}
public int getMaxBlocks() {
return maxBlocks;
}
public void setMouseX(int mouseX) {
this.mouseX = mouseX;
}
public void setMouseY(int mouseY) {
this.mouseY = mouseY;
}
}
And this is my code for Screen class:
public class Screen extends SurfaceView implements Callback
{
private Map map;
private SurfaceHolder holder;
private GameThread gamethread;
private Penguin penguin;
private boolean isSurfaceCreated;
private Bitmap tiles, character;
private int tileRows, tileColumns, minBlocks, maxBlocks;
public Screen(Context context, AttributeSet attb) {
super(context, attb);
tiles = BitmapFactory.decodeResource(getResources(), R.drawable.tile_sprites);
character = BitmapFactory.decodeResource(getResources(), R.drawable.penguin_sprite);
this.getHolder().addCallback(this);
this.tileRows = new Game().getTileRows();
this.tileColumns = new Game().getTileColumns();
this.minBlocks = new Game().getMinBlocks();
this.maxBlocks = new Game().getMaxBlocks();
}
}
Your help will be deeply appreciated 🙂
Activityclasses aren’t supposed to be instantiated by your code; they are created/re-used as needed when you useIntents. When you create anew Game(), there’s no intent associated with that and thus thegetExtrascalls in its constructor don’t find the integers you’re looking for–thus everything coming up as 0.If you know
Screenobjects are only used by theGameactivity, you could cast yourcontexttoGameand then call the getters directly.