Saturday, 26 October 2013

How to repeat 2 backgrounds continuously forever in corona SDK game

In this tutorial, we will learn about how to repeat 2 backgrounds forever in our corona SDK game application. The code is as follows:

  
  -- Creating 2 images to run as background --

  local bg1 = display.newImageRect( "bg_1.png" ,480 ,320)
  bg1.x = display.contentWidth/2; bg1.y = display.contentHeight/2

  local bg2 = display.newImageRect( "bg_2.png" ,480 ,320 )
  bg2.x = bg1.x + bg1.width; bg2.y = display.contentHeight/2



-- Declaring a local variable to determine the movement speed --
  local speed = 30



-- Move Function --

  function move()
      bg1.x = bg1.x-speed;
      bg2.x = bg2.x-speed;

      if(bg1.x + bg1.width/2 < 0)then
        bg1.x = bg1.width*3/2-speed
      elseif(bg2.x + bg2.width/2 < 0)then
        bg2.x = bg2.width*3/2-speed
      end
  end
  Runtime:addEventListener( "enterFrame", move )



No comments:

Post a Comment