Android studio 游戏速度不同设备
Android studio Game speed different devices
我正在 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 之间是对的吗?
但每次我以不同的方式尝试时,游戏速度总是不正确。
是否可以让我的游戏 运行 在不同的设备、不同的屏幕尺寸上以相同的速度运行?
问题是 screenHeight
是以像素为单位的屏幕高度,但游戏没有使用整个屏幕。这导致不同设备上的速度不同。所以screenHeight应该改成gamesLayout.getHeight()
.
我正在 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 之间是对的吗?
但每次我以不同的方式尝试时,游戏速度总是不正确。 是否可以让我的游戏 运行 在不同的设备、不同的屏幕尺寸上以相同的速度运行?
问题是 screenHeight
是以像素为单位的屏幕高度,但游戏没有使用整个屏幕。这导致不同设备上的速度不同。所以screenHeight应该改成gamesLayout.getHeight()
.