2012-12-05 54 views
2

我想做一個簡單的2D Android遊戲。在這個遊戲中,我試圖製作一些按鈕來控制角色,而角色則在SurfaceView中顯示。目前,SurfaceView只呈現黑屏。Android SurfaceView空

這是我的MainActivity,延伸Activity

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    GameView gm = (GameView)findViewById(R.id.snake_surface); 
    setContentView(R.layout.activity_main); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    getMenuInflater().inflate(R.layout.main, menu); 
    return true; 
} 

GameView類,它擴展SurfaceView

private HeroSprite hero; 
    public GameView(Context context, AttributeSet attributeSet) { 

     super(context, attributeSet); 

     gameLoopThread = new GameLoopThread(this); 

     getHolder().addCallback(new SurfaceHolder.Callback() { 

       public void surfaceDestroyed(SurfaceHolder holder) { 
         boolean retry = true; 
         gameLoopThread.setRunning(false); 
         while (retry) { 
           try { 
            gameLoopThread.join(); 
            retry = false; 
           } catch (InterruptedException e) {} 
         } 
       } 

       public void surfaceCreated(SurfaceHolder holder) { 
         createSprites(); 
         createHero(); 
         gameLoopThread.setRunning(true); 
         gameLoopThread.start(); 
       } 


       public void surfaceChanged(SurfaceHolder holder, int format,int width, int height) { 
       } 
     }); 
     bmpBlood = BitmapFactory.decodeResource(context.getResources(), R.drawable.blood1); 
    } 


     private void createHero(){ 
     hero=(createHeroSprite(R.drawable.aslan)); 

    } 
     private HeroSprite createHeroSprite(int resouce) { 
      Bitmap bmp = BitmapFactory.decodeResource(getResources(), resouce); 
      return new HeroSprite(this, bmp); 
    } 
    private void createSprites() { 
     sprites.add(createSprite(R.drawable.tavsan)); 
     sprites.add(createSprite(R.drawable.fare)); 
     sprites.add(createSprite(R.drawable.sansar)); 
     sprites.add(createSprite(R.drawable.tavsan)); 
     sprites.add(createSprite(R.drawable.fare)); 
     sprites.add(createSprite(R.drawable.sansar)); 
     sprites.add(createSprite(R.drawable.tavsan)); 
     sprites.add(createSprite(R.drawable.fare)); 
     sprites.add(createSprite(R.drawable.sansar)); 
     sprites.add(createSprite(R.drawable.tavsan)); 
     sprites.add(createSprite(R.drawable.tavsan)); 

    } 

    private Sprite createSprite(int resouce) { 
     Bitmap bmp = BitmapFactory.decodeResource(getResources(), resouce); 
     return new Sprite(this, bmp); 
    } 

    @Override 
    protected void onDraw(Canvas canvas) { 
     canvas.drawColor(Color.GREEN); 
     for (int i = temps.size() - 1; i >= 0; i--) { 
       temps.get(i).onDraw(canvas); 
     } 
     for (Sprite sprite : sprites) { 
       sprite.onDraw(canvas); 
     } 
     hero.onDraw(canvas); 
    } 

    @Override 
    public boolean onTouchEvent(MotionEvent event) { 
     if (System.currentTimeMillis() - lastClick > 300) { 
       lastClick = System.currentTimeMillis(); 
       float x = event.getX(); 
       float y = event.getY(); 
       synchronized (getHolder()) { 
         for (int i = sprites.size() - 1; i >= 0; i--) { 
           Sprite sprite = sprites.get(i); 
           if ((sprite).isCollition(x, y)) { 
            sprites.remove(sprite); 
            temps.add(new TempSprite(temps, this, x, y, bmpBlood)); 
            break; 
           } 
         } 
       } 
     } 
     return true; 
    } 

GameLoopThread類,它擴展Thread

static final long FPS = 10; 
private GameView view; 
private boolean running = false; 

public GameLoopThread(GameView view) { 
    this.view = view; 
} 

public void setRunning(boolean run) { 
    running = run; 
} 

@Override 
public void run() { 
    long ticksPS = 1000/FPS; 
    long startTime; 
    long sleepTime; 
    while (running) { 
     Canvas c = null; 
     startTime = System.currentTimeMillis(); 
     try { 
      c = view.getHolder().lockCanvas(); 
      synchronized (view.getHolder()) { 
       view.onDraw(c); 
      } 
     } finally { 
      if (c != null) { 
       view.getHolder().unlockCanvasAndPost(c); 
      } 
     } 
     sleepTime = ticksPS - (System.currentTimeMillis() - startTime); 
     try { 
      if (sleepTime > 0) 
       sleep(sleepTime); 
      else 
       sleep(10); 
     } catch (Exception e) { 
     } 
    } 
} 

這是我的佈局:

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

    <LinearLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="fill_parent" 
     android:layout_height="30dp" 
     android:gravity="center" 
     android:orientation="horizontal" > 

     <TextView 
      android:id="@+id/score" 
      android:layout_width="100dp" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_vertical|center_horizontal" 
      android:text="Score:" 
      android:textSize="20dp" /> 

     <Button 
      android:id="@+id/btnThread" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_vertical|center_horizontal" /> 

     <TextView 
      android:id="@+id/max" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_vertical|center_horizontal" 
      android:layout_marginLeft="100dp" 
      android:text="Max:" 
      android:textSize="20dp" /> 
    </LinearLayout> 

    <LinearLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:orientation="vertical" > 

     <FrameLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="0.3" > 

      <SurfaceView 
       android:id="@+id/snake_surface" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:layout_weight="0.3" /> 

     </FrameLayout> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="vertical" 
      android:weightSum="1" > 

      <Button 
       android:id="@+id/butUp" 
       android:layout_width="100dp" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center_horizontal|center" 
       android:text="UP" /> 

      <RelativeLayout 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" > 

       <Button 
        android:id="@+id/butLeft" 
        android:layout_width="100dp" 
        android:layout_height="wrap_content" 
        android:layout_alignParentLeft="true" 
        android:layout_alignParentTop="true" 
        android:text="LEFT" /> 

       <Button 
        android:id="@+id/butRight" 
        android:layout_width="100dp" 
        android:layout_height="wrap_content" 
        android:layout_alignParentRight="true" 
        android:layout_alignParentTop="true" 
        android:text="RIGHT" /> 
      </RelativeLayout> 

      <Button 
       android:id="@+id/butDown" 
       android:layout_width="100dp" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center_horizontal|center" 
       android:text="DOWN" /> 
     </LinearLayout> 

    </LinearLayout> 

</LinearLayout> 

回答

1

你在佈局視圖是SurfaceView,而不是一個GameView。你需要改變的是:

 <your.package.name.GameView 
      android:id="@+id/snake_surface" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_weight="0.3" /> 

,並用含有GameView包的實際名稱替換your.package.name

此外,行GameView gm = ...需要去setContentView

最後,當您修改將要繪製的項目時,請不要忘記調用gm.invalidate()

+0

解決了我的問題。感謝您的隊友,我一直在爲此工作3天。 – alp