So I’ve been following a tutorial and now I am trying to add my own things to the endless runner game. I am trying to add a top so a character can die if it hits it. There is a screen shot of the game attached in the file.
Collision Script
function checkCollisions()
wasOnGround = onGround
hasJumped = true
for a = 1, blocks.numChildren, 1 do
if(collisionRect.y - 10> blocks[a].y - 170 and blocks[a].x - 40 < collisionRect.x and blocks[a].x + 40 > collisionRect.x) then
speed = 0
killHero()
local tickSound = audio.loadSound( “Death.wav” )
audio.play( tickSound, { onComplete=killHero } )
end
end
for a = 1, blocks.numChildren, 1 do
if(hero.y >= blocks[a].y - 170 and blocks[a].x < hero.x + 60 and blocks[a].x > hero.x - 60) then
hero.y = blocks[a].y - 171
onGround = true
break
else
onGround = false
end
end
for a = 1, blocks.numChildren, 1 do
if(hero.y >= blocks[a].y - 170 and blocks[a].x < hero.x + 25 and blocks[a].x > hero.x - 80) then
hasJumped = true
end
end
for a = 1, blocks1.numChildren, 1 do
if(collisionRect.y - 10> blocks1[a].y - 170 and blocks1[a].x - 40 < collisionRect.x and blocks1[a].x + 40 > collisionRect.x) then
speed = 0
killHero()
local tickSound = audio.loadSound( “Death.wav” )
audio.play( tickSound, { onComplete=killHero } )
end
end
end
Top blocks script
topHeight = 25
topOffset = 10
topMin = - 50
topMax = 5
topLevel = topMin
topXoffset = 119 – 1 pixel less than background image width
for a = 1, 8, 1 do
isDone = false
numGen = math.random(2)
local newBlock1
if(numGen == 1 and isDone == false) then
newBlock1 = display.newImage(“images/top1.png”)
isDone = true
end
if(numGen == 2 and isDone == false) then
newBlock1 = display.newImage(“images/top2.png”)
isDone = true
end
newBlock1.name = (“block1” … a)
newBlock1.id = a
newBlock1.x = ((a-1) * topXoffset)
newBlock1.y = topLevel
blocks1:insert(newBlock1)
end
function updateBlocks1()
for a = 1, blocks1.numChildren, 1 do
if(a > 1) then
newX = (blocks1[a - 1]).x + topXoffset
else
newX = (blocks1[8]).x + topXoffset - speed
end
if((blocks1[a]).x < -40) then
(blocks1[a]).x, (blocks1[a]).y = newX, topLevel
updateScore()
else
(blocks1[a]):translate(speed * -1, 0)
end
checkEvent()
end
end
function resetGround()
--reset the groundLevel
topLevel = topMin
for a = 1, blocks.numChildren, 1 do
blocks1[a].x = ((a-1) * topXoffset)
blocks1[a].y = topLevel
end
end
Character collision rect
collisionRect = display.newRect(hero.x + 22, hero.y, 1, 70)
collisionRect.strokeWidth = 1
collisionRect:setFillColor(140, 140, 140)
collisionRect:setStrokeColor(180, 180, 180)
collisionRect.alpha = 0
Right now with all of this the character just keeps dying in mid air every time I start up the game. And I’ve tried changing some things around but I couldn’t find what’s wrong with it.
If anyone has any good ideas, I’d appreciate the help.
Thanks in advance.