【问题标题】:How can I tile an image in X direction while it is scrolling at a velocity?当图像以一定速度滚动时,如何在 X 方向平铺图像?
【发布时间】:2014-02-16 20:00:36
【问题描述】:

嘿,我这几天一直在上网,我也一直在阅读这方面的内容,我迫切需要帮助。 我正在编写一个 2D 游戏,我需要背景移动,我的角色将是静止的,他会跳过东西。

如果有人能把我推向正确的方向,或者甚至提供一个 sn-p 的代码,我会非常感激。

【问题讨论】:

    标签: java android xml horizontalscrollview tile


    【解决方案1】:

    我认为最简单的方法是在角色滚动时绘制背景图像两次,但我不确定如何使用 Horizo​​ntalScrollView。

    //x , y, width, height
    Background b1 = new Background(0, screenHeight/2, screenWidth, screenHeight);
    Background b2 = new Background(screenWidth, screenHeight/2, screenWidth, screenHeight);
    
    //update loop
    b1.x-=speed;
    b2.x-=speed;
    
    if(b1.x+b1.width/2<0){
       b1.x=b2.x+b2.width/2+b1.width/2;
    }
    if(b2.x+b2.width/2<0){
       b2.x=b1.x+b1.width/2+b2.width/2;
    }
    

    您可以对以不同速度更新的多种类型的背景图像使用这种方法。

    【讨论】:

      【解决方案2】:

      对于仍在为此苦苦挣扎的人,我已经解决了:

      下载 corona SDK 并使用它进行编程非常容易:

      function scrollBackground(self,event)
      if self.x < [Enter Background width in pixels] then
      self.x = [Enter Background width in pixels]
      else
                  self.x = self.x - [Speed - low number like 5 is slow scrolling, high number like 18 is fast scrolling]
              end
          end 
      

      使用上面的函数作为滚动背景,然后在下面添加:

      function scene:enterScene(event)
      background.enterFrame = scrollCity
      Runtime:addEventListener("enterFrame", background)
      

      【讨论】:

        猜你喜欢
        • 2016-12-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-02-16
        • 1970-01-01
        • 1970-01-01
        • 2015-05-04
        • 1970-01-01
        相关资源
        最近更新 更多