Endless runner top collission

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.

I will try to help if I can…

  1. I did not see an image attached

  2. can you list a link to the tutorial you are using?

  3. it is best to insert you code sample using the  ‘<>’ symbol located on the top part of the entry-window 

  4. Can you post ‘all’ of the code?  …  be sure to use the  ‘<>’ so it formats well and is easier to read

display.setStatusBar(display.HiddenStatusBar) local backgroundMusic = audio.loadStream( "Test.mp3" ) local backgroundMusicChannel = audio.play( backgroundMusic, { channel=1, loops=-1, fadein=5000 } ) -- Game Variables screenXwidth = display.contentWidth screenXcenter = screenXwidth/2 screenYheight = display.contentHeight screenYcenter = screenYheight/2-20 inEvent = 0 eventRun = 0 -- Groups blocks = display.newGroup() characters = display.newGroup() screen = display.newGroup() blocks1 = display.newGroup() require("ground") require("background") require("collisions") require("events") require("hero") require("game\_over") require("scoreboard") require("speed") require("top") screen:insert(backbackground) screen:insert(backgroundfar1) screen:insert(backgroundfar2) screen:insert(backgroundnear1) screen:insert(backgroundnear2) screen:insert(blocks) screen:insert(hero) screen:insert(collisionRect) screen:insert(scoreText) screen:insert(gameOver) screen:insert(blocks1) local function update( event ) if hero.isAlive then checkCollisions() updateSpeed() updateBackgrounds() updateBlocks() updateBlocks1() updateCharacter() else endGame() end end function touched( event ) if hero.isAlive then if(event.phase == "began") then if(onGround) then hero.accel = hero.accel + 20 end if(hasJumped) then hero.accel = hero.accel + 10 end end else if(event.x \< gameOver.x + 150 and event.x \> gameOver.x - 150 and event.y \< gameOver.y + 95 and event.y \> gameOver.y - 95) then restartGame() end end end timer.performWithDelay(1, update, -1) Runtime:addEventListener("touch", touched, -1) -- Creating Animated Character function killHero() hero.isAlive = false end function resetHero() hero.x = 60 hero.y = screenYcenter hero.gravity = -6 hero.accel = 3 hero.isAlive = true hero:setSequence("running") hero:play() hero.rotation = 0 end local options = { width = 100, height = 100, numFrames = 7 } local imageSheet = graphics.newImageSheet( "images/heroSpriteSheet.png", options ) local sequenceData = { { name="running", frames={ 1, 2, 3, 4, 5, 6 }, time=600 }, { name="jumping", start=7, count=1, time=600 } } hero = display.newSprite( imageSheet, sequenceData ) resetHero() 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 character = display.newGroup() character:insert(hero) character:insert(collisionRect) characters:insert(character) function updateCharacter() if(hero.isAlive == true) then if(onGround) then if(wasOnGround) then else hero:setSequence("running") hero:play() end else hero:setSequence("jumping") hero:play() end if(hero.accel \> -2) then hero.accel = hero.accel - 1 end hero.y = hero.y - hero.accel hero.y = hero.y - hero.gravity end collisionRect.y = hero.y -- -- if we are running right then keep moving right -- if(right) then -- hero.x = hero.x + 3 -- --if we are not moving right keep moving left -- else -- hero.x = hero.x - 3 -- end -- --if our hero has run off the screen have him turn -- --and run in the opposite direction. hero.xScale = -1 -- --will flip our sprite horizontally -- if(hero.x \> 380) then -- right = false -- hero.xScale = -1 -- end -- if(hero.x \< -60) then -- right = true -- hero.xScale = 1 -- end end -- Creating Ground groundHeight = 75 groundOffset = 100-groundHeight groundMin = screenYheight+groundHeight groundMax = groundMin-10 groundLevel = groundMin blockXoffset = 119 -- 1 pixel less than background image width for a = 1, 8, 1 do isDone = false numGen = math.random(2) local newBlock if(numGen == 1 and isDone == false) then newBlock = display.newImage("images/ground1.png") isDone = true end if(numGen == 2 and isDone == false) then newBlock = display.newImage("images/ground2.png") isDone = true end newBlock.name = ("block" .. a) newBlock.id = a newBlock.x = ((a-1) \* blockXoffset) newBlock.y = groundLevel blocks:insert(newBlock) end function updateBlocks() for a = 1, blocks.numChildren, 1 do if(a \> 1) then newX = (blocks[a - 1]).x + blockXoffset else newX = (blocks[8]).x + blockXoffset - speed end if((blocks[a]).x \< -40) then (blocks[a]).x, (blocks[a]).y = newX, groundLevel updateScore() else (blocks[a]):translate(speed \* -1, 0) end checkEvent() end end function resetGround() --reset the groundLevel groundLevel = groundMin for a = 1, blocks.numChildren, 1 do blocks[a].x = ((a-1) \* blockXoffset) blocks[a].y = groundLevel end end -- Creating Game Over gameOver = display.newImage("images/gameOver.png") gameOver.name = "gameOver" gameOver.x = 0 gameOver.y = 5000000000000000000 function endGame() speed = 0 gameOver.x = screenXcenter gameOver.y = screenYcenter hero:setSequence("jumping") hero:pause() -- timer.cancel( event.source ) end function restartGame() --move menu gameOver.x = 0 gameOver.y = screenYheight \* 2 resetScore() resetSpeed() resetHero() resetGround() resetBackgrounds() -- timer.performWithDelay(1, update, -1) end ---- Events function checkEvent() if(eventRun \> 0) then eventRun = eventRun - 1 if(eventRun == 0) then inEvent = 0 end end if(inEvent \> 0 and eventRun \> 0) then else local randomMax = 5 check = math.random(randomMax) if(check == randomMax) then inEvent = math.random(5) eventRun = 1 end end if(inEvent \> 0) then runEvent() end end function runEvent() if(inEvent == 5) then groundLevel = groundLevel - 60 else groundLevel = groundMin end if(inEvent == 4) then topLevel = topLevel + 60 else topLevel = topMin end end ------ Collision 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 end -- Creating Background backbackground = display.newImage("images/background.png") backbackground.x = screenXcenter backbackground.y = screenYcenter-groundOffset backgroundfar1 = display.newImage("images/bgfar1.png") backgroundfar1.x = screenXcenter backgroundfar1.y = screenYcenter-groundOffset backgroundfar2 = display.newImage("images/bgfar1.png") backgroundfar2.x = screenXwidth\*1.5 backgroundfar2.y = screenYcenter-groundOffset backgroundnear1 = display.newImage("images/bgnear2.png") backgroundnear1.x = screenXcenter backgroundnear1.y = screenYcenter-groundOffset backgroundnear2 = display.newImage("images/bgnear2.png") backgroundnear2.x = screenXwidth\*1.5 backgroundnear2.y = screenYcenter-groundOffset function updateBackgrounds() --far background movement backgroundfar1.x = backgroundfar1.x - (speed/55) if(backgroundfar1.x \< -(screenXwidth-1)) then backgroundfar1.x = screenXwidth\*2-1 end backgroundfar2.x = backgroundfar2.x - (speed/55) if(backgroundfar2.x \< -(screenXwidth-1)) then backgroundfar2.x = screenXwidth\*2-1 end --near background movement backgroundnear1.x = backgroundnear1.x - (speed/5) if(backgroundnear1.x \< -(screenXcenter-1)) then backgroundnear1.x = screenXwidth\*1.5 end backgroundnear2.x = backgroundnear2.x - (speed/5) if(backgroundnear2.x \< -(screenXcenter-1)) then backgroundnear2.x = screenXwidth\*1.5 end end function resetBackgrounds() backbackground.x = screenXcenter backbackground.y = screenYcenter-groundOffset backgroundfar1.x = screenXcenter backgroundfar1.y = screenYcenter-groundOffset backgroundfar2.x = screenXwidth\*1.5 backgroundfar2.y = screenYcenter-groundOffset backgroundnear1.x = screenXcenter backgroundnear1.y = screenYcenter-groundOffset backgroundnear2.x = screenXwidth\*1.5 backgroundnear2.y = screenYcenter-groundOffset end -- Creating Scoreboard score = 0 scoreText = display.newText("score: 0", 0, 0, "BorisBlackBloxx.ttf",65) scoreText.anchorX, scoreText.anchorY = 0, .5 scoreText.x, scoreText.y = 300, 100 function updateScore() score = score + 1 scoreText.text = "Score: " .. score scoreText.anchorX, scoreText.anchorY = 0, .5 scoreText.x, scoreText.y = 300, 100 end function resetScore() score = 0 scoreText.text = "Score: " .. score end ---- Speed baseSpeed = 5 speed = baseSpeed lastSpeed = speed function updateSpeed() speed = speed + .01 lastSpeed = speed end function resetSpeed() speed = baseSpeed lastSpeed = baseSpeed end ------ Top topHeight = 25 topOffset = 10 topMin = - 50 topMax = - 10 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

Your code looks well formatted and clean, at first glance.  However the first thing I would worry about is there appears to be a number of variables near the top and elsewhere in the code that are global (likely they should be ‘local’)…   some of them likely are included by the 9 require calls you have. 

I was hoping to run the code in my simulator, but I can’t do that without all the 9 ‘required modules’ you list.

So,  quickly looking at the code, the  checkCollisions() function does the killHero() … which I think is your initial question; that is the hero dies as soon as the game starts.

Try  to put some print statements inside the function checkCollisions() ; check and see what the hero’s x and y positions are compared to the ‘collisionRect’ x and y positions.   As well as all the blocks x and y positions.  

It seems like you are checking the collisionRect with the 8 blocks to determine to killHero.  I can guess why that is, but I would think it makes more sense to check the hero position to the collisionRect, to decide to call killHero().

Print statements inside this critical function checkCollisions() will give you at least somewhere to focus…  For instance if any of the blocks (which is what you are checking) are in the collisionRect frame your hero dies…  My guess is this is a vertical scroller and the blocks are scrolling up screen??? as soon as the game starts one fo those blocks collides and kill the hero.

  1. be sure to understand scope. All variables in a module should start with ‘local’, and kept local to the module or local to a function.  There are various schools fo thought on when or if ever to use globals. I am okay with occasional, and specific methods to use globals, but you come looks like you are forgetting or not sure went use ‘local’.

  2. use print statements in that function checkCollisions to see what values are colliding with the collisionRect.  

sorry, 

I just noticed, I guess the collisionRect stays/moves with the hero and collides with the blocks which kills the hero.  So apparently hero is colliding with one of the blocks as soon the game starts.

The print statements inside that collision function will give you some good clues to what may be causing it.

good luck!

Thanks, I will try that.

I will try to help if I can…

  1. I did not see an image attached

  2. can you list a link to the tutorial you are using?

  3. it is best to insert you code sample using the  ‘<>’ symbol located on the top part of the entry-window 

  4. Can you post ‘all’ of the code?  …  be sure to use the  ‘<>’ so it formats well and is easier to read

display.setStatusBar(display.HiddenStatusBar) local backgroundMusic = audio.loadStream( "Test.mp3" ) local backgroundMusicChannel = audio.play( backgroundMusic, { channel=1, loops=-1, fadein=5000 } ) -- Game Variables screenXwidth = display.contentWidth screenXcenter = screenXwidth/2 screenYheight = display.contentHeight screenYcenter = screenYheight/2-20 inEvent = 0 eventRun = 0 -- Groups blocks = display.newGroup() characters = display.newGroup() screen = display.newGroup() blocks1 = display.newGroup() require("ground") require("background") require("collisions") require("events") require("hero") require("game\_over") require("scoreboard") require("speed") require("top") screen:insert(backbackground) screen:insert(backgroundfar1) screen:insert(backgroundfar2) screen:insert(backgroundnear1) screen:insert(backgroundnear2) screen:insert(blocks) screen:insert(hero) screen:insert(collisionRect) screen:insert(scoreText) screen:insert(gameOver) screen:insert(blocks1) local function update( event ) if hero.isAlive then checkCollisions() updateSpeed() updateBackgrounds() updateBlocks() updateBlocks1() updateCharacter() else endGame() end end function touched( event ) if hero.isAlive then if(event.phase == "began") then if(onGround) then hero.accel = hero.accel + 20 end if(hasJumped) then hero.accel = hero.accel + 10 end end else if(event.x \< gameOver.x + 150 and event.x \> gameOver.x - 150 and event.y \< gameOver.y + 95 and event.y \> gameOver.y - 95) then restartGame() end end end timer.performWithDelay(1, update, -1) Runtime:addEventListener("touch", touched, -1) -- Creating Animated Character function killHero() hero.isAlive = false end function resetHero() hero.x = 60 hero.y = screenYcenter hero.gravity = -6 hero.accel = 3 hero.isAlive = true hero:setSequence("running") hero:play() hero.rotation = 0 end local options = { width = 100, height = 100, numFrames = 7 } local imageSheet = graphics.newImageSheet( "images/heroSpriteSheet.png", options ) local sequenceData = { { name="running", frames={ 1, 2, 3, 4, 5, 6 }, time=600 }, { name="jumping", start=7, count=1, time=600 } } hero = display.newSprite( imageSheet, sequenceData ) resetHero() 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 character = display.newGroup() character:insert(hero) character:insert(collisionRect) characters:insert(character) function updateCharacter() if(hero.isAlive == true) then if(onGround) then if(wasOnGround) then else hero:setSequence("running") hero:play() end else hero:setSequence("jumping") hero:play() end if(hero.accel \> -2) then hero.accel = hero.accel - 1 end hero.y = hero.y - hero.accel hero.y = hero.y - hero.gravity end collisionRect.y = hero.y -- -- if we are running right then keep moving right -- if(right) then -- hero.x = hero.x + 3 -- --if we are not moving right keep moving left -- else -- hero.x = hero.x - 3 -- end -- --if our hero has run off the screen have him turn -- --and run in the opposite direction. hero.xScale = -1 -- --will flip our sprite horizontally -- if(hero.x \> 380) then -- right = false -- hero.xScale = -1 -- end -- if(hero.x \< -60) then -- right = true -- hero.xScale = 1 -- end end -- Creating Ground groundHeight = 75 groundOffset = 100-groundHeight groundMin = screenYheight+groundHeight groundMax = groundMin-10 groundLevel = groundMin blockXoffset = 119 -- 1 pixel less than background image width for a = 1, 8, 1 do isDone = false numGen = math.random(2) local newBlock if(numGen == 1 and isDone == false) then newBlock = display.newImage("images/ground1.png") isDone = true end if(numGen == 2 and isDone == false) then newBlock = display.newImage("images/ground2.png") isDone = true end newBlock.name = ("block" .. a) newBlock.id = a newBlock.x = ((a-1) \* blockXoffset) newBlock.y = groundLevel blocks:insert(newBlock) end function updateBlocks() for a = 1, blocks.numChildren, 1 do if(a \> 1) then newX = (blocks[a - 1]).x + blockXoffset else newX = (blocks[8]).x + blockXoffset - speed end if((blocks[a]).x \< -40) then (blocks[a]).x, (blocks[a]).y = newX, groundLevel updateScore() else (blocks[a]):translate(speed \* -1, 0) end checkEvent() end end function resetGround() --reset the groundLevel groundLevel = groundMin for a = 1, blocks.numChildren, 1 do blocks[a].x = ((a-1) \* blockXoffset) blocks[a].y = groundLevel end end -- Creating Game Over gameOver = display.newImage("images/gameOver.png") gameOver.name = "gameOver" gameOver.x = 0 gameOver.y = 5000000000000000000 function endGame() speed = 0 gameOver.x = screenXcenter gameOver.y = screenYcenter hero:setSequence("jumping") hero:pause() -- timer.cancel( event.source ) end function restartGame() --move menu gameOver.x = 0 gameOver.y = screenYheight \* 2 resetScore() resetSpeed() resetHero() resetGround() resetBackgrounds() -- timer.performWithDelay(1, update, -1) end ---- Events function checkEvent() if(eventRun \> 0) then eventRun = eventRun - 1 if(eventRun == 0) then inEvent = 0 end end if(inEvent \> 0 and eventRun \> 0) then else local randomMax = 5 check = math.random(randomMax) if(check == randomMax) then inEvent = math.random(5) eventRun = 1 end end if(inEvent \> 0) then runEvent() end end function runEvent() if(inEvent == 5) then groundLevel = groundLevel - 60 else groundLevel = groundMin end if(inEvent == 4) then topLevel = topLevel + 60 else topLevel = topMin end end ------ Collision 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 end -- Creating Background backbackground = display.newImage("images/background.png") backbackground.x = screenXcenter backbackground.y = screenYcenter-groundOffset backgroundfar1 = display.newImage("images/bgfar1.png") backgroundfar1.x = screenXcenter backgroundfar1.y = screenYcenter-groundOffset backgroundfar2 = display.newImage("images/bgfar1.png") backgroundfar2.x = screenXwidth\*1.5 backgroundfar2.y = screenYcenter-groundOffset backgroundnear1 = display.newImage("images/bgnear2.png") backgroundnear1.x = screenXcenter backgroundnear1.y = screenYcenter-groundOffset backgroundnear2 = display.newImage("images/bgnear2.png") backgroundnear2.x = screenXwidth\*1.5 backgroundnear2.y = screenYcenter-groundOffset function updateBackgrounds() --far background movement backgroundfar1.x = backgroundfar1.x - (speed/55) if(backgroundfar1.x \< -(screenXwidth-1)) then backgroundfar1.x = screenXwidth\*2-1 end backgroundfar2.x = backgroundfar2.x - (speed/55) if(backgroundfar2.x \< -(screenXwidth-1)) then backgroundfar2.x = screenXwidth\*2-1 end --near background movement backgroundnear1.x = backgroundnear1.x - (speed/5) if(backgroundnear1.x \< -(screenXcenter-1)) then backgroundnear1.x = screenXwidth\*1.5 end backgroundnear2.x = backgroundnear2.x - (speed/5) if(backgroundnear2.x \< -(screenXcenter-1)) then backgroundnear2.x = screenXwidth\*1.5 end end function resetBackgrounds() backbackground.x = screenXcenter backbackground.y = screenYcenter-groundOffset backgroundfar1.x = screenXcenter backgroundfar1.y = screenYcenter-groundOffset backgroundfar2.x = screenXwidth\*1.5 backgroundfar2.y = screenYcenter-groundOffset backgroundnear1.x = screenXcenter backgroundnear1.y = screenYcenter-groundOffset backgroundnear2.x = screenXwidth\*1.5 backgroundnear2.y = screenYcenter-groundOffset end -- Creating Scoreboard score = 0 scoreText = display.newText("score: 0", 0, 0, "BorisBlackBloxx.ttf",65) scoreText.anchorX, scoreText.anchorY = 0, .5 scoreText.x, scoreText.y = 300, 100 function updateScore() score = score + 1 scoreText.text = "Score: " .. score scoreText.anchorX, scoreText.anchorY = 0, .5 scoreText.x, scoreText.y = 300, 100 end function resetScore() score = 0 scoreText.text = "Score: " .. score end ---- Speed baseSpeed = 5 speed = baseSpeed lastSpeed = speed function updateSpeed() speed = speed + .01 lastSpeed = speed end function resetSpeed() speed = baseSpeed lastSpeed = baseSpeed end ------ Top topHeight = 25 topOffset = 10 topMin = - 50 topMax = - 10 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

Your code looks well formatted and clean, at first glance.  However the first thing I would worry about is there appears to be a number of variables near the top and elsewhere in the code that are global (likely they should be ‘local’)…   some of them likely are included by the 9 require calls you have. 

I was hoping to run the code in my simulator, but I can’t do that without all the 9 ‘required modules’ you list.

So,  quickly looking at the code, the  checkCollisions() function does the killHero() … which I think is your initial question; that is the hero dies as soon as the game starts.

Try  to put some print statements inside the function checkCollisions() ; check and see what the hero’s x and y positions are compared to the ‘collisionRect’ x and y positions.   As well as all the blocks x and y positions.  

It seems like you are checking the collisionRect with the 8 blocks to determine to killHero.  I can guess why that is, but I would think it makes more sense to check the hero position to the collisionRect, to decide to call killHero().

Print statements inside this critical function checkCollisions() will give you at least somewhere to focus…  For instance if any of the blocks (which is what you are checking) are in the collisionRect frame your hero dies…  My guess is this is a vertical scroller and the blocks are scrolling up screen??? as soon as the game starts one fo those blocks collides and kill the hero.

  1. be sure to understand scope. All variables in a module should start with ‘local’, and kept local to the module or local to a function.  There are various schools fo thought on when or if ever to use globals. I am okay with occasional, and specific methods to use globals, but you come looks like you are forgetting or not sure went use ‘local’.

  2. use print statements in that function checkCollisions to see what values are colliding with the collisionRect.  

sorry, 

I just noticed, I guess the collisionRect stays/moves with the hero and collides with the blocks which kills the hero.  So apparently hero is colliding with one of the blocks as soon the game starts.

The print statements inside that collision function will give you some good clues to what may be causing it.

good luck!

Thanks, I will try that.