Hey everybody, i have a really annoying touch event in my game.
first of all, what i want this touch event to do is that when i touch either two of these display objects, i want them to switch to the object their assigned to. for instance if i touch the orange ball, i will be shooting the orange ball out of the cannon, etc, this is my problem and the simulator comes up with the Error
attempt to index Global 'event'
you’ll most likley find this problem in the newProj function. Here is my code. i know this is a simple question but i guess its better to be safe than sorry.
display.setStatusBar( display.HiddenStatusBar ) -- \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* -- DECLARE VARIABLES -- \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* -- local Performance = require('performance') local points local points2 -- = {} local proj -- = {} -- make it a table local projFiring local displayScore local scoreObj local score = 0 local ox, oy = math.abs(display.screenOriginX), math.abs(display.screenOriginY) local cw, ch = display.contentWidth, display.contentHeight local projCollisionFilter = { categoryBits = 2, maskBits = 5 } local pointsCollisionFilter = { categoryBits = 1, maskBits = 2 } local wallsCollisionFilter = { categoryBits = 4, maskBits = 2 } local proj2 local sky local wallL local wallR local wallB local wallT local DecorationWall local DecorationWall2 local DecorationWall3 local DecorationWall4 local platform local ZeroGravityBanana local normalBanana local setUp local turret local displayAmmo local ammoObj local Ammo = 6 local physics = require( "physics") physics.start( ) physics.setGravity( 0,4.85 ) physics.setDrawMode( "normal" ) local physicsData = (require "JunglePlatform").physicsData(1.0) -- \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* -- MAIN PART OF CODE -- \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* local function addPoints(points) score = score + points scoreObj.text = score scoreObj:setReferencePoint(display.CenterLeftReferencePoint) end local function pointsCollide(event,self) local phase = event.phase if (phase == "began") then if (event.other.type == "Melon") then addPoints(5) event.other:removeSelf( ) end if (event.other.type == "strawBerry") then addPoints(10) event.other:removeSelf( ) end if (event.other.type == "pineApple") then addPoints(20) event.other:removeSelf( ) end end end local function minusAmmo(ammoStuff) Ammo = Ammo - ammoStuff ammoObj.text = Ammo ammoObj:setReferencePoint(display.CenterLeftReferencePoint) end local function buildPlayArea() sky = display.newImage("Images/jungleBackground.png") sky.x = display.contentWidth \* .5 + 3 --sky:setFillColor(144, 251, 255 ) wallL = display.newRect( -ox, -oy,3, ch+oy+oy ) wallL.isVisible = false physics.addBody(wallL, "static", { bounce=0.6, friction=1.0, filter = wallsCollisionFilter } ) wallL.name = "notInteractive" wallR = display.newRect( cw-3+ox, -oy, 3, ch+oy+oy ) wallR.isVisible = false physics.addBody(wallR, "static", { bounce=0.6, friction=1.0, filter= wallsCollisionFilter } ) wallR.name = "notInteractive" wallB = display.newRect( -ox, ch-3+oy, cw+ox+ox, 3 ) wallB.isVisible = false physics.addBody(wallB, "static", { bounce=0.6, friction=1.0, filter = wallsCollisionFilter } ) wallB.name = "notInteractive" wallT = display.newRect( -ox, -oy, cw+ox+ox, 3 ) wallT.isVisible = false physics.addBody(wallT, "static", { bounce=0.6, friction=1.0, filter = wallsCollisionFilter } ) wallT.name = "notInteractive" DecorationWall = display.newImage( "Images/walls.png") DecorationWall.y = 6 DecorationWall2 = display.newImage("Images/walls.png" ) DecorationWall2.rotation = 90 DecorationWall2.x = 0 DecorationWall2.y = 280 DecorationWall3 = display.newImage("Images/walls.png" ) DecorationWall3.y = 760 DecorationWall4 = display.newImage("Images/walls.png" ) DecorationWall4.rotation = 90 DecorationWall4.x = 1000 DecorationWall4.y = 280 platform = display.newImage("Images/JunglePlatform.png") physics.addBody(platform,"static",physicsData:get("JunglePlatform") ) platform.x = display.contentWidth \* .5 platform.y = display.contentHeight \*.5 -250 platform2 = display.newImage( "Images/JunglePlatform.png" ) physics.addBody(platform2,"static",physicsData:get("JunglePlatform") ) platform2.rotation = 90 platform2.x = 195 platform2.y = 389 platform3 = display.newImage( "Images/JunglePlatform.png" ) physics.addBody(platform3,"static",physicsData:get("JunglePlatform") ) platform3.rotation = 90 platform3.x = 825 platform3.y = 389 displayScore = display.newText("Score: ",0,0,native.systemFont,30) --scoreLabel:setFillColor(246, 255, 218) displayScore.x = 450 displayScore.y = 30 scoreObj = display.newText(tostring(score),0,0,native.systemFont,30) scoreObj.x = displayScore.x + 80 scoreObj.y = displayScore.y end local function showBananaz() displayAmmo = display.newText("Bananaz: ",0,0,native.systemFont,30) --scoreLabel:setFillColor(246, 255, 218) displayAmmo.x = 250 displayAmmo.y = 30 ammoObj = display.newText(tostring(Ammo),0,0,native.systemFont,30) ammoObj.x = displayAmmo.x + 80 ammoObj.y = displayAmmo.y turret = display.newImage( "Images/magnet.png" ) turret.x = display.contentWidth \* .5 turret.y = display.contentHeight \* .5 + 250 end local function buildPoints() for xVal = 1,6 do points = display.newImage( "Images/pineApple.png") points.type = "pineApple" physics.addBody(points,"static",{ filter = pointsCollisionFilter,radius = 35 }) points.isSensor = true points.x = (xVal \*80) + 235 points.y = 210 --points.collision = pointsCollide points:addEventListener( "collision", pointsCollide ) end for xVal = 1,5 do points2 = display.newImage( "Images/waterMelon.png") points2.type = "Melon" physics.addBody(points2,"static",{ filter = pointsCollisionFilter,radius = 35 }) points2.isSensor = true points2.x = 265 points2.y = (xVal \*80) + 210 --points.collision = pointsCollide points2:addEventListener( "collision", pointsCollide ) end for xVal = 1,5 do points2 = display.newImage( "Images/strawBerry.png") points2.type = "strawBerry" physics.addBody(points2,"static",{ filter = pointsCollisionFilter,radius = 35 }) points2.isSensor = true points2.x = 760 points2.y = (xVal \*80) + 210 --points.collision = pointsCollide points2:addEventListener( "collision", pointsCollide ) end end local function newProj() if (event.phase == "began") then if self.type == "ZeroGravityBanana" then proj = display.newImage("Images/ZeroGravityBanana.png") physics.addBody( proj, "dynamic", {density=1, friction=0.5, bounce=0.7 ,radius = 25 ,filter= projCollisionFilter} ) proj.x = display.contentWidth \* .5 proj.y = display.contentHeight \* .5 + 250 projFiring = false proj.gravityScale = 0 proj.isBullet = true proj.isBodyActive = false proj.isVisible = false proj.type = "proj" proj.collision = pointsCollide proj:addEventListener( "collision", pointsCollide ) end if self.type == "normalBanana" then proj = display.newImage("Images/Bananaz.png") physics.addBody( proj, "dynamic", {density=1, friction=0.5, bounce=0.7 ,radius = 25 ,filter= projCollisionFilter} ) proj.x = display.contentWidth \* .5 proj.y = display.contentHeight \* .5 + 250 projFiring = false proj.isBullet = true proj.isBodyActive = false proj.isVisible = false proj.type = "proj" proj.collision = pointsCollide proj:addEventListener( "collision", pointsCollide ) end --[[proj = display.newImage("Images/ZeroGravityBanana.png") --proj:setFillColor( 100,225,300 ) physics.addBody( proj, "dynamic", {density=1, friction=0.5, bounce=0.7 ,radius = 25 ,filter= projCollisionFilter} ) proj.x = display.contentWidth \* .5 proj.y = display.contentHeight \* .5 + 250 projFiring = false proj.gravityScale = 0 proj.isBullet = true proj.isBodyActive = false proj.isVisible = false proj.type = "proj" proj.collision = pointsCollide proj:addEventListener( "collision", pointsCollide )]]-- end end local function showOptions() ZeroGravityBanana = display.newImage( "Images/ZeroGravityBanana.png" ) ZeroGravityBanana:scale(1.5,1.5) ZeroGravityBanana.x = 700 ZeroGravityBanana.type = "ZeroGravityBanana" normalBanana = display.newImage( "Images/Bananaz.png" ) normalBanana:scale(1.5,1.5) normalBanana.x = 800 normalBanana.type = "normalBanana" ZeroGravityBanana:addEventListener( "touch",newProj ) normalBanana:addEventListener( "touch",newProj ) end local function touchAction(event) if ( event.phase == "ended" and projFiring == false ) then minusAmmo(1) projFiring = true proj.isBodyActive = true proj:applyTorque( 200 ) proj.isVisible = true local px,py = event.x-proj.x, event.y-proj.y proj:applyLinearImpulse( px/5, py/5, proj.x, proj.y ) proj:applyTorque( 50 ) if (Ammo == 0) then projFiring = nil end if (Ammo \> 0) then projFiring = true end if (projFiring == true) then newProj() end end end showOptions() buildPlayArea() showBananaz() buildPoints() newProj() Runtime:addEventListener( "touch", touchAction ) --Runtime:addEventListener( "touch", touchAction )