Android非常基本的游戏开发

时间:2011-07-02 08:27:41

标签: java 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();
    }
}
}

3 个答案:

答案 0 :(得分: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)

我认为对于平滑移动的对象,您应该使用触摸事件的手势监听器功能。

以下链接可以为您提供帮助。

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

答案 2 :(得分:0)

根据我的愿望测试了代码,它运行得非常流畅。

我建议在真实设备上测试这类项目,模拟器可能非常难以预测,也会因你的cpu而变慢。

不是什么大不了但你可以用

清除你的画布
canvas.drawColor(Color.BLACK);

而不是绘制矩形。