2013-08-19 54 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);