使用视差屏幕

2023-12-15

我想在我的游戏代码中使用 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();
    }
}

并在 render 方法中将其绘制为

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

希望它能帮助你

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用视差屏幕 的相关文章

随机推荐

  • 如何将mysql表的行转置为列

    这是我当前的 mysql 表的样子 PunchID EmpID PunchEvent PunchDateTime 1 0456 clockin 5 14 2013 8 36 26 AM 48 0456 breakout 5 14 2013
  • 分散 Flask 模型时,引发 RuntimeError: 'application not Registered on db'

    我正在通过分散模型 蓝图来重构我的 Flask 应用程序 但遇到运行时错误 def create app app flask Flask app app config SQLALCHEMY DATABASE URI sqlite app r
  • 优化 Internet Explorer 11 的滚动速度

    我目前有一个类似议程的应用程序 其中第一列绝对水平 第一行绝对垂直 我通过捕获滚动效果并更改其附加的 CSS 类的 left 或 top 属性来实现此目的 这些课程最多可达 700 个项目 每天 2 年 window scroll func
  • Qt 工作 Windows 8 风格无框自定义窗口

    我最近在我的 Windows 7 机器上安装了适用于 Windows 的 Github 并且喜欢它的自定义框架 它非常适合整个应用程序主题 并且有自己的标题栏按钮 布局非常好 非常流畅 并且工作起来看起来非常自然和 我做了一些挖掘 发现了
  • app_stl 值与静态和共享构建 android 之间的关系

    一直想知道 现在很困惑 当在 Application mk 文件中设置 app stl stlport static 或 stlport shared 时 并且您尝试构建库的共享版本和静态版本 有什么效果 为什么你的 android ndk
  • C/C++ 的 LAPACK 包装器

    我想用Visual Studio 2008 用 C 编程 但也想使用 LAPACK 功能 是否有任何包装器 以便我可以使用LAPACK in Visual Studio 2008 犰狳对我来说效果很好 良好的API 卓越的性能
  • UIGraphicsGetCurrentContext() 生命周期短

    我有一个实现手绘图的视图 但我有一个小问题 我注意到在 iPad 3 上一切都变得一团糟 所以我尝试更新我的绘图代码 可能就像我一开始就应该做的那样 以仅更新被描边的部分 然而 打开后的第一个行程以及闲置约10秒后的第一个行程都非常慢 一切
  • 如何将 .ckpt 文件转换为 .pb

    我在对象检测 API 中使用 ssd mobilenets 来训练我自己的模型 并获取 ckpt 文件 它在我的计算机上运行良好 但现在我想在手机上使用该模型 所以 我需要将其转换为 pb 文件 我不知道该怎么做 有人可以帮忙吗 顺便说一下
  • 尽管 cordova 是最新的,但使用 ionic 创建新应用程序时 Cordova 版本错误

    使用创建新应用程序时出现以下错误ionic 我正在按照标准文档 http ionicframework com getting started gsakhardande gsakhardande PC 桌面 ionic start myAp
  • 在matlab中读取灰度图像[重复]

    这个问题在这里已经有答案了 我有灰度图像 lena bmp 我想使用 imread 函数在 matlab 中读取此图像 当我使用下面的代码来读取和显示图像时 我的图像是暗的 黑色 img imread lena bmp imshow img
  • 在C语言中,如何将整数插入字符串?

    我的代码得到一串字符 例如 aaabbdddd 函数将字母及其出现的次数插入到新字符串中 所以这个特定字符串的输出应该是 a3b2d4 我的问题是如何将数字插入字符串中 我尝试使用 itoa 并将整个字符串转换为单个数字 这是我的代码 de
  • 为什么我不能比较 Exception 对象是否相等?

    SSCCE import java util Objects public class FooMain private static Exception foo try throw new Exception catch Exception
  • strcat 与 char *a[10] 的问题

    include include
  • $Pos from Bottom inside <%循环DataObjects %>

    可以在数据对象上的模板循环内以某种方式判断您是否位于 Pos 24 但从底部开始计数 类似于 do stuff or like do stuff or like do stuff 在 Silverstripe 3 中 能够执行以下操作 He
  • 如何使用 Python Pandas 将 JMP *.jmp 文件读取到 Pandas 数据帧中

    我正在努力读书SAS JMP 文件与熊猫read csv函数进入 Pandas 数据框 有人有处理这种类型的数据文件的经验吗 最有效的方法是什么 这对我有用 其结果有时有点出乎意料 例如 有时我得到没有标题的 CSV 即使在 JMP 中它们
  • 'my/path/to/venv/lib64'' aria-label='函数未实现:'lib' -> 'my/path/to/venv/lib64''> 函数未实现:'lib' -> 'my/path/to/venv/lib64'

    操作系统 Manjaro蟒蛇 3 8 我的计算机上有 2 个分区 一个是安装 Manjaro 的位置 另一个是辅助 SSD 每次当我运行这个命令时virtualenv env在我的辅助 SSD 中 我收到以下错误 OSError Errno
  • 来自串行端口的 Readline 锁定

    我正在尝试从秤 RS232 接口读取数据 它通过串行端口发送连续的 ASCII 字符串流 但我无法获取该字符串流 我只想获取它发出的一行数据 我想我假设我会使用 Readline 来获取数据 但当我运行它时它只会锁定 PC 我认为它正在尝试
  • Pandas 数据框垂直合并

    我有一个关于合并两个数据框的疑问 例如我有 2 个数据框 如下所示 print df1 Year Location 0 2013 america 1 2008 usa 2 2011 asia print df2 Year Location
  • 如何在opencv中的相机流上叠加小动画

    我正在开发一个使用opencv作为我的大学项目的应用程序 它几乎完成了 除了我无法在我的相机流上覆盖动画视频 Flash视频 我想捕获用户的嘴巴 在检测到嘴巴后我想覆盖一个烟雾的动画视频 请问谁能帮我处理重叠部分吗 如果不可能 您能否阐明任
  • 使用视差屏幕

    我想在我的游戏代码中使用 libgdx 来使用视差屏幕 其中屏幕沿 y 方向移动 我的游戏代码给出为 public class ParallaxLayer public TextureRegion region public Vector2