2011-08-07 7 views
0

私はSurfaceViewを拡張するビューを使用し、SurfaceViewに画像を描画するスレッドを使用して簡単なゲームを作成しています。ゲームには、ドロアブルを描き、更新する独自のスレッド(ゲームエンジン)があります。ロックされたキャンバスがヌルでない場合、Android SurfaceViewが空白に表示されます

私はこれを達成するために3つのクラス、つまりBattleActivity、BattleView、BattleThreadを持っています。 BattleActivityは別のアクティビティから呼び出されます。 BattleThreadはBattleView.update()とBattleView.render()を呼び出してジョブを実行します。しかし、私は何も働いていない。私はロギングとデバッグ(これは私が試したものです)を通してすべてが到達可能であることは知っていますが、どちらが間違っているかまだ分かりません。誰でも私を助けてくれませんか?


public class BattleActivity extends Activity { 
@Override 
public void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 

    // Making it full screen 
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); 

    //... shortened 

    // Create the battle view 
    battleView = new BattleView(this); 
    battleView.setBackground(battleBackground); 
    setContentView(battleView); 
}} 

BattleView:

public class BattleView extends SurfaceView implements SurfaceHolder.Callback{ 
//... shortened 

public BattleView(Context context) 
{ 
    super(context); 
    getHolder().addCallback(this); 
    // Surface has been created 
    battleThread = new BattleThread(getHolder(), this); 
    setFocusable(true); 
} 

public synchronized void render(Canvas canvas) 
{ 
    // this function is called when I do debugging, but no image is shown 
    // canvas and background is not null 
    canvas.drawBitmap(background, 0, 0, null); 
} 

public synchronized void update() 
{ 
    monsterState = leftMonster.update(); 
    //... shortened 
    rightMonster.update(); 
} 

@Override 
public void surfaceCreated(SurfaceHolder holder) 
{ 
    // Start the thread 
    battleThread.setRunning(true); 
    battleThread.start(); 
}} 

BattleThread:あなたはレンダリングの代わりに、あなたのレンダリングにonDraw(キャンバスキャンバス)を呼び出す必要が

public class BattleThread extends Thread { 
public BattleThread(SurfaceHolder surfaceHolder, BattleView battleView) 
{ 
    super(); 
    this.surfaceHolder = surfaceHolder; 
    this.battleView = battleView; 
} 

@Override 
public void run() 
{ 
    Canvas canvas; 
    //... shortened 

    while (running) 
    { 
     canvas = null; 
     // try locking the canvas for exclusive pixel editing 
     // in the surface 
     try 
     { 
      canvas = this.surfaceHolder.lockCanvas(); 
      synchronized (surfaceHolder) 
      { 
       beginTime = System.currentTimeMillis(); 
       framesSkipped = 0; // resetting the frames skipped 
       // update game state 
       this.battleView.update(); 

       beginTime = System.currentTimeMillis(); 
       // render it 
       this.battleView.render(canvas);    

       //... shortened 
      } 
     } 
     finally 
     { 
      if (canvas != null) 
      { 
       surfaceHolder.unlockCanvasAndPost(canvas); 
      } 
     } // end finally 
    } 
} 

}

答えて

1

私はこれが古い投稿だと知っています。

私はこの同じ問題を抱えていて、あなたとほとんど同じコードで、私の必要としたのは、postInvalidate()を私のSurfaceViewでオーバーライドしたonDraw(Canvas canvas)メソッドから呼び出したことです。それを完了

@Override 
protected void onDraw(Canvas canvas) 
{ 
    mPaperPlaneImage.draw(canvas); 
    postInvalidate(); 
} 
0

onDrawはwiで動作するため、ハードウェア画面バッファ2番目の例を確認してくださいHow can I use the animation framework inside the canvas?

+0

が、それは何とかうまくいきませんでした:

これは私のコードです。 :(現在、BattleViewのすべてのコードをロールバックして、動作しているかどうかを確認しようとしています。 – Febiyan

関連する問題