How to shoot a gun

[lua]

local composer = require( “composer” )

local scene = composer.newScene()

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( “bullet.png”, 50, 15 )

    bullet.x = gunGun.x - 40

    bullet.y = gunGun.y - 25

    bullet.enterFrame = bulletMove

    Runtime:addEventListener( “enterFrame”, bullet )

end

function scene:create( event )

    local sceneGroup = self.view

    gunGun = display.newImageRect( “LCP.png”, 100, 72)

    gunGun.x = display.contentCenterX

    gunGun.y = display.contentCenterY

    gunGun:addEventListener( “touch”, gunTouched )

    sceneGroup:insert( gunGun )

end

function scene:show( event )

    local sceneGroup = self.view

    local phase = event.phase

    if phase == “will” then

    elseif phase == “did” then

    end

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 gunGun then

         gunGun:removeSelf( )

         gunGun = nil

    end

end


– Listener setup

scene:addEventListener( “create”, scene )

scene:addEventListener( “show”, scene )

scene:addEventListener( “hide”, scene )

scene:addEventListener( “destroy”, scene )


return scene

[/lua]

I wrote this using composer not storyboard so the scene:create would be the same as scene:enterScene you have and so on with the scene:show, scene:hide, scene:destroy. Also I wrote this in about 30 minutes so its not pretty at all no background nothing just the basis of functionality you want. It creates a gun object in the center of the screen when its touched a bullet appears and moves until its off screen

Create a new blank project and paste my above code into the main.lua 

Now the things you will have to change for my code to work the png images you will have to swap with your own images so change “LCP.png” to the name of your gun image and “bullet.png” to the name of your projectile image and put in the correct dimensions for them. Also you will have to put those images in the new projects folder

Well I’m going out to eat so I’ll probably check back here tomorrow when I wake up if you have any questions @brandont264 just post them or send me a private message I’ll answer to the best of my abilities and we’ll get it working… just don’t let this discourage you from coding keep at it we’re here to help you.

Including Lua Code

To format your code for inclusion in a post, please wrap it in [lua][/lua] tags and we’ll make it look all pretty for you.

Your code

[lua]

function activateGun( self, event )

    self:shootGun()

end

 

function touchScreen( event )

    

    if event.phase == “began” then

      minigun.enterFrame = activateGun

      Runtime: addEventListener(“enterFrame”, minigun)

    end

    if event.phase == “ended” then

      Runtime: removeEventListener(“enterFrame”, minigun)

 

    end

end

[/lua]

 

Also your question is very broad “how do I shoot a gun” with some information missing that is needed to answer it. Do you want the gun object to appear where you touch and then fire? or do you have a character that you touch and he fires the gun when touched?..

No it is already thwere but when the user touches the screen i want the gun to fire .

Where do you want the player to shoot the gun? Just in a straight line? Is it like a pistol or a machine gun?

–SonicX278

If you want it to fire whenever the user touches anywhere on the screen add this line. This is assuming that the touchScreen function makes the animation for the bullet/projectile/ray gun  :D  happen.

[lua]Runtime:addEventListener(“touch”, touchScreen)[/lua]

Also this will make it so that any touch on the screen whether its an object or button being touched will activate the shootGun function so “beware”

if you want it to fire only when the gun is touched then

[lua]gunObjectName:addEventListener(“touch”, touchScreen)[/lua]

Nothing happens when I touch the screen .

This is what I put 

function touchGun( event ) if event.phase == "began" then Gun.enterFrame = activateGun gunObjectName:addEventListener("touch", touchScreen) end if event.phase == "ended" then gunObjectName:addEventListener("touch", touchScreen) end end function activateGun( self, event ) gunGun:addEventListener("touch", touchScreen) end function scene:enterScene(event) end function scene:exitScene(event) end
function touchGun( event ) if event.phase == "began" then Gun.enterFrame = activateGun gunObjectName:addEventListener("touch", touchScreen) end if event.phase == "ended" then gunObjectName:addEventListener("touch", touchScreen) end end function activateGun( self, event ) gunGun:addEventListener("touch", touchScreen) bullet = display.newImage("bullet1.jpg") bullet.x = 50 bullet.y = 300 screenGroup:insert(bullet) end function scene:enterScene(event) end function scene:exitScene(event) end

This is an update .

Ok, I’m having some trouble figuring out what you want. 

1 - Do you want the player to shoot 1 bullet at a time or many?

2 - Do you want the player to shoot when it gets touched? Or when anything gets touched? Or a button?

3 - Where does the player shoot the bullet(s)?

4 - How are the bullets being spawned? Do you have a function for that?

–SonicX278

  1. There is not player just the gun . I want it to shoot differently for different guns . For example : pistol one at a time and a machine gun many .

  2. I want the gun to shoot when the player touches the gun . 

  3. I want the bullets to shoot from the gun . So when they touch the gun the bullet looks like it’s coming from the gun

  4. They will appear from the gun . No I don’t have a function for that .

This is a sample. Put in in a main.lua and it will work. When you open it then press in the red box(The Gun).

local physics = require("physics") physics.start() local bullet = {} local bCounter = 1 local gun = display.newRect( display.contentCenterX, display.contentCenterY + 200, 40, 40 ) gun:setFillColor( 1, 0, 0 ) local function shootBullet(event) if event.phase == "ended" then bullet[bCounter] = display.newRect( gun.x, gun.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 gun:addEventListener( "touch", shootBullet )

–SonicX278

It doesn’t work

It works.

–SonicX278

I just created a sample project and put that code In the main.luag file and when I run it in the simulator it stays black

If it doesn’t work you have to tell us whats not working and what errors there are. I feel like you come to the forums for people to write your code for you. The code you gave us before will help us in no way to help you. We need information. The forums are a place for you to get help with code not to get people to write you code --RoamingGamer said this before–. If the simulator stayed black then you are prob using the landscape sim? I made the sample in a portrait.

–SonicX278 

Ok it works . So do I use this code as an example ?

Yes. Referring to my code. What do you want to happen?

–SonicX278

When the user touches the gun , it shoots the bullets

That’s exactly what is happening in that example…

–SonicX278

I did not literally think you were going to replace your code with my line of code. Of course it didn’t work where you put “gunObjectName” should be the name of the object that is your gun on the screen not literally put “gunObjectName” also you’re adding the listener twice when the touch begins and when it ends thats probably another error…  :mellow: … I don’t usually do this but… 

I will write the code for you but I will need the name of the “gunObject” is it gunGun?