2015-05-11 19 views
-3

這裏是擴展表面視圖的類,在其運行方法中我稱之爲動畫球的方法,但球心不是動畫我想在android上做一個乒乓球遊戲,但我卡住了,我不知道爲什麼球沒有移動

public class OurView extends SurfaceView implements Runnable 
{ 
    Canvas c; 
    Thread t = null; 

    SurfaceHolder holder; 
    boolean isItOk = false; 
    Rect racketTop,racketBottom; 


    int TopLeft ; 
    int TopRight ; 
    int TopTop ; 
    int TopBottom; 

    int bottomLeft; 
    int bottomRight; 
    int bottomTop; 
    int bottomBottom; 

    GameState state; 



    public OurView(Context context) { 
     super(context); 
     holder = getHolder(); 
     state = new GameState(); 
    } 

    @Override 
    public void run() { 

     while (isItOk == true) { 

      if (!holder.getSurface().isValid()) 
       continue; 

      c = holder.lockCanvas(); 
      c.drawARGB(0, 0, 0, 0); 
      TopLeft = ((c.getWidth()/2)/2) + 50; 
      TopRight = (c.getWidth()/2) + ((c.getWidth()/2)/2) - 50; 
      TopTop = getTop(); 
      TopBottom = 10; 

      bottomLeft = ((c.getWidth()/2)/2) + 50; 
      bottomRight = (c.getWidth()/2) + ((c.getWidth()/2)/2) - 50; 
      bottomTop = getBottom() - 10; 
      bottomBottom = getBottom(); 


      racketTop = new Rect();                 // for top racket 
      racketTop.set(TopLeft, TopTop, TopRight, TopBottom);          //left = ((c.getWidth()/2)/2)+50 
                               //top = getTop() 
                               //right = (c.getWidth()/2)+((c.getWidth()/2)/2)-50 
                               //bottom = 10 


      racketBottom = new Rect();               // FOR BOTTOM RACKET 
      racketBottom.set(bottomLeft, bottomTop, bottomRight, bottomBottom);             //left = ((c.getWidth()/2)/2)+50 
                               // top = getBottom()-10 
                               // right = (c.getWidth()/2)+((c.getWidth()/2)/2)-50 
                               // bottom = getBottom() 


      Paint white = new Paint(); 
      white.setColor(Color.WHITE); 
      white.setStyle(Paint.Style.FILL); 

      c.drawRect(racketTop, white); 
      c.drawRect(racketBottom, white); 

在這裏,我調用的方法

  state.update(); 

      state.draw(c,white); 
      holder.unlockCanvasAndPost(c); 




     } 
    } 
    public void pause() 
    { 
     isItOk = false; 
     while(true) 
     { 
      try 
      { 
       t.join(); 
      } 
      catch(InterruptedException e) 
      { 
       e.printStackTrace(); 
      } 

      break; 
     } 

     t = null; 
    } 

    public void resume() 
    { 
     isItOk = true; 
     t = new Thread(this); 
     t.start(); 


    } 

這裏是球定義的類

 public class GameState { 




int ballx,bally;  //ball x and y position 
int ball_size = 20;   // size of ball 
static int ballvelx = 3; // velocity x of ball 
static int ballvely = 3; // velocity y of ball 



public GameState() { 

} 




public void update() 
{ 
    ballx += ballvelx; 
    bally += ballvely; 

} 

public void draw(Canvas canvas, Paint paint) 
{ 
    //objects color 
    paint.setColor(Color.WHITE); 

    ballx = canvas.getWidth()/2; 
    bally = canvas.getHeight()/2; 

    //ball 
    canvas.drawCircle(ballx,bally,ball_size,paint); 
} 

}

幫助我,這是我第一次的Android遊戲

回答

1

不會對你的代碼的其餘部分來看:

ballx = canvas.getWidth()/2; 
bally = canvas.getHeight()/2; 

這將重置你的球的每次繪製時的位置。難怪它不會移動,當你重置其位置。
改爲撥打update(),讓球移動。

+0

謝謝解決了我的問題,但問題來了,無論何時球移動它留下一條線。 – Red

+2

那麼你沒有清除每一幀的屏幕。 – FruitAddict