How to shoot a gun

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:

[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.