我将位图移动到画布内部 我希望当这个位图关闭到屏幕的右边界时,即: x位置< getWidth()具有一定的小值 我想翻译画布坐标,以便画布本身也沿着位图内部的移动移动
另一种意义; 我希望位图继续永久向右移动,当然当它关闭到屏幕的右边界时它很快就会消失所以我希望画布本身随我移动以使位图始终可见 我想知道如何在翻译画布后设置x位置值
即:所有具有角色的游戏向右移动,屏幕随之移动。
这是在这里找到的代码
http://www.edu4java.com/en/androidgame/androidgame3.html
并且我编辑了它,现在当位图关闭到屏幕的右边界时,位图会重复移动。
MyActivity.java
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new GameView(this));
}
和MyGameView(我问题的主要类别)
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class GameView extends SurfaceView {
private Bitmap bmp;
private SurfaceHolder holder;
private GameLoopThread gameLoopThread;
private int x = 0;
private int xSpeed = 2;
public GameView(Context context) {
super(context);
gameLoopThread = new GameLoopThread(this);
holder = getHolder();
holder.addCallback(new SurfaceHolder.Callback() {
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
boolean retry = true;
gameLoopThread.setRunning(false);
while (retry) {
try {
gameLoopThread.join();
retry = false;
} catch (InterruptedException e) {
}
}
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
gameLoopThread.setRunning(true);
gameLoopThread.start();
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format,
int width, int height) {
}
});
bmp = BitmapFactory.decodeResource(getResources(), R.drawable.attention);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if(x==getWidth())
{
Log.e("cds","cdca");
canvas.translate(x, 0);
x=10;
}
else
{
x = x + xSpeed;
}
if(canvas!=null) {
canvas.drawColor(Color.BLACK);
canvas.drawBitmap(bmp, x, 10, null);
}
}
}
GameLoopThread .java
import android.graphics.Canvas;
public class GameLoopThread extends Thread {
static final long FPS = 10;
private GameView view;
private boolean running = false;
public GameLoopThread(GameView view) {
this.view = view;
}
public void setRunning(boolean run) {
running = run;
}
@Override
public void run() {
long ticksPS = 1000 / FPS;
long startTime;
long sleepTime;
while (running) {
Canvas c = null;
startTime = System.currentTimeMillis();
try {
c = view.getHolder().lockCanvas();
synchronized (view.getHolder()) {
view.onDraw(c);
}
} finally {
if (c != null) {
view.getHolder().unlockCanvasAndPost(c);
}
}
sleepTime = ticksPS-(System.currentTimeMillis() - startTime);
try {
if (sleepTime > 0)
sleep(sleepTime);
else
sleep(10);
} catch (Exception e) {}
}
}
}
答案 0 :(得分:0)
好的,您需要将画布向左翻译,但是您要将画布向右移动。
使用变量moveCanvasX将画布向左翻译。你还需要以相同的速度移动画布和精灵,用速度向右移动精灵" xSpeed",将画布向左移动速度" xSpeed"所以精灵总是朝着正确的方向前进。我希望这能解决你的问题。
int moveCanvasX=0;
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if(x>=getWidth()-bmp.getWidth())
{
Log.e("cds","cdca");
canvas.translate(-moveCanvasX, 0);
moveCanvasX=moveCanvasX+xSpeed;
x=x + xSpeed;
}
else
{
x = x + xSpeed;
}
if(canvas!=null) {
canvas.drawColor(Color.BLACK);
canvas.drawBitmap(bmp, x, 10, null);
}
}