【问题标题】:libgdx sprite-sheet based animation scaling基于 libgdx sprite-sheet 的动画缩放
【发布时间】:2015-10-03 22:54:50
【问题描述】:

总的来说,我对 libgdx 和 android 编程很陌生。我在渲染基于 sprite-sheet 的动画时遇到问题,并且在不同的屏幕尺寸上使其尺寸相同。

如果我在 note 4 上运行以下代码,动画非常小,但在 Zenfone 2 上却非常大,最后在我的笔记本电脑上,它非常小,几乎看不到。

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

我正在关注《libgdx 跨平台游戏开发食谱》这本书。

对于如何在游戏单元中正确使用以使游戏在不同屏幕尺寸上保持相同的任何帮助,我将不胜感激,这样 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);
    }
}

}

【问题讨论】:

    标签: java android animation libgdx screen


    【解决方案1】:

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

     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);。您只需要这样做一次。

    文档:

    【讨论】:

    • 谢谢!那么在视口中传递“SCENE_WIDTH”和“SCENE_HEIGHT”会起作用吗?而在batch.draw中,第一个参数应该是“techman.Frame”然后只是宽度高度x y?
    • @EdoardoPona 是的,它会工作。您应该选择使您的世界中的事物易于计算的世界坐标(我建议您使用与制作概念艺术相同的分辨率)。是的,我修复了 draw 方法中的错误,谢谢
    • 是的!有效!不过,我必须在 Gdx.gl.clclear 方法中添加最后一件事:“| GL20.GL_DEPTH_BUFFER_BIT”,谢谢!
    猜你喜欢
    • 1970-01-01
    • 2014-10-03
    • 2014-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多