Hi, I’m trying to create a collision between my character and a rock on the ground in my game.lua file but I can’t get the collision function to work exclusively between the character(player) and the rock on the ground. I want the character to jump over the rock and go to restart if it hits the rock. My issue is that when I do try to make the player jump with a tap on the screen, instead of going up and coming back down to its starting x position it goes up and goes off screen and then the program notices a collision I guess. Any help with possible fixes would be deeply appreciated. I would like the player to go up and back down to its starting x without the program noticing a collision between the scrolling platforms or going off screen.Here is what I have in my lua file:
local physics = require “physics”
physics.start()
physics.setGravity(0,100)
local mydata = require(“mydata”)
local storyboard = require (“storyboard”)
local scene = storyboard.newScene()
mydata.score = 0
function scene:createScene(event)
local screenGroup = self.view
local background = display.newImage(“MeteorSprintbgv2.png”)
screenGroup:insert(background)
bg = display.newImageRect(“MeteorSprintbgv2.png”,920,1425)
bg.anchorX = 0
bg.anchorY = 1
bg.x = 0
bg.y = display.contentHeight
screenGroup:insert(bg)
elements = display.newGroup()
elements.anchorChildren = true
elements.anchorX = 0
elements.anchorY = 1
elements.x = 0
elements.y = 0
screenGroup:insert(elements)
platform = display.newImageRect(“SideScroller.png”,920,576)
platform.anchorX = 0
platform.anchorY = 1
platform.x = 0
platform.y = display.contentHeight - 135
platform.speed = 10
screenGroup:insert(platform)
platform2 = display.newImageRect(“Platform.png”,920,100)
platform2.anchorX = 0
platform2.anchorY = 1
platform2.x = 0
platform2.y = display.contentHeight - 40
platform2.speed = 10
screenGroup:insert(platform2)
platform3 = display.newImageRect(“SideScroller.png”,920,576)
platform3.anchorX = 0
platform3.anchorY = 1
platform3.x = platform3.width
platform3.y = display.contentHeight - 135
platform3.speed = 10
screenGroup:insert(platform3)
platform4 = display.newImageRect(“Platform.png”,920,100)
platform4.anchorX = 0
platform4.anchorY = 1
platform4.x = platform4.width
platform4.y = display.contentHeight - 40
platform4.speed = 10
screenGroup:insert(platform4)
p_options =
{
– Required params
width = 150,
height = 150,
numFrames = 4,
– content scaling
sheetContentWidth = 600,
sheetContentHeight = 150,
}
playerSheet = graphics.newImageSheet( “DinoColor1.png”, p_options )
player = display.newSprite( playerSheet, { name=“player”, start=1, count=4, time=250 } )
player.anchorX = 0.5
player.anchorY = 0.5
player.x = display.contentCenterX - 275
player.y = display.contentHeight - 210
physics.addBody(player, “static”, {density=.1, bounce=0.1, friction=1})
player:applyForce(0, -300, player.x, player.y)
player:play()
screenGroup:insert(player)
scoreText = display.newText(mydata.score,display.contentCenterX,
150, “Showcard Gothic Regular”, 58)
scoreText:setFillColor(0,0,0)
scoreText.alpha = 0
screenGroup:insert(scoreText)
instructions = display.newImageRect(“Instructions.png”,400,328)
instructions.anchorX = 0.5
instructions.anchorY = 0.5
instructions.x = display.contentCenterX
instructions.y = display.contentCenterY + 100
screenGroup:insert(instructions)
end
function onCollision(event)
if ( event.phase == “began” ) then
storyboard.gotoScene( “restart” )
end
end
function waterScroller(self,event)
if self.x < (-900 + (self.speed*2)) then
self.x = 900
else
self.x = self.x - self.speed
end
end
–[[function groundScroller(self,event)
if self.x < (-900 + (self.speed*2)) then
self.x = 900
else
self.x = self.x - self.speed
end
end]]–
local gameStarted = false
function jumpUp(event)
if event.phase == “began” then
if gameStarted == false then
player.bodyType = “dynamic”
instructions.alpha = 0
scoreText.alpha = 1
addMeteorTimer = timer.performWithDelay(1000, addMeteor, -1)
moveMeteorTimer = timer.performWithDelay(2, moveMeteor, -1)
gameStarted = true
player:applyForce(0, -300, player.x, player.y)
else
player:applyForce(0, -200, player.x, player.y)
end
end
end
function moveRock()
for a = elements.numChildren,1,-1 do
if(elements[a].x < display.contentCenterX - 170) then
if elements[a].scoreAdded == false then
mydata.score = mydata.score + 1
scoreText.text = mydata.score
elements[a].scoreAdded = true
end
end
if(elements[a].x > -100) then
elements[a].x = elements[a].x - 12
else
elements:remove(elements[a])
end
end
end
function addRock()
height = math.random(display.contentCenterY - 200, display.contentCenterY + 200)
Rock = display.newImageRect(‘meteor1.png’,50,50)
Rock.anchorX = 0.5
Rock.anchorY = 0
Rock.x = display.contentWidth + 100
Rock.y = height + 160
physics.addBody(Rock, “static”, {density=1, bounce=0.1, friction=.2})
elements:insert(rock)
end
local function checkMemory()
collectgarbage( “collect” )
local memUsage_str = string.format( “MEMORY = %.3f KB”, collectgarbage( “count” ) )
print( memUsage_str, "TEXTURE = "…(system.getInfo(“textureMemoryUsed”) / (1024 * 1024) ) )
end
function scene:enterScene(event)
storyboard.removeScene(“start”)
Runtime:addEventListener(“touch”, jumpUp)
platform.enterFrame = waterScroller
Runtime:addEventListener(“enterFrame”, platform)
platform2.enterFrame = waterScroller
Runtime:addEventListener(“enterFrame”, platform2)
platform3.enterFrame = waterScroller
Runtime:addEventListener(“enterFrame”, platform3)
platform4.enterFrame = waterScroller
Runtime:addEventListener(“enterFrame”, platform4)
Runtime:addEventListener(“collision”, onCollision)
memTimer = timer.performWithDelay( 1000, checkMemory, 0 )
end
function scene:exitScene(event)
Runtime:removeEventListener(“touch”, jumpUp)
Runtime:removeEventListener(“enterFrame”, platform)
Runtime:removeEventListener(“enterFrame”, platform2)
Runtime:removeEventListener(“enterFrame”, platform3)
Runtime:removeEventListener(“enterFrame”, platform4)
Runtime:removeEventListener(“collision”, onCollision)
timer.cancel(addMeteorTimer)
timer.cancel(moveMeteorTimer)
timer.cancel(memTimer)
end
function scene:destroyScene(checkMemory)
end
scene:addEventListener(“createScene”, scene)
scene:addEventListener(“enterScene”, scene)
scene:addEventListener(“exitScene”, scene)
scene:addEventListener(“destroyScene”, scene)
return scene