2011-07-02 149 views
1

任何人都可以用這個簡單的代碼來幫助我嗎? 爲什麼圓不能順利移動? 它有什麼問題?Android非常基礎的遊戲開發

package chaseme; 

import android.app.Activity; 
import android.content.Context; 
import android.graphics.Canvas; 
import android.graphics.Color; 
import android.graphics.Paint; 
import android.graphics.Point; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.SurfaceHolder; 
import android.view.SurfaceView; 
import android.view.Window; 

public class ChaseMe extends Activity { 
/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    setContentView(new SampleView(this)); 
} 

private class SampleView extends SurfaceView implements SurfaceHolder.Callback, Runnable { 
    private Point point; 
    private Thread thread; 
    private SurfaceHolder holder; 
    private Paint _rect; 
    private Paint _circle; 
    private boolean running; 
    private int WIDTH; 
    private int HEIGHT; 
    private float radius = 20; 

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

     point = new Point(20, 20); 
     _rect = new Paint(); 
     _rect.setColor(Color.BLACK); 

     _circle = new Paint(); 
     _circle.setColor(Color.BLUE); 

     holder = this.getHolder(); 
     holder.addCallback(this); 
     thread = new Thread(this); 

    } 

    private void updateModel() { 
     if(point.x > WIDTH - radius) { 
      point.x = 20; 
     } 
     if(point.y > HEIGHT - radius) { 
      point.y = 20; 
     } 
     point.x++; 
     point.y++; 
    } 

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

    @Override 
    public void surfaceCreated(SurfaceHolder holder) { 
     WIDTH = getWidth(); 
     HEIGHT = getHeight(); 
     this.setRunning(true); 
     thread.start(); 
    } 

    @Override 
    public void surfaceDestroyed(SurfaceHolder holder) { 
     boolean retry = true; 

     this.setRunning(false); 
     while (retry) { 
      try { 
       thread.join(); 
       retry = false; 
      } 
      catch (InterruptedException e) {} 
     } 
    } 

    private void setRunning(boolean b) { 
     running = b; 
    } 

    @Override 
    public void run() { 
     while(running) { 
      Canvas c = null; 
      updateModel(); 

      try { 
       c = holder.lockCanvas(null); 
       // synchronized (holder) {      
        render(c); 
       // } 
      } 
      catch(Exception e) { 
       Log.e("main", e.getMessage()); 
      } 
      finally { 
       if(c!=null) { 
        holder.unlockCanvasAndPost(c); 
       }     
      }    
     } 
    } 

    private void render(Canvas c) {   
     c.drawRect(0, 0, WIDTH, HEIGHT, _rect);  
     c.drawCircle(point.x, point.y, radius , _circle); 

     //c.save(); 
     //c.restore(); 
    } 
} 
} 
+0

如果你在渲染控制檯中打印一些東西,幀頻是否好?我不知道你的應用運行速度是否太快,因爲你從來不會讓你的線程在動畫幀之間進入睡眠狀態。 – Snicolas

+0

嗯,我知道是什麼問題,它是模擬器運行速度非常慢,我跑了一個測試,看到FPS,它從大約20下降到10. – Yotam

+0

一個設備是唯一的真實世界測試在Android上。這也是調試應用程序的最快方式。我只使用模擬器來測試我的設備無法測試的東西(分辨率,sdk版本......)。但通常我會在設備上測試我的代碼。 – Snicolas

回答

1

你不能像目前那樣調用point.x ++。您需要計算相對於經過時間和屏幕大小的移動。

第1步:在每一個幀,計算出多少時間做

long now = System.currentTimeMillis(); 
elapsed = (now - mLastTime); 
totalTimeElapsed += elapsed; 

,然後在你的主循環的結束,因爲最後一幀通過你做

mLastTime = now; 

第2步。獲取屏幕比例:

screenWidth = MyClass.this.getWidth(); 
screenHeight = MyClass.this.getHeight(); 
float a = screenWidth; 
float b = screenHeight; 
screenRatioX = a/WIDTH_OF_YOUR_PHONE; 
screenRatioY = b/HEIGTH_OF_YOUR_PHONE; 

第3步。現在您可以開始製作動畫了,例如,如果您想要從右向左移動你的圈子:

spriteX1 = (int) ((spriteX1 + (VELOCITY*screenRatioX*elapsed))+0.5); 
spriteX2 = spriteX1 + spriteWidth; 

以2.0或其他東西的速度開始並從那裏調整。

祝你好運!

1

我認爲對於平滑移動的對象,您應該使用觸摸事件的手勢監聽器功能。

下面的鏈接可能會幫助您。

1.http://mobile.tutsplus.com/tutorials/android/android-gesture/ 2.http://stackoverflow.com/questions/937313/android-basic-gesture-detection

0

根據我的願望測試了代碼,並且運行非常流暢。

我建議在真實設備上測試這種類型的項目,模擬器可能非常不可預測,速度也取決於您的cpu。

沒什麼大不了的,但你可以用

canvas.drawColor(Color.BLACK); 

透明的而不是繪製一個矩形畫布。