基于libgdx精灵表的动画缩放

时间:2015-10-03 22:54:50

标签: java android animation libgdx screen

我对libgdx和android编程很新。我在渲染基于精灵表的动画时遇到问题,并且在不同的屏幕尺寸上使它变得相同。

如果我在我的笔记4上运行以下代码,动画非常小,在Zenfone 2上相当大,而且它非常大,最后在我的笔记本电脑上,它是如此之小,几乎看不到。< / p>

我真的不明白为什么会这样,以及如何在两部手机上做到这一点。我认为使用带有游戏单元和视口的正交相机可以完成这项工作,但我可能做错了,因为它没有。

我正在关注这本书&#34; libgdx跨平台游戏开发食谱&#34;。

我非常感谢任何有关如何在游戏单元中正确使用以使游戏在不同屏幕尺寸上相同的帮助,因此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);
    }
}

}

1 个答案:

答案 0 :(得分:0)

这不是使用视口的正确方法:

 viewport = new FitViewport(Gdx.graphics.getWidth(),Gdx.graphics.getHeight(), camera);

您应该使用常量值800x600初始化视口,并在编写代码时使用它们:

 viewport = new FitViewport(WORLD_WIDTH,WORLD_HEIGHT, camera);

渲染时图像将自动拉伸/调整大小,您不需要这样做:

batch.draw(techman.Frame,x,y,width,height);

PS:不要在render()方法中调用batch.setProjectionMatrix(camera.combined);。你只需要这样做一次。

文档: