简体   繁体   English

LibGDX运动不顺畅

[英]LibGDX movement not smooth

I'm making a game in LibGDX. 我在LibGDX中制作游戏。 I have 4 textures as a parallax background and some obstacles (one in the upper part and others in the bottom part of the screen), this is the movement part: 我有4个纹理作为视差背景和一些障碍物(一个在上部,另一个在屏幕的底部),这是运动部分:

//Parallax    
if (bckgndmiddle_x <= -Const.VIEWPORT_W*2+(game_speed*delta)/2) bckgndmiddle_x=-(game_speed*delta)/2; else bckgndmiddle_x-=(game_speed*delta)/2;
    if (bckgndfar_x <= -Const.VIEWPORT_W*2+(game_speed*delta)/5)      bckgndfar_x=-(game_speed*delta)/5; else bckgndfar_x-=(game_speed*delta)/5;

    for (Obstacle obst:obstacles) {
        obst.update(game_speed*delta);
    }

    //End Main game loop

    player.update();
    game.batch.begin();
    game.batch.draw(wall, bckgndfar_x,floor.getRegionHeight()+100);
    game.batch.draw(wall, bckgndfar_x+wall.getRegionWidth(),floor.getRegionHeight()+100);
    game.batch.draw(bot_furniture, bckgndmiddle_x,floor.getRegionHeight());
    game.batch.draw(bot_furniture, bckgndmiddle_x+bot_furniture.getRegionWidth(),floor.getRegionHeight());
    game.batch.draw(floor, bckgndmiddle_x,0);
    game.batch.draw(floor, bckgndmiddle_x+floor.getRegionWidth(),0);
    game.batch.draw(ceiling, bckgndfar_x,Const.VIEWPORT_H-ceiling.getRegionHeight());
    game.batch.draw(ceiling, bckgndfar_x+ceiling.getRegionWidth(),Const.VIEWPORT_H-ceiling.getRegionHeight());

The obstacle update method is just x-= speed; 障碍物更新方法只是x- =速度; as speed is the parameter received 因为速度是收到的参数

The problem is that from time to time the textures wiggles strange, like if the device cannot handle the game and freezes for a split second. 问题在于,纹理有时会发出奇怪的声音,就像设备无法处理游戏并暂时冻结一样。

Any clue why is this happening? 任何线索为什么会发生这种情况?

EDIT 编辑

What happens is that the textures stutters from time to time (I'm Spanish and I didn't know that word) 发生的事情是纹理不时地断断续续(我是西班牙语,我不知道那个词)

I think that it must be something related to the second image of each part of the background, that adding of the width. 我认为它必须是与背景的每个部分的第二个图像相关的东西,即增加宽度。 Like it adds the width, but sometimes it is too much because of a drop of fps (like 1 or 2 fps) and the next time it "moves" back to the good position because fps are back to normal. 就像它增加了宽度一样,但有时它会因为fps的下降(如1或2 fps)而过多,并且下次因为fps恢复正常而“移动”回到好位置。

EDIT 2 I tried it without the obstacles and it still stutters, I tried it again with the obstacles and without the background and it doesnt, so it must be something with what I said in the first edit. 编辑2我尝试了它没有障碍,它仍然口吃,我再次尝试与障碍,没有背景,它没有,所以它必须与我在第一次编辑中说的话。

BTW, the FPS drop is less than 1 (checked) 顺便说一句,FPS下降小于1(已检查)

I have just tried to draw a entire background image (no parallax) and still the same issue. 我刚刚试图绘制整个背景图像(没有视差),仍然是同样的问题。

I see one possible reason that has nothing to do with FPS drops: 我看到一个与FPS掉落无关的可能原因:

Here's your first line, separated out for legibility: 这是你的第一行,为了易读性而分开:

if (bckgndmiddle_x <= -Const.VIEWPORT_W*2+(game_speed*delta)/2) 
    bckgndmiddle_x = -(game_speed*delta)/2; 
else 
    bckgndmiddle_x -= (game_speed*delta)/2;

Usually, the else statement is used to move the background at a constant speed. 通常, else语句用于以恒定速度移动背景。 But whenever it gets to the if statement, the position is explicitly set without regard to its previous position, so the movement will not be smooth. 但是无论何时进入if语句,都会明确设置位置而不考虑其先前的位置,因此移动将不会平滑。 You need to move it to a position where it looks like it has moved exactly -(game_speed*delta)/2 from its previous position. 你需要将它移动到一个看起来准确移动的位置-(game_speed*delta)/2从它之前的位置开始。

One way to do this is to always move the object with speed, and shift it forward by the texture's width only when necessary as a correction. 一种方法是始终以速度移动对象,并仅在必要时将其向前移动纹理的宽度作为校正。 For example, assuming you are keeping the bottom left of the screen at (0, 0):: 例如,假设您将屏幕的左下角保持为(0,0)::

float midWidth = floor.getRegionWidth();

bckgndmiddle_x -= game_speed*delta/2; //Always move it at constant speed.
if (bckgndmiddle_x + 2*midWidth < Const.VIEWPORT_W) //Right edge starting to show. 
    bckgndmiddle_x += midWidth; // Shift it exactly by its width so the jump is undetectable

The 2* above is because I'm assuming from your other code that you are actually drawing two copies of the background texture side by side. 上面的2*是因为我从你的其他代码中假设你实际上并排绘制了两个背景纹理副本。 If you were drawing a single, wider texture that's wider than the viewport width constant, then you'd remove the 2* . 如果您绘制的单个更宽的纹理比视口宽度常量更宽,那么您将删除2*

This same issue applies of course to your second line of code as well, where you are setting the far background displacement. 同样的问题当然适用于您的第二行代码,您可以在其中设置远背景位移。

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

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