Background infinite using corona sdk

十年热恋 提交于 2019-12-19 11:34:53

问题


I'm trying to scroll side a background in corona sdk (infinity background) I used two images repeated (854x176).

I tried this function:

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

it's working just fine but the problem that a small white space occurred between repetitions. Is there a better way to do this?


回答1:


One way of doing this would be to take advantage of Graphics 2.0 Engine's feature called repeating fills.

Here are the steps:

  1. Set the default texture wrapping mode for x (you could do the same for y too):

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

    The modes for wrapping are:

    • "clampToEdge" - (default) clamped fill won't repeat
    • "repeat" - fill is repeated as if same tiles were placed side by side
    • "mirroredRepeat" - fill is repeated in a mirrored pattern, each tile being a mirror image of the one next to it
  2. Create a rectangle the size of the background you want to have, eg. full screen

    local background = display.newRect(display.contentCenterX, display.contentCenterY, 320, 480)
    
  3. Fill your display object with your background image

    background.fill = {type = "image", filename = "background.jpg"}
    
  4. Animate it using whatever method fits your app. Below's just one way:

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

    Here you simply run transition on x property of background.fill object, delta=true indicating that we're rather using changes in x value, not final ending values (see here).

    Play with the values for time, x, set delta to false, play with wrapping modes, just to see what effect it has on animation. You might even accidentally discover some cool effect which you might want to use later...

    Check this excellent tutorial by Brent Sorrentino, who goes through more details on fills. Additionally, see sample code in CoronaSDK under Graphics-Premium/PatternFill.

    Full code:

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


来源:https://stackoverflow.com/questions/26828279/background-infinite-using-corona-sdk

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!