我想从我的游戏扩展活动传递一些值到屏幕扩展SurfaceView使用getter,但我总是0,我不知道发生了什么。
这是我的Game类代码:
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;
}
}
这是我的Screen类代码:
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();
}
}
我们将非常感谢您的帮助:)
答案 0 :(得分:1)
Activity
类不应该由您的代码实例化;当您使用Intent
时,会根据需要创建/重新使用它们。当您创建new Game()
时,没有与之关联的意图,因此其构造函数中的getExtras
调用找不到您正在寻找的整数 - 因此所有内容都会显示为0。
如果您知道Screen
个对象仅由Game
活动使用,您可以将context
转换为Game
,然后直接调用getter。