libgdx sprite-sheet 基于动画缩放
libgdx sprite-sheet based animation scaling
总的来说,我对 libgdx 和 android 编程还很陌生。我在渲染基于 sprite-sheet 的动画以及在不同屏幕尺寸上设置相同尺寸时遇到问题。
如果我 运行 在我的 note 4 上使用以下代码,动画非常小,在 Zenfone 2 上反而相当大,最后在我的笔记本电脑上它非常小以至于几乎看不到.
我真的不明白为什么会出现这种情况,以及如何在两部手机上使它相同。我认为使用带有游戏内单位和视口的正交相机就可以完成这项工作,但我可能做错了什么,因为它没有。
我正在关注这本书"libgdx cross-platform game development cookbook"。
我将非常感谢任何有关如何在游戏单元中正确使用以使游戏在不同屏幕尺寸上相同的帮助,这样 512x512 像素的图像在 note4 上不会很小,在 Zenfone 上不会很大(我的动画每一帧都是512px的平方)。
就个人电脑而言,我只是不知道发生了什么,我真的很感激任何关于为什么会发生的解释!
谢谢大家!
package com.mygdxGame;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.Animation.PlayMode;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasRegion;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.viewport.FitViewport;
import com.badlogic.gdx.utils.viewport.Viewport;
import java.util.Comparator;
public class MyGdxGame extends ApplicationAdapter {
private static final float WORLD_TO_SCREEN = 1.0f / 100.0f;
private static final float SCENE_WIDTH = 12.80f;
private static final float SCENE_HEIGHT = 7.20f;
private static final float FRAME_DURATION = 1.0f / 20.0f;
private TextureAtlas techmanAtlas;
private Animation techmanRun;
private float animationTime;
private OrthographicCamera camera;
public Viewport viewport;
public SpriteBatch batch;
@Override
public void create(){
camera = new OrthographicCamera();
viewport = new FitViewport(Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), camera);
batch = new SpriteBatch();
animationTime = 0.0f;
techmanAtlas = new TextureAtlas(Gdx.files.internal("TechMan.atlas"));
Array<TextureAtlas.AtlasRegion> techmanRegions = new Array<TextureAtlas.AtlasRegion>(techmanAtlas.getRegions());
techmanRegions.sort(new RegionComparator());
techmanRun = new Animation(FRAME_DURATION, techmanRegions, PlayMode.LOOP);
camera.position.set(SCENE_WIDTH * 0.5f, SCENE_HEIGHT * 0.5f, 0.0f);
}
@Override
public void dispose(){
batch.dispose();
techmanAtlas.dispose();
}
@Override
public void render(){
Gdx.gl.glClearColor(1, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
animationTime += Gdx.graphics.getDeltaTime();
camera.update();
batch.setProjectionMatrix(camera.combined);
batch.begin();
TextureRegion techmanFrame = techmanRun.getKeyFrame(animationTime);
int width = techmanFrame.getRegionWidth();
int height = techmanFrame.getRegionWidth();
float originX = width * 0.5f;
float originY = height * 0.5f;
batch.draw(techmanFrame,
1.0f - originX, 3.70f - originY,
originX, originY,
width, height, //width, height
WORLD_TO_SCREEN, WORLD_TO_SCREEN,
0.0f);
batch.draw(techmanRun.getKeyFrame(animationTime), 100.0f, 275.0f);
batch.end();
}
@Override
public void resize(int width, int height){
viewport.update(width, height, false);
}
private static class RegionComparator implements Comparator<AtlasRegion> {
@Override
public int compare(AtlasRegion region1, AtlasRegion region2){
return region1.name.compareTo(region2.name);
}
}
}
这不是使用视口的正确方法:
viewport = new FitViewport(Gdx.graphics.getWidth(),Gdx.graphics.getHeight(), camera);
您应该使用常量值初始化视口,例如 800x600,并在编写代码时使用它们:
viewport = new FitViewport(WORLD_WIDTH,WORLD_HEIGHT, camera);
图像将在渲染时自动streched/resized,您不需要这样做:
batch.draw(techman.Frame,x,y,width,height);
PS:不要在render()方法内部调用batch.setProjectionMatrix(camera.combined);
。你只需要这样做一次。
文档:
总的来说,我对 libgdx 和 android 编程还很陌生。我在渲染基于 sprite-sheet 的动画以及在不同屏幕尺寸上设置相同尺寸时遇到问题。
如果我 运行 在我的 note 4 上使用以下代码,动画非常小,在 Zenfone 2 上反而相当大,最后在我的笔记本电脑上它非常小以至于几乎看不到.
我真的不明白为什么会出现这种情况,以及如何在两部手机上使它相同。我认为使用带有游戏内单位和视口的正交相机就可以完成这项工作,但我可能做错了什么,因为它没有。
我正在关注这本书"libgdx cross-platform game development cookbook"。
我将非常感谢任何有关如何在游戏单元中正确使用以使游戏在不同屏幕尺寸上相同的帮助,这样 512x512 像素的图像在 note4 上不会很小,在 Zenfone 上不会很大(我的动画每一帧都是512px的平方)。
就个人电脑而言,我只是不知道发生了什么,我真的很感激任何关于为什么会发生的解释!
谢谢大家!
package com.mygdxGame;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.Animation.PlayMode;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasRegion;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.viewport.FitViewport;
import com.badlogic.gdx.utils.viewport.Viewport;
import java.util.Comparator;
public class MyGdxGame extends ApplicationAdapter {
private static final float WORLD_TO_SCREEN = 1.0f / 100.0f;
private static final float SCENE_WIDTH = 12.80f;
private static final float SCENE_HEIGHT = 7.20f;
private static final float FRAME_DURATION = 1.0f / 20.0f;
private TextureAtlas techmanAtlas;
private Animation techmanRun;
private float animationTime;
private OrthographicCamera camera;
public Viewport viewport;
public SpriteBatch batch;
@Override
public void create(){
camera = new OrthographicCamera();
viewport = new FitViewport(Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), camera);
batch = new SpriteBatch();
animationTime = 0.0f;
techmanAtlas = new TextureAtlas(Gdx.files.internal("TechMan.atlas"));
Array<TextureAtlas.AtlasRegion> techmanRegions = new Array<TextureAtlas.AtlasRegion>(techmanAtlas.getRegions());
techmanRegions.sort(new RegionComparator());
techmanRun = new Animation(FRAME_DURATION, techmanRegions, PlayMode.LOOP);
camera.position.set(SCENE_WIDTH * 0.5f, SCENE_HEIGHT * 0.5f, 0.0f);
}
@Override
public void dispose(){
batch.dispose();
techmanAtlas.dispose();
}
@Override
public void render(){
Gdx.gl.glClearColor(1, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
animationTime += Gdx.graphics.getDeltaTime();
camera.update();
batch.setProjectionMatrix(camera.combined);
batch.begin();
TextureRegion techmanFrame = techmanRun.getKeyFrame(animationTime);
int width = techmanFrame.getRegionWidth();
int height = techmanFrame.getRegionWidth();
float originX = width * 0.5f;
float originY = height * 0.5f;
batch.draw(techmanFrame,
1.0f - originX, 3.70f - originY,
originX, originY,
width, height, //width, height
WORLD_TO_SCREEN, WORLD_TO_SCREEN,
0.0f);
batch.draw(techmanRun.getKeyFrame(animationTime), 100.0f, 275.0f);
batch.end();
}
@Override
public void resize(int width, int height){
viewport.update(width, height, false);
}
private static class RegionComparator implements Comparator<AtlasRegion> {
@Override
public int compare(AtlasRegion region1, AtlasRegion region2){
return region1.name.compareTo(region2.name);
}
}
}
这不是使用视口的正确方法:
viewport = new FitViewport(Gdx.graphics.getWidth(),Gdx.graphics.getHeight(), camera);
您应该使用常量值初始化视口,例如 800x600,并在编写代码时使用它们:
viewport = new FitViewport(WORLD_WIDTH,WORLD_HEIGHT, camera);
图像将在渲染时自动streched/resized,您不需要这样做:
batch.draw(techman.Frame,x,y,width,height);
PS:不要在render()方法内部调用batch.setProjectionMatrix(camera.combined);
。你只需要这样做一次。
文档: