背景无限使用电晕sdk

时间:2014-11-09 12:41:11

标签: sdk lua corona

我正试图在电晕sdk(无限背景)中滚动背景 我重复使用了两张图像(854x176)。

我试过这个功能:

 function mov(self, event)
   if self.x < -854 then
     self.x = 854
   else
     self.x = self.x - self.speed
   end
 end

它的工作正常,但问题是重复之间出现了一个小的空白区域。 有更好的方法吗?

1 个答案:

答案 0 :(得分:5)

这样做的一种方法是利用Graphics 2.0 Engine的称为重复填充的功能。

以下是步骤:

  1. 设置x的默认纹理包装模式(对y也可以这样做):

    display.setDefault("textureWrapX", "mirroredRepeat")
    

    包装模式是:

    • clampToEdge ” - (默认)限制填充不会重复
    • 重复” - 重复填充,就好像相同的瓷砖并排放置一样
    • mirroredRepeat ” - 以镜像模式重复填充,每个图块都是旁边一个图像的镜像
  2. 创建一个您想要的背景大小的矩形,例如。全屏

    local background = display.newRect(display.contentCenterX, display.contentCenterY, 320, 480)
    
  3. 使用背景图片填充显示对象

    background.fill = {type = "image", filename = "background.jpg"}
    
  4. 使用适合您应用的任何方法为其设置动画。以下只是一种方式:

    local function animateBackground()
        transition.to(background.fill, {time=5000, x=1, delta=true, onComplete=animateBackground})
    end
    
    animateBackground()
    

    在这里,您只需在background.fill对象的x属性上运行转换,delta = true表示我们正在使用x值的更改,而不是最终结束值(see here)

    使用时间值x,将delta设置为false,使用包装模式进行播放,只是为了查看它对动画的影响。你甚至可能偶然发现了一些你可能想在以后使用的很酷的效果......

    查看布伦特索伦蒂诺的this excellent tutorial,他会详细了解填充情况。另外,请参阅CoronaSDK中Graphics-Premium / PatternFill下的示例代码。

    完整代码:

    display.setDefault("textureWrapX", "mirroredRepeat")
    
    local background = display.newRect(display.contentCenterX, display.contentCenterY, 320, 480)
    background.fill = {type = "image", filename = "background.jpg" }
    
    local function animateBackground()
        transition.to( background.fill, { time=5000, x=1, delta=true, onComplete=animateBackground } )
    end
    
    animateBackground()
    
相关问题