我目前正在为Android for java设计一个基本精灵类,因为我不想使用2D引擎,Google也不会出于某种原因提供任何精灵API ...
无论如何,我的问题是关于我设计的课程。我有一个Sprite类,它采用位图并将其转换为" sprite"。然后,当用户创建一个新的" SpriteContainer"
时,可以将此精灵传递给我创建的自定义视图。我的DynamicSpriteContainer代码如下:
package com.me.spriteGraphics;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class DynamicSpriteContainer extends SurfaceView implements SurfaceHolder.Callback {
private SurfaceHolder holder;
public int x, y;
private Bitmap b;
public Sprite sprite;
private Canvas c = new Canvas();
private boolean bool = false;
public DynamicSpriteContainer(Context context, Sprite spriteImage) {
//public ImageView(Context context) {
super(context);
//get sprite
b = spriteImage.getBitmap();
//set holder
holder = getHolder();
holder.addCallback(this);
//set position
x = y = 0;
update();
}
public void surfaceCreated(SurfaceHolder holder) {
c = holder.lockCanvas();
c.drawARGB(255, 150, 150, 10);
//set up bitmap image
c.drawBitmap(b, x, y, null);
holder.unlockCanvasAndPost(c);
update();
}
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
public void surfaceDestroyed(SurfaceHolder holder) {
}
private void update() {
//this code will alter the character in the way the developer specifies (per frame)
Thread repeat = new Thread( new Runnable() {
public void run() {
try {
//move sprite in constant motion
while (bool) {
//redraw
c = holder.lockCanvas();
c.drawARGB(255, 150, 150, 10);
//set up bitmap image
c.drawBitmap(b, x, y, null);
holder.unlockCanvasAndPost(c);
}
} catch (Error e) {
e.printStackTrace();
bool = false;
}
}
});
repeat.start();
}
public void setX(int x) {
//notice how this same code above has the holder, the holder is recognized above, not here.
c = holder.lockCanvas();
c.drawARGB(255, 150, 150, 10);
//set up bitmap image
c.drawBitmap(b, x, y, null);
holder.unlockCanvasAndPost(c);
}
//set the info on pause, resume, stop...
//this data is for error stopping, so the app can resume and stop without passing an error to the user!
public void passInfo(boolean isPaused, boolean isResumed, boolean isStopped) {
if (isPaused && sprite.auto) {
bool = false;
}
if (isResumed && sprite.auto) {
bool = true;
}
if (isStopped && sprite.auto) {
bool = false;
}
}
}
对于长代码摘录感到抱歉。
主要问题是在update()函数中,持有者被识别,但在setX()中,它不是......
如何让setX()识别持有者?
提前多谢你!!!