Hi all I just started learning Corona and am practicing with a simple space invader like game Ive been doing fairly well so far but have run into a hurdle. I keep getting this error msg
Project sandbox folder: C:\Users\KNED\AppData\Local\Corona Labs\Corona Simulator\Sandbox\adv_scratch…spaceodyssey-339F6B044AEC0E432E0CFFA02BFA6359\Documents
foo
ERROR: Runtime error
C:\Users\KNED\Documents\Corona Projects\Adv_scratch…SpaceOdyssey\main.lua:172: attempt to compare nil with number
stack traceback:
C:\Users\KNED\Documents\Corona Projects\Adv_scratch…SpaceOdyssey\main.lua:172: in function '_listener’
?: in function <?:167>
?: in function <?:169>
It happens when the player collides with a “bolt”. but it desn’t happen all of the time.
Message seems to be pointing me to the underlined section of
this chunk and when I comment this section out everything works:
for i = #boltTable, 1, -1 do local thisBolt = boltTable[i] if ( thisBolt.x \< -1000 or thisBolt.x \> display.contentWidth + 1000 or thisBolt.y \< -100 or thisBolt.y \> display.contentHeight + 100 ) then delete() end end
does anyone know what is wrong. Any help will be appreciated
----------------------------------------------------------------------------------------- -- -- main.lua -- ----------------------------------------------------------------------------------------- --FORWARD VALUES local physics = require( "physics" ) physics.start() physics.setGravity( 0, 0 ) local boltgo local boltTable = {} --[[DECLARE TABLE FOR LATER USAGE]] math.randomseed( os.time() ) -- Setup/Load Background img local bg = display.newImageRect("bg.jpg", display.actualContentWidth, display.actualContentHeight) -- fill screen bg.x =display.contentCenterX bg.y = display.contentCenterY --Setup Img Atlas local data = { frames = { { name=bolt1, x = 172, y = 0, width = 75, height = 178, sourceX=0, sourceY=0, sourceWidth=75 , sourceHeight=178 }, { name=bolt2, x = 172, y = 180, width = 50, height = 112, sourceX=0, sourceY=0, sourceWidth=50 , sourceHeight=112 }, { name=bolt3, x = 0, y = 0, width = 170, height = 400, sourceX=0, sourceY=0, sourceWidth=170 , sourceHeight=400 } }, sheetContentWidth = 256, sheetContentHeight = 512 } local sheetData = { frames = { { name=astro\_0grav1, x = 0, y = 0, width = 318, height = 412, sourceX=51, sourceY=69, sourceWidth=479 , sourceHeight=529 }, { name=astro\_0grav2, x = 320, y = 0, width = 316, height = 412, sourceX=52, sourceY=65, sourceWidth=467 , sourceHeight=522 }, { name=astro\_0grav3, x = 0, y = 414, width = 316, height = 417, sourceX=51, sourceY=74, sourceWidth=495 , sourceHeight=538 }, { name=astro\_0grav4, x = 318, y = 414, width = 316, height = 415, sourceX=52, sourceY=59, sourceWidth=453 , sourceHeight=512 } }, sheetContentWidth = 1024, sheetContentHeight = 1024 } --initiate atlas, expose values (imgs) within var "data" local sheet = graphics.newImageSheet("sprites.png", data) local sheet2 = graphics.newImageSheet("asprites.png", sheetData) --setup imgs local seqData = { {name = "astro", start = 1, count = 2, time = 1700, loopCount = 0, loopDirection = "bounce"} } --[[local seqData = { -- non-consecutive frames sequence { name = "astro", frames = { math.random(3),math.random(3,5)}, time = 4000, loopCount = 0, loopDirection = "forward" }]] local function collision (self,event) if event.phase == "began" then if event.target.myName == "astro" and event.other.myName == "bolt" then display.remove( event.other ) --DELETE BOLT IMSTANCE FROM GAME table.remove( boltTable, i ) print("foo") -- score/alpha else end end end local sprite = display.newSprite(sheet2,seqData) physics.addBody( sprite, "kinematic", { --[[radius=40,]] bounce=0 } ) sprite.x = display.contentCenterX sprite.y = display.contentCenterY + 300 sprite:scale( 0.68, 0.68 ) sprite.myName = "astro" --have to set sequence sprite: setSequence ( "astro" ) --have to play sequence set sprite: play() sprite.collision = collision --setup listener \<\< --- sprite:addEventListener("collision", sprite) -- --listener told to look for sprite.collision ------ --PLAYER TOUCH MOVEMENT local function onBackgroundTouch(event) if(event.phase == "ended") then transition.to(sprite, {x=event.x, y = event.y, time = 1000}) end end bg:addEventListener("touch", onBackgroundTouch) --[[RANDOMLLY SPAWN BOLTS ON SCREEN 2 SECONDS AFTER GAME STARTS SURE 1 DOESNT SPAWN UNDER PLAYER AT START OF SPAWNING--]] local seqData2 = { {name = "bolt", start = 1, count = 1, time = 1700, loopCount = 0, loopDirection = "bounce"}} function boltgo() bolt = display.newSprite(sheet,seqData2) physics.addBody( bolt, "dynamic", { --[[radius=40,]] bounce=0 } ) bolt.x = math.random(- 800, 1800) bolt.y = -50 bolt:scale( math.random(0.68, 1), math.random(0.68,1) ) bolt:setLinearVelocity( 0, math.random( 200,600 ) ) --transition.blink( bolt, { time=3000 } ) -------------------------------------------------------------------------- table.insert( boltTable, bolt ) --[[GIVE DATA TO TABLE (table name, value to be placed in table)]] bolt.myName = "bolt" --[[GIVE THE NEW TABLE VALUE A NAME]] --------------------------------------------------------------------------- --have to set sequence bolt: setSequence ( "bolt" ) --have to play sequence set bolt: play() end function delete(event) display.remove( thisBolt ) --DELETE BOLT IMSTANCE FROM GAME table.remove( boltTable, i ) --DELETE BOLT INSTANCE FROM BOLTTABLE print("removed") end --GAME LOOP: SPAWN BOLTS function manageTime( event ) boltgo() for i = #boltTable, 1, -1 do local thisBolt = boltTable[i] if ( thisBolt.x \< -1000 or thisBolt.x \> display.contentWidth + 1000 or thisBolt.y \< -100 or thisBolt.y \> display.contentHeight + 100 ) then delete() end end end timer.performWithDelay( 1200, manageTime, 0 )