I am having trouble creating a table for an object and having them all perform the same function. I have two files, turretM.lua and main.lua. On turretM.lua, I have the code for firing a turret, on main.lua, I create my table of turrets, I am having trouble trying to get all of the turrets to fire at once. Right now, I am getting this error:
Attempt to index field '?' (a nil value) File: ssk2/math2d.lua Line: 110
This is from a result of the function trying to make this work with the table. So, how could I get this to work?
turretM.lua:
local lifetime = 5000 local projectileCount = 3 local spread = 7 local bulletSpeed = 300 local turretM = {} local function onCollision2( self, event ) if( event.other.myType == "bullet" ) then return false end display.remove(self) return false end function turretM.fire() local vec = ssk.math2d.diff( turret, target ) --Most likely the line causing the error local angle0 = ssk.math2d.vector2Angle(vec) local angle = angle0 - spread \* (projectileCount-1)/2 for i = 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 = onCollision2, 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 return turretM
main.lua:
require "ssk2.loadSSK" \_G.ssk.init( {} ) require "physics" physics.start( ) physics.setGravity(0,0) local turretM = require "turretM" local turrets = {} local turret local currentTurret local background = display.newGroup() local content = display.newGroup() local function onCollision( self, event ) display.remove(self) return false end local function onTouch( self, event ) if( event.phase ~= "began" ) then return end local target = ssk.display.newImageRect( content, event.x, event.y, "corona256.png", { size = 40, collision = onCollision }, { radius = 20 } ) turretM.fire() end ssk.display.newRect( background, centerX, centerY, { w = fullw, h = fullh, fill = \_G\_, alpha = 0.2, touch = onTouch } ) --[[local function onDropped( self ) for k,v in pairs( turrets ) do v:setFillColor( unpack(\_P\_) ) end self:setFillColor( unpack(\_G\_) ) currentTurret = self end]] for a = 1, 2 do turret = ssk.display.newImageRect( content, (centerX - 100) + (a \* 200), bottom - 75, "rg256.png", { size = 100 } ) ssk.misc.addSmartDrag( turret, { retval = true } ) turrets[#turrets + 1] = turret end
Note: This will not work if you do not have SSK2 Lite or Pro, and I will not provide SSK2, as it is a payed module.