Actually I’m not going to change my setup
- because I don’t know what I would change.
- Going back to #1 It is formatted just fine so I don’t see why I should change it.
Anyways, here are both my functions for creating&shooting a player/user bullet and my setup for creating a PTmine:
– PTmine –
local function PTMBtnSetAlpha(event) PTMBtn.alpha = 1 end -- Create PTmine local function buildPTmine() local PTmineG = display.newGroup() PTmineG.PTmineTmr = timer.performWithDelay() PTmineG.PTmineBWtmr = timer.performWithDelay() --PTMBtn.alpha = .7 print( "creating PTmine" ) PTmineG.body = loader:createSpriteFromSHDocument("PTmine", "GameFX", "LevelAssets.pshs"); physics.addBody( PTmineG.body , "dynamic", { friction= 0, bounce= 1.0, density= .6, radius= 10, isSensor = true, filter = PTmineCollisionFilter} ); PTmineG.body.x = user\_tank.x; PTmineG.body.y = user\_tank.y + 5; PTmineG.body:scale(.2,.2) PTmineG.body.class = "PTmine" ------------------------------------------------------------------------ -- Blow up PTmine function function ExplodeMine(self) --local parentMine = PTmines[self.\_index] PTmineCount = PTmineCount - 1 print(" PTmineCount: " .. PTmineCount) ExplosionEmitter = Particles.GetEmitter("ExplosionEmitter") ExplosionEmitter.x = self.x ExplosionEmitter.y = self.y Particles.StartEmitter("ExplosionEmitter") PTmineG.BW = display.newCircle(self.x, self.y, 10) PTmineG.BW.isVisible = false PTmineG.BW.class = "PTmineBW" -- increases PTmine's radius to act as a type of blast-wave, and then checks for collisions using the 'onPTmineBWCollision' funciton physics.addBody(PTmineG.BW, "dynamic", {radius = 60, isSensor = true}) print("PTmineBW size increased") PTmineG:insert( PTmineG.BW ) PTmineG.BW.\_index = PTmineG.body.\_index -- remove PTmineBW function removeBW() display.remove(PTmineG.BW) print("No collisions registered with the BW. BW removed") end -- Collision detection function for PTmineBW local onPTmineBWCollision = function(self, event) if event.phase == "began" and event.other.class == "Dbarrier" then if PTmineG.PTmineBWtmr then timer.cancel(PTmineG.PTmineBWtmr) end print("BW hit PlayerTank") display.remove(self) display.remove(event.other) end return true end PTmineG.BW.collision = onPTmineBWCollision PTmineG.BW:addEventListener( "collision", PTmineG.BW ) PTmineG.PTmineBWtmr = timer.performWithDelay(15, removeBW) display.remove(self) end ---------------------------------------------------------------------------- -- Collision function for PTmineG local onPTmineGCollision = function(self, event) local parentMine = PTmines[self.\_index] if ( event.phase == "began" and event.other.class == "bullet" or event.other.class == "EnemyBullet" ) then if PTmineG.PTmineTmr then timer.cancel(PTmineG.PTmineTmr) end print("PTmineG.PTmineTmr stopped.") print("PlayerBulletCount: " .. PlayerBulletCount) PlayerBulletCount = PlayerBulletCount - 1 Particles.DeleteEmitter(event.other.emitterRef) display.remove(event.other) timer.performWithDelay(5, function() ExplodeMine( PTmineG.body )end, 1) end return true end PTmineG.body.collision = onPTmineGCollision PTmineG.body:addEventListener("collision", PTmineG.body) PTmineG.PTmineTmr = timer.performWithDelay(6000, function() ExplodeMine(PTmineG.body) end, 1) ------------------------------------------------------------------------ camera:add( PTmineG, 7, false ) PTmineG:insert( PTmineG.body ) table.insert(PTmines, PTmineG) PTmineG.body.\_index = table.indexOf(PTmines, PTmineG) return PTmineG end -- on PTmine button release local function onPTMBtnRelease(event) if PTmineCount \<= 1 then PTmineCount = PTmineCount + 1 print(" PTmineCount: " .. PTmineCount) timer.performWithDelay(10, buildPTmine) elseif PTmineCount == 2 then print("Already two mines present. End function.") end end PTMBtn = widget.newButton{ defaultFile = "GUI\_assets/But\_shooting.png", overFile = "GUI\_assets/But\_shooting-over.png", onPress = PTMBtnSetAlpha, onRelease = onPTMBtnRelease -- Event listener function } PTMBtn:setReferencePoint( display.CenterReferencePoint ); PTMBtn.x = 935; PTMBtn.y = 675; PTMBtn:scale(.755, .755); PTMBtn.alpha = .7 PTMBtnbgi = display.newImage("GUI\_assets/Bg\_shooting.png") PTMBtnbgi.x = PTMBtn.x; PTMBtnbgi.y = PTMBtn.y; PTMBtnbgi:scale(.4, .4) local function onPTMBtnTap( event ) local T = event.target if (event.phase == "began") then display.getCurrentStage():setFocus( T, event.id ) T.isFocus = true CreatePTmine() elseif event.phase == "ended" then display.getCurrentStage():setFocus( T, nil) T.isFocus = false end return true; end --Adds an event listener to the transRect PTMBtn:addEventListener( "tap", onPTMBtnTap )
– user bullet –
local function shootBullet() audio.play( tank\_shot ) local bullet = display.newGroup() PlayerBulletCount = PlayerBulletCount + 1 print("PlayerBulletCount increased by 1") emitterCount = emitterCount + 1 local pt = mathapi.rotateAboutPoint( {x=user\_turret.x,y=user\_turret.y-47}, user\_turret, user\_turret.rotation - 90 ) -- In bullet fire function local emitterName = "E" .. emitterCount local Emitter = display.newGroup() --bullet attributes bullet.body = loader:createSpriteFromSHDocument("Weapon3ex", "GameFX", "LevelAssets.pshs"); bullet.body.x = pt.x; bullet.body.y = pt.y; physics.addBody( bullet.body , "dynamic", { friction= 0, bounce= 1.0, density= .6, radius= 7, filter = bulletCollisionFilter} ); bullet.body:scale(.63,.57); bullet.body.class = "bullet" bullet.body.isBullet = true bullet.body.rotation = user\_turret.rotation + 90 bullet.body:applyForce( pt.x - user\_turret.x, pt.y - user\_turret.y, user\_turret.x, user\_turret.y ) -- Bullet Emitter Particles.CreateEmitter(emitterName, 0, 0, 0, false, true) -- Uses the base name (generated dynamically with each tank) bullet.body.emitter = Particles.GetEmitter(emitterName) bullet.body.emitterRef = emitterName Particles.CreateParticleType ("PBulletSTrail", { imagePath = "gfx/smoke01.png", imageWidth = 10, imageHeight = 12, directionVariation = 45, velocityVariation = 50, rotationVariation = 360, rotationChange = 30, useEmitterRotation = false, alphaStart = 1, fadeInSpeed = 1.5, fadeOutSpeed = .5, fadeOutDelay = 100, scaleStart = .5, scaleVariation = 1, scaleInSpeed = .5, emissionShape = 1, emissionRadius = 1, killOutsideScreen = true, lifeTime = 150, blendMode = "subtract", } ) Particles.AttachParticleType(emitterName , "PBulletSTrail", 32, 99999,0) local R = 153 local G = 153 local B = 153 Particles.SetParticleProperty("PBulletSTrail", "colorStart", {R,G,B}) local function attachBEmitters() if BEmitters then print( "Number of Emitters: " .. tostring( #BEmitters ) ) if #BEmitters~=0 then Particles.SetEmitterTarget( emitterName, bullet.body, true, 0, 0, 0 ) Particles.StartEmitter(emitterName) end end return Emitter end for i=1,1 do BEmitters[#BEmitters + 1] = attachBEmitters() end timer.performWithDelay(10, attachBEmitters) local onPlayerBulletCollision = function(self, event) local parentObject = PlayerBullets[self.\_index] if (event.phase == "began") then if (event.other.class == "EnemyBullet") then print("PlayerBulletCount decreased by 1") PlayerBulletCount = PlayerBulletCount - 1 EnemyBulletCount = EnemyBulletCount - 1 Particles.DeleteEmitter(event.target.emitterRef) Particles.DeleteEmitter(event.other.emitterRef) display.remove(self) display.remove(event.other) print(" Projectile Removed ") end elseif (event.phase == "ended") then PlayerBulletCount = PlayerBulletCount - 1 Particles.DeleteEmitter(event.target.emitterRef) display.remove(self) end return true end bullet.body.collision = onPlayerBulletCollision bullet.body:addEventListener("collision", bullet.body) bullet:insert( bullet.body ) bullet:insert( bullet.body.emitter ) camera:add(bullet, 2, false) table.insert(PlayerBullets, bullet) bullet.body.\_index = table.indexOf(PlayerBullets, bullet) bullet.body.emitter.\_index = bullet.body.\_index return bullet end
Maybe there is an error in my code somewhere that is causing the collisions to conflict.
-Saer
Edit: Hopefully you’ll be able to read the code fine… the way this forum formats code is atrocious.