Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 基于libgdx sprite表的动画缩放_Java_Android_Animation_Libgdx_Screen - Fatal编程技术网

Java 基于libgdx sprite表的动画缩放

Java 基于libgdx sprite表的动画缩放,java,android,animation,libgdx,screen,Java,Android,Animation,Libgdx,Screen,一般来说,我对libgdx和android编程相当陌生。我在渲染基于精灵图纸的动画时遇到问题,并且在不同的屏幕大小上使其大小相同 如果我在note 4上运行以下代码,动画非常小,在Zenfone 2上它相当大,最后在我的笔记本电脑上它非常小,几乎看不见 我真的不明白为什么会发生这种情况,以及如何在两部手机上做到相同。我原以为使用带有ingame单元和视口的正交摄影机就可以了,但我可能做错了什么,因为事实并非如此 我正在学习libgdx跨平台游戏开发食谱 我非常感谢任何关于如何在游戏单元中正确使用

一般来说,我对libgdx和android编程相当陌生。我在渲染基于精灵图纸的动画时遇到问题,并且在不同的屏幕大小上使其大小相同

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

我真的不明白为什么会发生这种情况,以及如何在两部手机上做到相同。我原以为使用带有ingame单元和视口的正交摄影机就可以了,但我可能做错了什么,因为事实并非如此

我正在学习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);
    }
}

}

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

 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:不要调用batch.setProjectionMatrix.combined;在渲染方法内部。你只需要做一次

文件:


非常感谢。在视口中传递场景宽度和场景高度是否有效?在batch.draw中,第一个参数应该是techman.Frame,然后是宽度高度xy?@EdoardoPona是的,它可以工作。你应该选择在你的世界里容易计算的世界坐标,我建议的分辨率与你在概念艺术中的分辨率相同。是的,我修正了画法中的错误,谢谢!成功了!不过,我必须在Gdx.gl.clclear方法中添加最后一件事:|GL20.gl_DEPTH_BUFFER_BIT,谢谢!