Back again with another few issues regarding my endless running/sledding game that I’m working on. Basically it revolves around a sled sliding down a hill and is more just me messing with physics and spawning terrain features, etc. but I’m having a few issues.
The first involves scrolling the screen with the player. In my enterFrame event I have the following:
local function frameEvent(event) local deltaX = (-1)\*math.abs(lastX - sled.x); local deltaY = (-1)\*math.abs(lastY - sled.y); for i = tiles.numChildren, 1, -1 do tiles[i]:translate(deltaX, deltaY); if (tiles[i].x + 128) \< display.screenOriginX then display.remove(tiles[i]); end end local speedX, speedY = sled:getLinearVelocity(); if speedX \> 50 then sled:setLinearVelocity(50, speedY); end lastX = sled.x; lastY = sled.y; end
Basically I spawn all of my terrain into the tiles group and want to scroll these tiles backwards as the player moves forwards. However, the following happens:
(Beginning)

(After ~4-5s)

Basically the scrolling is happening but not fast enough, so what’s the best way to do this? I thought basing it on the player’s x and y-cords would work.
My second problem is that the terrain eventually doesn’t spawn correctly (with respect to the y-coords). I’m using the following code right now to randomly generate slopes:
local function newTerrain() local width = math.random(64, 128); local height = math.random(16, 64); local polyVerts = {(-0.5)\*width, (-0.5)\*height, 0.5\*width, 0.5\*height, (-0.5)\*width, 0.5\*height}; local poly = display.newPolygon(tiles, spawnX + 0.5\*width, spawnY + 0.5\*height, polyVerts); poly:setFillColor(0.557, 1, 1); if physicsStarted then physics.addBody(poly, {density = 3, friction = 0.1, bounce = 0.2, shape = polyVerts}); poly.bodyType = "static"; else poly.savedVerts = polyVerts; end local rect = display.newRect(tiles, spawnX + 0.5\*width, spawnY + height + 400, width, 800); rect:setFillColor(0.557, 1, 1); spawnX = spawnX + width; spawnY = spawnY + height; return width; end function scene:show(event) local sceneGroup = self.view; local phase = event.phase; if phase == "will" then physics.start(); physics.setGravity(0, 6); physicsStarted = true; physics.addBody(sled, {density = 3, friction = 0.02, bounce = 0}); for i = tiles.numChildren, 1, -1 do if tiles[i].savedVerts ~= nil then physics.addBody(tiles[i], {density = 3, friction = 0.8, bounce = 0.2, shape = tiles[i].savedVerts}); tiles[i].bodyType = "static"; end end Runtime:addEventListener("enterFrame", frameEvent); elseif phase == "did" then timer.performWithDelay(1, newTerrain, 0); end end
It works for the first part however eventually (after about 20s) the following starts to occur:

It just continues to slowly become more and more offset but I don’t know why.
Also just posted the whole file for reference if that helps: https://hastebin.com/jutoyomofi.lua.
Thanks