Bullet not firing

I have a problem . When I click on the gun it is suppose to fire straight out like it’s coming from the gun , but it doesn’t . It stutters behind the gun . When I click the gun the bullet is suppose to come from a certain place that makes it look like it’s coming from the gun . 

 minigun = display.newImage("minigun2.png") minigun.x = 50 minigun.y = 300 screenGroup:insert(minigun)

local bullet = {} local bCounter = 1 local function bulletMove(self, event) if self.x == -95 then display.remove( bullet ) else self.x = self.x - 2 end return true end local function gunTouched( event ) bullet = display.newImageRect( "bullet3.png", 50, 25 ) bullet.x = minigun.x - 40 bullet.y = minigun.y - 25 bullet.enterFrame = bulletMove Runtime:addEventListener( "enterFrame", bullet ) end function scene:show( event ) local sceneGroup = self.view local phase = event.phase if phase == "will" then elseif phase == "did" then end minigun:addEventListener( "touch", gunTouched ) end function scene:hide( event ) local sceneGroup = self.view local phase = event.phase if event.phase == "will" then Runtime:removeEventListener( "enterFrame", bullet ) elseif phase == "did" then end end function scene:destroy( event ) local sceneGroup = self.view if minigun then minigun:removeSelf( ) minigun = nil end end

Couple of things, you have declared bullet as a table so you should be creating a table object like: bullet[bCounter] = …

However if you don’t need to access the bullet object again outside of the scope of the enterframe listener just create it locally in the gunTouched function like this:

 local function bulletMove(self, event) if self.x == -95 then --Remove the event listener Runtime:removeEventListener( "enterFrame", self) --Remove the object self:removeSelf() self = nil else self.x = self.x - 2 end return true end local function gunTouched( event ) --Swapped for circle as I dont have the image... local bullet = display.newCircle(sceneGroup, 0,0,10) bullet.x = minigun.x - 40 bullet.y = minigun.y - 25 bullet.enterFrame = bulletMove Runtime:addEventListener( "enterFrame", bullet ) end

Also, in your guntouched function you will be firing a bullet in each touch event (began/moved/ended) so I would change it to this:

local function gunTouched( event ) local phase = event.phase --Pick up the "began" phase of the touch if phase == "began" then --Swapped for circle as I dont have the image... local bullet = display.newCircle(sceneGroup, 0,0,10) bullet.x = minigun.x - 40 bullet.y = minigun.y - 25 bullet.enterFrame = bulletMove Runtime:addEventListener( "enterFrame", bullet ) end end

Same with your composer events too, make sure your logic sits within an event to stop it getting fired multiple times:

function scene:show( event ) local sceneGroup = self.view local phase = event.phase if phase == "will" then elseif phase == "did" then --Only happens on the "did" phase of scene creation minigun:addEventListener( "touch", gunTouched ) end end

Hope this helps and good luck :wink:

No the bullet is still behind the gun stuttering behind the gun

Then just change the x and y values of the bullet when it is created :slight_smile:

I tried that it didn’t help

So using this code:

local composer = require( "composer" ) local scene = composer.newScene() --Forward Decs local bullet = {} local minigun = nil local gunTouched = nil local bulletMove = nil function scene:create(event ) local sceneGroup = self.view function bulletMove(self, event) if self.x \< -95 then --Remove the event listener Runtime:removeEventListener( "enterFrame", self) --Remove the object self:removeSelf() self = nil else self.x = self.x - 2 end return true end function gunTouched( event ) local phase = event.phase if event.phase == "began" then local b = display.newCircle(sceneGroup, 0, 0, 5)--display.newImageRect(sceneGroup, "bullet3.png", 50, 25 ) b.x = minigun.x - 40 b.y = minigun.y - 25 b.enterFrame = bulletMove Runtime:addEventListener( "enterFrame", b ) --Insert into bullet table bullet[b] = b end end minigun = display.newRect(0,0,20,20)--display.newRect(sceneGroup,"minigun2.png") minigun.x = 500 minigun.y = 300 end function scene:show( event ) local sceneGroup = self.view local phase = event.phase if phase == "will" then elseif phase == "did" then minigun:addEventListener( "touch", gunTouched ) end end function scene:hide( event ) local sceneGroup = self.view local phase = event.phase if event.phase == "will" then for k,v in pairs(bullet) do Runtime:removeEventListener( "enterFrame", bullet[k] ) end elseif phase == "did" then end end function scene:destroy( event ) local sceneGroup = self.view end scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) return scene

can you get the bullet to start in the right place by adjusting the b.x and b.y values?

Nope . This function is shooting the bullets but they are together :

local bullet = {} local bCounter = 1 local function shootBullet(event) if event.phase == "ended" then bullet[bCounter] = display.newImage( "bullet3.png" , minigun.x, minigun.y, 6, 6 ) bullet[bCounter].value = bCounter physics.addBody( bullet[bCounter], "dynamic" ) bullet[bCounter].gravityScale = 0 bullet[bCounter].myName = "bullet" bullet[bCounter]:setLinearVelocity( 0, -200 ) bCounter = bCounter + 1 end end function scene:show(event) minigun:addEventListener( "touch", shootBullet ) end function scene:hide(event) end

This is totally different to the code you posted originally.  Its making it very difficult to help you.

The code I previously posted is a previous answer to my previous question . That code shoots the bullets but 4 at a time . I’ll just stick to the code you gave me .

This is my current code :

-- requires local physics = require "physics" physics.start() local composer = require( "composer" ) local scene = composer.newScene() -- background function scene:create(event) local screenGroup = self.view local background = display.newImageRect("images.png",display.contentWidth,display.contentHeight) background.x = display.contentCenterX background.y = display.contentCenterY screenGroup:insert(background) centerX = 230 centerY = 60 round = 1 roundTxt = display.newText( "Round: "..round, centerX, centerY, native.systemFontBold, 20 ) minigun = display.newImage("minigun2.png") minigun.x = 50 minigun.y = 300 screenGroup:insert(minigun) zombie = display.newImage("images3.png") zombie.x = 410 zombie.y = 296 zombie.speed = math.random(2,10) zombie.initY = zombie.y zombie.amp = math.random(20, 100) zombie.angle = math.random(1, 260) physics.addBody(zombie, "static", {density=.1, bounce=0.2, friction=.2, radius=12}) screenGroup:insert(zombie) end --Forward Decs local bullet = {} local minigun = nil local gunTouched = nil local bulletMove = nil function bulletMove(self, event) if self.x \< -95 then --Remove the event listener Runtime:removeEventListener( "enterFrame", self) --Remove the object self:removeSelf() self = nil else self.x = self.x - 2 end return true end function gunTouched( event ) local phase = event.phase if event.phase == "began" then local b = display.newCircle(sceneGroup, 0, 0, 5)--display.newImageRect(sceneGroup, "bullet3.png", 50, 25 ) b.x = minigun.x - 40 b.y = minigun.y - 25 b.enterFrame = bulletMove Runtime:addEventListener( "enterFrame", b ) --Insert into bullet table bullet[b] = b end end function scene:show( event ) local sceneGroup = self.view local phase = event.phase if phase == "will" then elseif phase == "did" then minigun:addEventListener( "touch", gunTouched ) end end function scene:hide( event ) local sceneGroup = self.view local phase = event.phase if event.phase == "will" then for k,v in pairs(bullet) do Runtime:removeEventListener( "enterFrame", bullet[k] ) end elseif phase == "did" then end end function scene:destroy( event ) local sceneGroup = self.view end scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) return scene

This is my current error :

game.lua:88: attempt to index upvalue 'minigun' (a nil value)

Hello ? I am still getting the error

Couple of things, you have declared bullet as a table so you should be creating a table object like: bullet[bCounter] = …

However if you don’t need to access the bullet object again outside of the scope of the enterframe listener just create it locally in the gunTouched function like this:

 local function bulletMove(self, event) if self.x == -95 then --Remove the event listener Runtime:removeEventListener( "enterFrame", self) --Remove the object self:removeSelf() self = nil else self.x = self.x - 2 end return true end local function gunTouched( event ) --Swapped for circle as I dont have the image... local bullet = display.newCircle(sceneGroup, 0,0,10) bullet.x = minigun.x - 40 bullet.y = minigun.y - 25 bullet.enterFrame = bulletMove Runtime:addEventListener( "enterFrame", bullet ) end

Also, in your guntouched function you will be firing a bullet in each touch event (began/moved/ended) so I would change it to this:

local function gunTouched( event ) local phase = event.phase --Pick up the "began" phase of the touch if phase == "began" then --Swapped for circle as I dont have the image... local bullet = display.newCircle(sceneGroup, 0,0,10) bullet.x = minigun.x - 40 bullet.y = minigun.y - 25 bullet.enterFrame = bulletMove Runtime:addEventListener( "enterFrame", bullet ) end end

Same with your composer events too, make sure your logic sits within an event to stop it getting fired multiple times:

function scene:show( event ) local sceneGroup = self.view local phase = event.phase if phase == "will" then elseif phase == "did" then --Only happens on the "did" phase of scene creation minigun:addEventListener( "touch", gunTouched ) end end

Hope this helps and good luck :wink:

No the bullet is still behind the gun stuttering behind the gun

Then just change the x and y values of the bullet when it is created :slight_smile:

I tried that it didn’t help

So using this code:

local composer = require( "composer" ) local scene = composer.newScene() --Forward Decs local bullet = {} local minigun = nil local gunTouched = nil local bulletMove = nil function scene:create(event ) local sceneGroup = self.view function bulletMove(self, event) if self.x \< -95 then --Remove the event listener Runtime:removeEventListener( "enterFrame", self) --Remove the object self:removeSelf() self = nil else self.x = self.x - 2 end return true end function gunTouched( event ) local phase = event.phase if event.phase == "began" then local b = display.newCircle(sceneGroup, 0, 0, 5)--display.newImageRect(sceneGroup, "bullet3.png", 50, 25 ) b.x = minigun.x - 40 b.y = minigun.y - 25 b.enterFrame = bulletMove Runtime:addEventListener( "enterFrame", b ) --Insert into bullet table bullet[b] = b end end minigun = display.newRect(0,0,20,20)--display.newRect(sceneGroup,"minigun2.png") minigun.x = 500 minigun.y = 300 end function scene:show( event ) local sceneGroup = self.view local phase = event.phase if phase == "will" then elseif phase == "did" then minigun:addEventListener( "touch", gunTouched ) end end function scene:hide( event ) local sceneGroup = self.view local phase = event.phase if event.phase == "will" then for k,v in pairs(bullet) do Runtime:removeEventListener( "enterFrame", bullet[k] ) end elseif phase == "did" then end end function scene:destroy( event ) local sceneGroup = self.view end scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) return scene

can you get the bullet to start in the right place by adjusting the b.x and b.y values?

Nope . This function is shooting the bullets but they are together :

local bullet = {} local bCounter = 1 local function shootBullet(event) if event.phase == "ended" then bullet[bCounter] = display.newImage( "bullet3.png" , minigun.x, minigun.y, 6, 6 ) bullet[bCounter].value = bCounter physics.addBody( bullet[bCounter], "dynamic" ) bullet[bCounter].gravityScale = 0 bullet[bCounter].myName = "bullet" bullet[bCounter]:setLinearVelocity( 0, -200 ) bCounter = bCounter + 1 end end function scene:show(event) minigun:addEventListener( "touch", shootBullet ) end function scene:hide(event) end

This is totally different to the code you posted originally.  Its making it very difficult to help you.

The code I previously posted is a previous answer to my previous question . That code shoots the bullets but 4 at a time . I’ll just stick to the code you gave me .

This is my current code :

-- requires local physics = require "physics" physics.start() local composer = require( "composer" ) local scene = composer.newScene() -- background function scene:create(event) local screenGroup = self.view local background = display.newImageRect("images.png",display.contentWidth,display.contentHeight) background.x = display.contentCenterX background.y = display.contentCenterY screenGroup:insert(background) centerX = 230 centerY = 60 round = 1 roundTxt = display.newText( "Round: "..round, centerX, centerY, native.systemFontBold, 20 ) minigun = display.newImage("minigun2.png") minigun.x = 50 minigun.y = 300 screenGroup:insert(minigun) zombie = display.newImage("images3.png") zombie.x = 410 zombie.y = 296 zombie.speed = math.random(2,10) zombie.initY = zombie.y zombie.amp = math.random(20, 100) zombie.angle = math.random(1, 260) physics.addBody(zombie, "static", {density=.1, bounce=0.2, friction=.2, radius=12}) screenGroup:insert(zombie) end --Forward Decs local bullet = {} local minigun = nil local gunTouched = nil local bulletMove = nil function bulletMove(self, event) if self.x \< -95 then --Remove the event listener Runtime:removeEventListener( "enterFrame", self) --Remove the object self:removeSelf() self = nil else self.x = self.x - 2 end return true end function gunTouched( event ) local phase = event.phase if event.phase == "began" then local b = display.newCircle(sceneGroup, 0, 0, 5)--display.newImageRect(sceneGroup, "bullet3.png", 50, 25 ) b.x = minigun.x - 40 b.y = minigun.y - 25 b.enterFrame = bulletMove Runtime:addEventListener( "enterFrame", b ) --Insert into bullet table bullet[b] = b end end function scene:show( event ) local sceneGroup = self.view local phase = event.phase if phase == "will" then elseif phase == "did" then minigun:addEventListener( "touch", gunTouched ) end end function scene:hide( event ) local sceneGroup = self.view local phase = event.phase if event.phase == "will" then for k,v in pairs(bullet) do Runtime:removeEventListener( "enterFrame", bullet[k] ) end elseif phase == "did" then end end function scene:destroy( event ) local sceneGroup = self.view end scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) return scene

This is my current error :

game.lua:88: attempt to index upvalue 'minigun' (a nil value)