Hello, I am having some trouble creating an explosion when a bullet collides with an object. The code for the explosion and the bullet and the bullet’s implementation are in separate files.
Here is the code:
--explosion.lua local M = {} function M.new(group, x, y, xScale, yScale, xOffset, yOffset) --local removeTimer = timer.performWithDelay(500, function() display.remove(explosion) end, 1) local explosionInfo = require "sprite.lua.explosion" local explosionSheet = graphics.newImageSheet("sprite/png/explosion.png", explosionInfo:getSheet()) local explosionData = {name = "explosion", start = explosionInfo:getFrameIndex("explosion1"), count = 8, time = 500, loopCount = 1} local explosion = display.newSprite(group, explosionSheet, explosionData) explosion.x, explosion.y = x + xOffset, y + yOffset --error line \<----------- explosion.xScale, explosion.yScale = xScale, yScale explosion:play() --explosion.timer = removeTimer end return M ------------------------------------------------------------------------------------------------------------------------------------- --bullet.lua local M = {} local explosion = require "other.explosion" function M.new(group, object) local bulletCollisionFilter = {categoryBits = 4, maskBits = 2} local function createBullet(x, y, xOffset, yOffset) local bullet = display.newImageRect(group, "sprite/ship/Bullet.png", 32, 33) physics.addBody(bullet, "dynamic", {filter = bulletCollisionFilter}) bullet.x, bullet.y = x, y bullet.damage = 25 bullet:toBack() bullet.xOffset, bullet.yOffset = xOffset, yOffset return bullet end object.bullets = {} object.bullets[1] = createBullet(object.x, object.y, -26, -20) object.bullets[2] = createBullet(object.x, object.y, 26, -20) local function collision1(self, event) if event and event.phase == "began" then explosion.new(group, self.x, self.y, 0.5, 0.5, math.random(-10, 10), math.random(-10, 10)) display.remove(self) end end for i = 1, #object.bullets do local bullet = object.bullets[i] local vx, vy = object:getLinearVelocity() local vec1 = ssk.math2d.angle2Vector( object.rotation + 180, true) vec1 = ssk.math2d.normalize( vec1 ) vec1 = ssk.math2d.scale( vec1, bullet.yOffset ) vec1 = ssk.math2d.add( vec1, object ) local vec2 = ssk.math2d.angle2Vector( object.rotation + 90, true ) vec2 = ssk.math2d.normalize( vec2 ) vec2 = ssk.math2d.scale( vec2, bullet.xOffset ) bullet.rotation = object.rotation - 90 bullet.x = vec1.x + vec2.x bullet.y = vec1.y + vec2.y bullet:setLinearVelocity(vx \* 4, vy \* 4) local removeTimer = timer.performWithDelay(5000, function() display.remove(bullet) end, 1) bullet.timer = removeTimer bullet.collision = collision1 listen("collision", bullet) end end return M ------------------------------------------------------------------------------------------------------------------------------------- --ship.lua local function shoot() local vx, vy = ship:getLinearVelocity() if (vx \< -100 or vx \> 100) or (vy \< -100 or vy \> 100) then bullet.new(group, ship) end end local shootTimer = timer.performWithDelay(100, shoot, 0)
Right now, about a second after I fire the first bullet, all bullets on screen simultaneously explode and I get an “Attempt to perform arithmetic on local x (a nil value)” error. The error line is in explosion.lua and I marked it.
Any help would be great, thank you!