我做了一个程序来画一个球在屏幕上弹跳。它昨天工作正常,但我只是加载它,现在它不会在正确的X位置绘制,它只是停留在0. Y仍然很好但是..
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 :(得分: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;
}
通过该更改,它应该按预期运行。
希望它可以帮助你:)