2013-08-19 52 views
2

我正在使用画布进行游戏。我需要这样做,所以当游戏获胜时它会改变活动,但这只能在活动文件中完成。所以我的游戏是这样工作的:它从MainActivity.class开始,然后创建画布并将其设置为其视图。Android:从SurfaceView更改活动

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!!! 
    } 
} 

} 

所以在公共无效checkWon1()我不能够改变的活动,因为意图不能从被称为非活动类。我怎么能回到MainActivity并告诉它改变活动?

评论如果你需要更多的解释。

回答

1

你改变这样的:

Intent intent = new Intent().setClass(getContext(), ActivityToChange.class); 
((Activity) getContext()).startActivity(intent);