我正在Android Studio里做一个游戏。现在我的游戏完成了,但是游戏速度在大屏幕上是不同的…
我用这个计时器运行我的游戏:
if(timer == null){
timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
if (start_flg) {
handler.post(new Runnable() {
@Override
public void run() {
changePos();
}
});
}
}
}, 0, 20);
}changePos()看起来像这样:
public void changePos() {
SPEED_BOX =(screenHeight/280);
long time = System.nanoTime();
double delta_time = (double) ((time - last_time) / 1000000)/10;
last_time = time;
// Move Box
if (action_flg) {
// Touching
boxY += SPEED_BOX*delta_time;
box.setImageDrawable(imageBox1);
} else {
// Releasing
boxY -= SPEED_BOX*delta_time;
box.setImageDrawable(imageBox2);
}
// Check box position.
if (boxY < 0) {
boxY = 0;
box.setImageDrawable(imageBox1);
}
if (frameWidth - boxSize < boxY) {
boxY = frameWidth - boxSize;
box.setImageDrawable(imageBox2);
}
box.setY(boxY);
}我纠正了我的deltaTime总是在1.5到2.9之间吗?
但每次我尝试不同的方式时,游戏速度总是不正确。有没有可能让我的游戏在不同的设备,不同的屏幕尺寸上运行相同的速度?
发布于 2020-12-14 00:18:40
问题是,screenHeight是以像素为单位的屏幕高度,但游戏并不使用整个屏幕。这导致了不同设备上的速度不同。因此,应该将screenHeight更改为gamesLayout.getHeight()。
https://stackoverflow.com/questions/65267843
复制相似问题