我有一个名为OurView
的班级。我有3 Buttons
来调用moveImage()
并将其传递给两个数字,具体取决于Button
。moveImage()
。
xCoord
应该会更改yCoord
和xCoord
的值,而这似乎也是如此。从yCoord
打印moveImage()
和delay()
时,它们会显示正确的值。
但是,从doDraw()
或package com.thatoneprogrammerkid.gameminimum;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class OurView extends SurfaceView implements SurfaceHolder.Callback {
private SurfaceHolder holder;
private Handler handler = new Handler();
private Bitmap testimg;
private int xCoord = 500;
private int yCoord = 500;
int xPos;
int yPos;
public OurView(Context context) {
super(context);
init();
}
public OurView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public OurView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
private void init() {
holder = getHolder();
holder.addCallback(this);
testimg = BitmapFactory.decodeResource(getResources(),R.drawable.testimg);
}
void moveImage(int xChange, int yChange) {
this.xCoord = this.xCoord + xChange;
this.yCoord = this.yCoord + yChange;
System.out.println("Move Image");
printCoords();
}
void printCoords() {
System.out.println(xCoord);
System.out.println(yCoord);
}
void delay() {
System.out.println("Delay");
printCoords();
handler.postDelayed(new Runnable() {
public void run() {
Canvas canvas = getHolder().lockCanvas();
System.out.println("Run");
printCoords();
if(canvas != null){
synchronized (getHolder()) {
doDraw(canvas, xCoord, yCoord);
}
holder.unlockCanvasAndPost(canvas);
}
}
}, 33);
}
void doDraw(Canvas canvas, int x, int y) {
xPos = this.xCoord + (testimg.getWidth()/2);
yPos = this.yCoord + (testimg.getHeight()/2);
System.out.println("Draw");
printCoords();
canvas.drawARGB(255, 55, 255, 255);
canvas.drawBitmap(testimg, x, y, null);
delay();
}
@Override
public void surfaceCreated(final SurfaceHolder holder) {
delay();
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {}
}
打印时,它们会显示原始值500.
我做错了什么导致变量没有更新?
以下是OurView类:
package com.thatoneprogrammerkid.gameminimum;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
public class SurfaceViewExample extends Activity {
OurView gameView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
gameView = new OurView(this);
setContentView(R.layout.activity_surface_view_example);
}
public void arrowPressed(View view) {
switch (view.getId()) {
case R.id.arrowLeft:
gameView.moveImage(-1,0);
break;
case R.id.arrowRight:
gameView.moveImage(1,0);
break;
case R.id.arrowUp:
gameView.moveImage(0,1);
break;
}
}
}
这是按钮按下的句柄:
510
500
编辑:
澄清一下,按下按钮10次后,moveImage()中的printCoords()会显示以下内容:
500
500
并且所有其他位置的printCoords()显示以下内容:
Test.php
答案 0 :(得分:0)
如果我猜对了,你用来调用moveImage()和delay()的引用是不一样的。也许你做了类似的事情:
ourView = new OurView();
调用moveImage后。