java - 使用视差屏幕

标签 java libgdx

我想使用 libgdx 在我的游戏代码中使用视差屏幕,其中屏幕在 y 方向移动。我的游戏代码为...

   public class ParallaxLayer{
   public TextureRegion region ;
   public Vector2 parallaxRatio;
   public Vector2 startPosition;
   public Vector2 padding ;
   public ParallaxLayer(TextureRegion region,Vector2 parallaxRatio,Vector2 padding){
      this(region, parallaxRatio, new Vector2(0,0),padding);
   }

   public ParallaxLayer(TextureRegion region,Vector2 parallaxRatio,Vector2 startPosition,Vector2 padding){
      this.region  = region;
      this.parallaxRatio = parallaxRatio;
      this.startPosition = startPosition;
      this.padding = padding;
   }
}


   public class ParallaxBackground {
   private ParallaxLayer[] layers;
   private Camera camera;
   private SpriteBatch batch;
   private Vector2 speed = new Vector2();

   public ParallaxBackground(ParallaxLayer[] layers,float width,float height,Vector2 speed){
      this.layers = layers;
      this.speed.set(speed);
      camera = new OrthographicCamera(width, height);
      batch = new SpriteBatch();
   }

   public void render(float delta){
    this.camera.position.add(speed.x * delta, speed.y * delta, 0);
    for(ParallaxLayer layer : layers){
        batch.setProjectionMatrix(camera.projection);
        batch.begin();
        float currentY = - camera.position.y * layer.parallaxRatio.y % ( layer.region.getRegionHeight() + layer.padding.y) ;

        if( speed.y < 0 )
            currentY += -( layer.region.getRegionHeight() + layer.padding.y);
        do{
            float currentX = -camera.position.x * layer.parallaxRatio.x % ( layer.region.getRegionWidth() + layer.padding.x) ;
            if( speed.x < 0)
                currentX += -(layer.region.getRegionWidth() + layer.padding.x);
            do{
                batch.draw(layer.region,
                        -this.camera.viewportWidth/2 + currentX + layer.startPosition.x ,
                        -this.camera.viewportHeight/2 + currentY + layer.startPosition.y);
                currentX += ( layer.region.getRegionWidth() + layer.padding.x);
            }while(currentX < camera.viewportWidth);
            currentY += ( layer.region.getRegionHeight() + layer.padding.y);
        }while( currentY < camera.viewportHeight);
        batch.end();
    }
}

并在渲染方法中将其绘制为

 ParallaxBackground rbg = new ParallaxBackground(new ParallaxLayer[]{
                new ParallaxLayer(bgRegion,new Vector2(),new Vector2(0, 0)),
                new ParallaxLayer(bgRegion2,new Vector2(1.0f,1.0f),new Vector2(0, 500)),
                new ParallaxLayer(bgRegion3,new Vector2(0.1f,0),new Vector2(0,480),new Vector2(0, 0)),
        }, 480, 800, new Vector2(350,0)); 
}

rbg.render(deltaTime);

最佳答案

ParallaxBackground rbg = new ParallaxBackground(new ParallaxLayer[]{
            new ParallaxLayer(bgRegion,new Vector2(),new Vector2(0, 0)),
            new ParallaxLayer(bgRegion2,new Vector2(1.0f,1.0f),new Vector2(0, 500)),
            new ParallaxLayer(bgRegion3,new Vector2(0.1f,0),new Vector2(0,480),new Vector2(0, 0)),
    }, 480, 800, new Vector2(0,350)); 

如果你阅读这个类的文档,你会发现最后一个参数是相对速度比,你在 y 中传递了 0。你需要传递一些其他的值

我正在提供我的游戏的另一个示例

   new ParallaxLayer(Assets.backgroundSprite, new Vector2(0, 0), new Vector2(0, 200000)), 
       new ParallaxLayer(Assets.clouds, new Vector2(0, 0.05f), new Vector2(0, 440), new Vector2(0, 200000)),
        new ParallaxLayer(Assets.cloud1, new Vector2(0.4f, 0), new Vector2(240, 750), new Vector2(1000, 200000)), 
        new ParallaxLayer(Assets.cloud2, new Vector2(0.06f, 0), new Vector2(40, 600), new Vector2(500, 200000)),
        new ParallaxLayer(Assets.cloud1, new Vector2(0.5f, 0), new Vector2(700, 680), new Vector2(1500, 200000)), 

希望对你有帮助

关于java - 使用视差屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18172006/

相关文章:

java - 无法绑定(bind)到 Openshift 上的端口

java - LibGdx 工具错误

java - 视频驱动程序不支持 OpenGL

java - 从 double 到 float 的可能有损转换

java - 从类函数反序列化java对象

Java 找不到任何数组的符号?

java - 什么是取消引用可能的空指针?

java - 如何使所有单元测试失败

java - GWT 接口(interface)问题 : Breaking on exception: TypeError: Cannot read property 'getRestWrapper' of undefined

java - libgdx TiledMap 大小不正确