使用视差屏幕 [英] Using Parallax screen

查看:92
本文介绍了使用视差屏幕的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to use parallax screen in my game code using libgdx in which screen is moving in y direction. My game code is given as...

   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();
    }
}

并在render方法中将其绘制为

and draw it in render method as

 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。你需要传递一些其他价值

if u read the documentation of this class u find that the last parameter is the relative velocity ratio and u passed 0 in y. u need to pass some other value than it

我正在给我另一个游戏样本

i am giving another sample of my game

   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)), 

希望它可以帮助你

这篇关于使用视差屏幕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆