我正在制作游戏,我希望玩家能够以“X'位于屏幕中间的一半位置,以及一个' Y'播放器底部位于屏幕底部上方5个像素的位置。我尝试使用显示和显示指标来获取屏幕大小并设置相对于此的x和y位置,但它看起来不起作用。图像根本不在屏幕上绘制。我只是得到一个空白的屏幕。如果我给出恒定坐标(ex.100,50),它可以工作。这就是我现在想要实现的目标:
x = (displayMetrics.widthPixels / 2);
y = displayMetrics.heightPixels - (height + 5);
这是自定义视图将其绘制到屏幕的方式:
canvas.drawBitmap(player.getImage(), player.getX(), player.getY(), null);
自定义视图类 -
public class GameGraphics extends SurfaceView implements Runnable {
private SurfaceHolder surfaceHolder;
private Thread gameGraphics;
private GameCore gameCore;
private Player player;
public GameGraphics(Context context, GameCore gameCore) {
super(context);
this.gameCore = gameCore;
this.player = gameCore.getPlayer();
surfaceHolder = getHolder();
gameGraphics = new Thread(this);
gameGraphics.start();
}
@Override
public void run() {
while(gameCore.isRunning() == true){
if(!surfaceHolder.getSurface().isValid())
continue;
Canvas canvas = surfaceHolder.lockCanvas();
canvas.drawBitmap(player.getImage(), player.getX(), player.getY(), null);
surfaceHolder.unlockCanvasAndPost(canvas);
}
}
}
玩家类 -
public Player(GameCore gameCore, Context context) {
this.gameCore = gameCore;
this.context = context;
displayMetrics = new DisplayMetrics();
gameCore.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
/* INITIALIZE HEIGHT AND WIDTH VARIABLES */
height = 72;
width = 72;
x = (displayMetrics.widthPixels / 2);
y = displayMetrics.heightPixels - (height + 5);
xChange = 0;
/* INITIALIZE IMAGE */
image = BitmapFactory.decodeResource(context.getResources(),R.drawable.ic_launcher);
}
***我使用默认的ic_launcher来测试****