How to shoot a gun

How do i shoot a gun when the user touches the screen . This is my code :

I also want to change where it says minigun.enterFrame because I will have more than one gun in the game so can I also get help with that too ? Thanks

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

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?

Also you should use composer not storyboard but that’ll just have to be solved later I’ll be back on this post in about 20-30 minutes i’m gonna write you an example :slight_smile: