我正在用画布制作游戏。我需要这样做,所以当游戏获胜时,它会改变活动,但这只能在活动文件中完成。所以我的游戏是这样工作的:它从 MainActivity.class 开始,然后创建画布并将其设置为视图。
MainAcitivity.java:
public class MainActivity extends Activity {
private MainGamePanel game;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
// making it full screen
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
// set our MainGamePanel as the View
game = new MainGamePanel(this, 1);
setContentView(game);
}
}
MainGamePanel.java:
public class MainGamePanel extends SurfaceView implements
SurfaceHolder.Callback {
public MainGamePanel(Context context, int lvl) {
super(context);
this.context = context;
Level = lvl;
getHolder().addCallback(this);
setFocusable(true);
thread = new MainThread(getHolder(), this);
ball = new com.csdevelopers.canvas.sprite.Ball(
BitmapFactory.decodeResource(getResources(), R.drawable.ball),
100, 100);
if (Level == 1) {
lvl1 = new lvl1(context);
}
public void checkWon1(){
if(lvl1.checkWon(ball)){
// change Activity HERE!!!
}
}
}
因此,在公共 void checkWon1() 中,我无法更改 Activity,因为无法从非 Activity 类调用意图。我怎样才能回到 MainActivity 并告诉它更改 Activity?
如果您需要更多解释,请发表评论。