简体   繁体   English

在不同屏幕尺寸下的精灵速度

[英]Sprite Speed on different Screen Sizes

I am trying to understand the concept of speed in a game which supports different screen sizes (in android). 我试图了解支持不同屏幕尺寸(在Android中)的游戏中速度的概念。

Let's say I have a Sprite that should take 10 seconds from the right end in landscape mode to the left end. 假设我有一个Sprite,从横向模式的右端到左端需要10秒钟。 The Frame Rate is not steady so sometimes it's 30 for a while and then all of a sudden it changes to 10. But I still want my Sprite to take 10 seconds from one end to the other. 帧速率不稳定,因此有时会持续30秒钟,然后突然变为10。但是我仍然希望Sprite从一端到另一端花费10秒。 How would I do that? 我该怎么做?

final int time = 10; //time from one end to the otehr
float baseSpeedX = screenWidth / time;
float currentFrameRate = 30.0; //just as an example
float baseFrameRate    = 50.0; //the frame rate we want
float factor = baseFrameRate / currentFrameRate;
playerSpeedX += baseSpeedX * factor;

This is what I understood: 这是我的理解:
You get the base speed by dividing the screen width by the time you want to get the sprite from one end to the other. 通过将屏幕宽度除以您想要从一端到另一端获得精灵的时间,可以得到基本速度 The base speed is the speed your Sprite should add to your current location every second if a steady framerate is available. 基本速度是指如果有稳定的帧速率,则Sprite应该每秒增加到当前位置的速度。 However, I don't know how to achieve that so I just get the current frame rate and divide the base frame rate , the frame rate we really want for our game, to get the factor we multiply our base speed with. 但是,我不知道如何实现这一目标,所以我只是获得当前帧速率,然后将基本帧速率 (我们游戏中真正想要的帧速率)相除,以获得与基本速度相乘的因子。

This would, if it really works, make the Sprite reach the other end in 10 seconds, right? 如果确实可行,这将使Sprite在10秒内到达另一端,对吗? Even if we have a frame rate of 10 for 2 seconds and then 50 after that for 8 seconds. 即使我们的帧速率为10表示2秒,然后设置为50表示8秒。 The higher the frame rate now, the lower the Sprite increases in distance and the lower the frame rate, the higher the Sprite increases in distance. 现在,帧速率越高,Sprite距离增加的越少,而帧速率越低Sprite距离增加的越高。

If this all works, would it be something I could use in a game? 如果这一切都可行,那我可以在游戏中使用它吗? Or is it totally wrong approach for getting the sprite speed? 还是获取精灵速度完全错误的方法?

You are on the right track 您走在正确的轨道上

Generally when you are moving sprites, you need to make a way so that the sprite moves slower when there is a higher fps, and move quicker when there is a lower fps. 通常,在移动Sprite时,需要采取一种方法,以使fps较高时Sprite的移动速度较慢,而fps较低时Sprite的移动速度更快。 This can be done by multiplying another variable called delta to your baseSpeed. 这可以通过将另一个称为delta variable乘以baseSpeed来完成。 Delta in this case is the time it takes from the current frame and the previous frame. 在这种情况下,增量是从当前帧到上一帧所花费的时间。 With this, games with high fps will have a low delta because it takes a short amount of time to go from one frame to another. 这样,高fps的游戏将具有较低的增量,因为从一帧到另一帧需要花费很短的时间。 Following this pattern, a low fps game will see a high amount of delta. 按照这种模式,低fps游戏将看到大量的增量。 By calculating the delta, you can have uniformity that your sprite will be the same speed at different fps targets. 通过计算增量,您可以使子画面在不同的fps目标下具有相同的速度。 You can usually calculate delta by subtracting the current time and the last rendered time. 通常,您可以通过减去当前时间和上次渲染的时间来计算增量。

In theory, if you use your baseSpeedX formula (from first glance, it seems sound) and multiply it with the delta, you will have your speed: 从理论上讲,如果您使用baseSpeedX公式(乍一看似乎baseSpeedX )并将其与增量相乘,您将获得以下速度:

double delta = currentTime - timeOfLastFrame; // I don't know specifically what time libraries there are on your platform
double speed = baseSpeedX * delta;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM