2014-10-19 66 views
0

我製作了一個程序來畫一個在屏幕上彈跳的球。昨天工作得很好,但我只是裝了起來,現在也不會在正確的X位置上繪製,它只是停留在0 Y還細,但..Eclipse,drawCircle函數沒有繪製到正確的位置

class GameThreadView extends SurfaceView implements Runnable { 


// for thread 
Thread renderThread = null; 
// volatile equals keep order of execution 
volatile boolean running = false; 
// for canvas, also used to synchronize 
SurfaceHolder holder; 
GameThreadAndroid gta; 
Paint bg; 
Paint fg; 
// game stuff 
int frameCount = 0; 
int count = 0; 
float aDeltaTime = 0f; 
float accumTime = 0f; 

int touchX = -50; 
int touchY = -50; 

float ballX, ballY = 0.5f; 

float moveX, moveY = 0.2f; 

Random r = new Random(); 

public GameThreadView(Context context) { 
    super(context); 
    gta = (GameThreadAndroid)context; 

    holder = getHolder(); 

    fg = new Paint(Paint.ANTI_ALIAS_FLAG); 
    fg.setTextSize(36); 
    fg.setTextAlign(Paint.Align.CENTER); 
    fg.setColor(getResources().getColor(R.color.text)); 

    bg = new Paint(); 
    bg.setColor(getResources().getColor(R.color.background)); 
    Log.d("GAME","GameThreadView"); 

    // 
    moveY = r.nextFloat() * 0.5f; 
} 

/* 
    * Actually always starts a new thread 
    */ 
public void resume() {   
    running = true; 
    renderThread = new Thread(this); 
    renderThread.start();   
} 
/* 
    * Actually kills thread 
    */ 
public void pause() {   
    running = false; 

    while(true) { 
    try { 
    renderThread.join(); 
    break; 
    } catch (InterruptedException e) { 
    // retry 
    } 
    } 
    renderThread = null;   
} 

public void run() { 
    long startTime = System.nanoTime(); 

    while(running) { 
    if(!holder.getSurface().isValid()) 
    continue; 

    float deltaTime = (System.nanoTime()-startTime)/1000000000.0f; 
    startTime = System.nanoTime(); 

    updateGame(deltaTime); 

    Canvas canvas = holder.lockCanvas();    
    drawSurface(canvas);           
    holder.unlockCanvasAndPost(canvas);    
    } 
} 
// All game logic 
private void updateGame(float deltaTime) { 
    accumTime += deltaTime; 
    count++;   

    if (accumTime > 1f) { 
    aDeltaTime = deltaTime; 
    frameCount = count; 
    count = 0; 
    accumTime = 0; 
    Log.d("GAME", String.format("frame count = %d", frameCount)); 
    } 

    // move the ball 
    ballX += moveX * deltaTime; 
    ballY += moveY * deltaTime; 

    // hit walls 
    if(ballX > 1.0f){ 
    ballX = 1.0f; 
    moveX = -moveX; 
    } 
    if(ballY > 1.0f){ 
    ballY = 1.0f; 
    moveY = -moveY; 
    } 
    if(ballX < 0.0f){ 
    ballX = 0.0f; 
    moveX = -moveX; 
    } 
    if(ballY < 0.0f){ 
    ballY = 0.0f; 
    moveY = -moveY; 
    } 
} 

private void drawSurface(Canvas canvas) {  
    canvas.drawPaint(bg); 

    int w = canvas.getWidth()/2; 
    canvas.drawText(Integer.toString(frameCount), w, canvas.getHeight() * 1/3, fg);   
    canvas.drawText(Float.toString(aDeltaTime), w, canvas.getHeight() * 2/3, fg); 

    canvas.drawCircle(ballX * canvas.getWidth(), ballY * canvas.getHeight(), 20, fg); 
} 


/* 
    * Called from outside this object so must be synchronized 
    * @see android.view.View#onTouchEvent(android.view.MotionEvent) 
    */ 
public boolean onTouchEvent(MotionEvent event) { 
    synchronized (this) { 
    switch (event.getAction()) { 
    case MotionEvent.ACTION_DOWN: 
    case MotionEvent.ACTION_MOVE: 
    case MotionEvent.ACTION_UP: 
    touchX = (int) event.getX(); 
    touchY = (int) event.getY(); 
    break; 
    } 
    return true; 
    } 
} 

回答

0

我認爲問題是,你從來沒有在這裏初始化的Movex(與零初始化):

float moveX, moveY = 0.2f; 

和,在接下來的方法,你重新設置moveY,但MOVEX仍與零值:

public GameThreadView(Context context) { 
    super(context); 
    gta = (GameThreadAndroid)context; 

    holder = getHolder(); 

    fg = new Paint(Paint.ANTI_ALIAS_FLAG); 
    fg.setTextSize(36); 
    fg.setTextAlign(Paint.Align.CENTER); 
    fg.setColor(getResources().getColor(R.color.text)); 

    bg = new Paint(); 
    bg.setColor(getResources().getColor(R.color.background)); 
    Log.d("GAME","GameThreadView"); 

    // Here you set moveY 
    moveY = r.nextFloat() * 0.5f; 

    // You need to add somthing similar to moveX, like this 
    moveX = r.nextFloat() * 0.5f; 


} 

隨着這一變化,它應該運行預計。

希望它可以幫助你:)

+0

啊謝謝你!不知道我是怎麼錯過的 – user3126325 2014-10-20 20:15:06