hey everybody
in my game, i am creating a collision event, were as when you collide with the object, you get 100 points, but there is a problem with that in my game, here it is. its is either coming up with an error saying "attempt to index upvalue enemyBalloon or “trying to compare number with nil”. can someone please help me, im really urgent. thanks!
Hazza
display.setStatusBar(display.HiddenStatusBar) local physics = require "physics" physics.start();physics.setGravity(0,2.7) local ox, oy = math.abs(display.screenOriginX) + 25, math.abs(display.screenOriginY) local cw, ch = display.contentWidth, display.contentHeight local stage = display.getCurrentStage() physics.setDrawMode("normal") local foreGround = display.newGroup() local Balloons = display.newGroup() Balloons.isVisible = true local scoreLabel local scoreObj local score = 0 --------------------------------------------------------------------- --FOREGROUND OBJECTS -------------------------------------------------------------------- local background = display.newImage(foreGround,"background.jpg") background.x = 0 local ground = display.newImage(foreGround,"floor.png") physics.addBody(ground,"static") ground.x = 10 ground.y = 480 ground.myName = "removeObject" local turret = display.newImageRect(foreGround,"BalloonTurret.png",134,134) turret:setFillColor(45, 255, 215) turret.x = 160 turret.y = 420 local ox, oy = math.abs(display.screenOriginX), math.abs(display.screenOriginY) local cw, ch = display.contentWidth, display.contentHeight local wallL = display.newRect( -ox, -oy,1.5, ch+oy+oy ) wallL.isVisible = false physics.addBody(wallL, "static", { bounce=0.6, friction=1.0 } ) wallL.type = "notSticky" local wallR = display.newRect( cw-1.5+ox, -oy, 1.5, ch+oy+oy ) wallR.isVisible = false physics.addBody(wallR, "static", { bounce=0.6, friction=1.0 } ) wallR.type = "notSticky" local wallT = display.newRect( -ox, -oy, cw+ox+ox, 1.5 ) wallT.isVisible = false physics.addBody(wallT, "static", { bounce=0.6, friction=1.0 } ) wallT.isSensor = true wallT.y = -74 wallT.myName = "removeObject" ------------------------------------------------------------- ------------------------------------------------------------- local projFiring = false local proj local enemyBalloon local scaleFactor = 1.0 local physicsData = (require "Balloon").physicsData(1.0) --display.newRoundedRect( left, top, width, height, cornerRadius ) local myRoundedRect local idx = 0 for xVal = 1,9 do idx = idx + 1 myRoundedRect = display.newRoundedRect(0, 0, 20, 10, 3) myRoundedRect.x = (xVal \* 34) -10 myRoundedRect.y = 60 myRoundedRect.alpha = 0.4 myRoundedRect.strokeWidth = 3 myRoundedRect:setFillColor(140, 140, 140) myRoundedRect:setStrokeColor(220, 255, 216) end local function addToScore(num) score = score + num scoreObj.text = score scoreObj:setReferencePoint(display.CenterLeftReferencePoint) if enemyBalloon.y \> myRoundedRect.y then score = 200 elseif enemyBalloon.y \< myRoundedRect.y then score = 100 end end scoreLabel = display.newText("Score: ",0,0,native.systemFont,18) --scoreLabel:setFillColor(246, 255, 218) scoreLabel.x = 250 scoreLabel.y = 10 scoreObj = display.newText(tostring(score),0,0,native.systemFont,18) scoreObj.x = scoreLabel.x + 32 scoreObj.y = scoreLabel.y local t --collision handler local function projCollide( self,event ) if ( event.phase == "began" ) then --get world coordinates of projectile for joint reference --self:removeEventListener( "collision", self ) ; self.collision = nil --delay function to resolve collision local function resolveColl( timerRef ) if ( timerRef.source.action == "makeJoint" ) then local weldJoint = physics.newJoint( "weld", self, event.other, self.x, self.y ) end end --check if velocity of projectile is sufficient to "stick" local vx,vy = self:getLinearVelocity() local dirVel = math.sqrt( (vx\*vx)+(vy\*vy) ) if ( dirVel \> 10 ) then --if sufficient, stop velocity and trigger joint creation self:setLinearVelocity( 0,0 ) t = timer.performWithDelay( 10, resolveColl, 1 ) ; t.action = "makeJoint" addToScore(100) else --if not sufficient, "break" projectile and create new t = timer.performWithDelay( 10, resolveColl, 1 ) ; t.action = "none" end if (event.other.type == "notSticky") then --t = timer.performWithDelay(10,resolveColl,1) t.action = "none" elseif (event.other.myName == "removeObject") then self:removeSelf();t.action = "none";self = nil print("i got removed") end end end local function newProj() proj = display.newImage(Balloons,"Balloon.png"); proj.x = turret.x ; proj.y = turret.y - 20 physics.addBody( proj, "dynamic",physicsData:get("balloon") ) proj.gravityScale = -5 projFiring = false proj.isBullet = true proj.isBodyActive = false proj.isVisible = false end local function spawnEnemy() enemyBalloon = display.newImage(Balloons,"Balloon.png") physics.addBody(enemyBalloon,"dynamic", physicsData:get("balloon") ) enemyBalloon.x = math.random(0,310) enemyBalloon.y = -55 enemyBalloon:setFillColor( 214, 0, 19 ) enemyBalloon:applyTorque( 50 ) enemyBalloon.type = "notSticky" enemyBalloon.collision = projCollide enemyBalloon:addEventListener("collision",enemyBalloon) return enemyBalloon end local function touchAction(event) if ( event.phase == "began" and projFiring == false ) then projFiring = true proj.isBodyActive = true proj.isVisible = true local px,py = event.x-proj.x, event.y-proj.y proj:applyLinearImpulse( px/8, py/8, proj.x, proj.y ) proj:applyTorque( 50 ) proj.gravityScale = -5 proj.collision = projCollide ; proj:addEventListener( "collision", proj ) end if (projFiring == true) then newProj() end end newProj() stage:addEventListener( "touch", touchAction ) local spawnTmr local spawnTime = 5000 local difficultyLevels = 4 --local spawnTimer = timer.performWithDelay(5000,spawnEnemy, 0 )---- THIS IS AN INFINITE LOOP HARRY ,U MUST YOU'SE THIS A LOT local function increaseDifficulty() if spawnTmr ~= nil then timer.cancel(spawnTmr) spawnTmr = nil end spawnTime = spawnTime - 1000 -- just to be safe, set this to whatever spawn rate would be the -- fastest, so game never spawns to fast if spawnTime \< 1000 then spawnTime = 3000 end -- spawn at this rate until this timer is canceled spawnTmr = timer.performWithDelay(spawnTime, spawnEnemy, 0) end -- this timer loops only till spawning at most difficult speed is reached local difficultyTmr = timer.performWithDelay(5000, increaseDifficulty,difficultyLevels )