I have a problem. I am trying to have the character create a target every half second than have 5 turrets simultaneously fire at that target. It would almost be as if the turrets are homing in on the character, but the turrets do not fire. This is my code:
main.lua:
require "ssk2.loadSSK" \_G.ssk.init( {} ) require "physics" physics.start( ) physics.setGravity(0,0) local turretM = require "turretM" local turrets = {} local character local background = display.newGroup() local content = display.newGroup() character = display.newCircle(100, 100, 20) physics.addBody(character, "dynamic", {isSensor = true}) local ox = fullw/5 for i = 1, 5 do local turret = ssk.display.newImageRect( content, left + (i-1) \* ox + ox/2, bottom - 75, "rg256.png", { size = 100, fill = \_W\_ } ) turrets[#turrets+1] = turret end local function activateTurret() local target = ssk.display.newCircle( content, character.x, character.y, { size = 40, fill = \_G\_}, { radius = 10 } ) target.timer = display.remove timer.performWithDelay(50, target) turretM.fire(turrets, target) end ssk.display.newRect( background, centerX, centerY, { w = fullw, h = fullh, fill = \_GREY\_ } ) timer.performWithDelay(500, activateTurret, 0)
turretM.lua:
local lifetime = 5000 local projectileCount = 3 local spread = 7 local bulletSpeed = 300 local turretM = {} local function onCollision( self, event ) if( event.other.myType == "bullet" ) then return false end display.remove(self) return false end function turretM.fire( turrets, target ) for i = 1, #turrets do local turret = turrets[i] local vec = ssk.math2d.diff( turret, target ) local angle0 = ssk.math2d.vector2Angle(vec) local angle = angle0 - spread \* (projectileCount-1)/2 for j = 1, projectileCount do local vec2 = ssk.math2d.angle2Vector( angle, true ) vec2 = ssk.math2d.normalize(vec2) vec2 = ssk.math2d.scale( vec2, bulletSpeed ) local bullet = ssk.display.newImageRect( target.group, turret.x, turret.y, "arrow.png", { size = 20, rotation = angle, collision = onCollision, myType = "bullet", fill = \_Y\_ }, { isSensor = true, radius = 10} ) bullet:toBack() bullet:setLinearVelocity( vec2.x, vec2.y ) bullet.timer = display.remove timer.performWithDelay( lifetime, bullet ) angle = angle + spread end end end return turretM
If the code is broken, I have a copy so I can always replace it.