2016-04-27 57 views
0

我正在開發2D遊戲,我正在嘗試使視圖在繪製和更新所有內容時告訴包含遊戲結束的活動並顯示彈出窗口屏幕。問題是我不知道如何從視圖訪問活動中的方法,因爲它們在不同的線程上工作。android-call方法從活動中查看

代碼看起來或多或少像這樣,我想從視圖類訪問方法displayFrame()。實際代碼中的方法會對其他只有活動才能訪問的textView等進行更多操作。

public class MainActivity extends Activity{ 

    private MyView myView; 
    RelativeLayout finalFrame; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     myView = new MyView(this.getApplicationContext()); 
     this.setContentView(myView); 

     finalFrame = (RelativeLayout) findViewById(R.id.framefinal); 
    } 

    private void displayFrame(){ 
     this.finalFrame.setVisibility(View.VISIBLE); 
    } 
} 

public class MyView extends SurfaceView implements SurfaceHolder.Callback{ 
    private MyThread myThread; 
    private boolean finished = false; 

    public MyView(Context context) { 
     super(context); 

     getHolder().addCallback(this); 
     this.myThread = new MyThread(getHolder(), this); 

     this.myThread.setRunning(true); 
    } 

    public void update() { 
     this.finished = someMethod(); 
     if(this.finished){ 
      MainActivity.displayFrame(); 
     } 
    } 

    public void draw(Canvas canvas){ 
     //draw something 
    } 

    @Override 
    public void surfaceCreated(SurfaceHolder holder) { 

    } 

    @Override 
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { 

    } 

    @Override 
    public void surfaceDestroyed(SurfaceHolder holder) { 

    } 
} 

public class MyThread extends Thread{ 
    private MyView myView; 
    private SurfaceHolder surfaceHolder; 
    private boolean running = false; 
    private static Canvas canvas; 

    public MyThread(SurfaceHolder surfaceHolder, MyView myView){ 
     super(); 
     this.surfaceHolder = surfaceHolder; 
     this.myView = myView; 
    } 

    @Override 
    public void run(){ 
     while(running){ 
      canvas = null; 

      try { 
       canvas = this.surfaceHolder.lockCanvas(); 
       synchronized (surfaceHolder) { 
        this.myView.update(); 
        this.myView.draw(canvas); 
       } 
      } catch (Exception e) {e.printStackTrace();} 
      finally{ 
       if(canvas!=null) 
       { 
        try { 
         surfaceHolder.unlockCanvasAndPost(canvas); 
        } 
        catch(Exception e){e.printStackTrace();} 
       } 
      } 
     } 
    } 

    public void setRunning(boolean running){ 
     this.running = running; 
    } 
} 
+0

公共類MainActivity擴展活動{ 公共類MainActivity擴展活動{時就是這樣或者只是粘貼問題 – Haroon

+0

它不是...對不起:P編輯它(只是一個粘貼問題) – TBone91

回答

1

最簡單的方法是在視圖中定義一個監聽器接口,並使活動實現它。然後你可以通知活動你的遊戲已經結束並執行適當的邏輯。

public class MyView extends SurfaceView implements SurfaceHolder.Callback { 

    // your other fields 
    private GameListener mGameListener; 

    public void setGameListener(GameListener gameListener) { 
     mGameListener = gameListener; 
    } 

    public void update() { 
     // your logic 
     if (mGameListener != null) { 
      // calling onGameEnd on the main thread 
      post(new Runnable() { 

       public void run() { 
        mGameListener.onGameEnd(); 
       } 

      }); 
     } 
    } 

    // rest of your class as it is 

    public interface GameListener { 

     void onGameEnded(); 

    } 
} 

public class MainActivity extends Activity implements GameListener { 

    // the rest of your class 

    public void onGameEnded() { 
     // call the method you need 
    } 
} 
+0

這工作完美。謝謝:) – TBone91

+0

更新:佈局是構建,但沒有繪製....我可以點擊視圖上的按鈕,但我不能以圖形方式看到它或它包含任何東西 – TBone91

+0

哪個佈局沒有完全繪製? –

0
public class MyView extends SurfaceView implements SurfaceHolder.Callback{ 
private MyThread myThread; 
private boolean finished = false; 
private Context context; 

public MyView(Context context) { 
    super(context); 
    this.context=context; 

    getHolder().addCallback(this); 
    this.myThread = new MyThread(getHolder(), this); 

    this.myThread.setRunning(true); 
} 

runOnUiThread(new Runnable(){ 
@Override 
public void run(){ 
    update(); 
} 
}); 

    public void update() { 
    this.finished = someMethod(); 
    if(this.finished){ 
     MainActivity mainActivity = (MainActivity) context; 
     mainActivity.displayFrame(); 
    } 
} 

public void draw(Canvas canvas){ 
    //draw something 
} 

@Override 
public void surfaceCreated(SurfaceHolder holder) { 

} 

@Override 
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { 

} 

@Override 
public void surfaceDestroyed(SurfaceHolder holder) { 

} 
} 

你可以試試這個

+0

這不起作用,因爲我無法將上下文轉換爲MainActivity – TBone91

+0

您的意思是您無法投射,什麼妨礙您這樣做? – Haroon

+0

它看起來像上下文改變,我收到一個錯誤,說它不能被鑄造 – TBone91