0
votes

I am working on a car racing game and one of the things I am trying to achieve is to ensure that the background image moves vertically infinitely while at the same time when you touch the background, it changes the car's lanes.

function scene:create( event )
  physics.start();
  physics.setGravity(0, 0);

local group = self.view;

local bg = display.newImageRect(group, "IMG/bg.png", 384, totalHeight);
bg.x, bg.y = centerX, centerY;


function bg:touch(event)
    if event.phase == "began" then
      if event.x < centerX then
        redCar:changeLane();
      else
        blueCar:changeLane();
      end
    end
  end
  bg:addEventListener("touch", bg);
end

I have tried different articles online but none works well. Any guide would be appreciated. Thanks

1
Try JungleScene demo from Corona Sample's it has horizontal infinite scrolling scene which you can change to vertical - iBad
You might want to check my previous answer, just change x to y to move vertically: stackoverflow.com/questions/26828279/… - Melquiades

1 Answers

0
votes

Here is the code to make a vertical scrolling background:

local function moveBg(dt)  
  bg1.y = bg1.y + scrollSpeed * dt
  bg2.y = bg2.y + scrollSpeed * dt
  if (bg1.y - display.contentHeight/2) > display.actualContentHeight then
      bg1:translate(0, -bg1.contentHeight * 2)
  end
  if (bg2.y - display.contentHeight/2) > display.actualContentHeight then
      bg2:translate(0, -bg2.contentHeight * 2)
  end
end

And the complete tutorial is here: http://lomza.totem-soft.com/tutorial-scrollable-background-in-corona-sdk/