Shooting

How would I shoot a bullet (Bullet.png) from my “Bear” object when I touch the background? Currently When I hold down on the background it prints “Shooting” every 100 milliseconds. I need to make it actually shoot the bullet from the front of my bear object. But my bear object rotates as you will see from my code below. SO I need the bullet to shoot from the front, and shoot from the correct rotation. This link ( http://youtu.be/p8cBuvScY_Q ) is from a previous question I asked that has been solved, but it should show what I mean by rotation. For some reason when I pasted the code below there is not the correct spaces, but you get the idea.
Current code:[lua]----------------------------------------------------------------------------------

– Invasion Play.lua


local storyboard = require( “storyboard” )
local scene = storyboard.newScene()



– NOTE:

– Code outside of listener functions (below) will only be executed once,
– unless storyboard.removeScene() is called.



– BEGINNING OF YOUR IMPLEMENTATION

local bear
local leftButton
local rightButton
local back
local pressBack
local shootButton
local background
– Called when the scene’s view does not exist:
function scene:createScene( event )
local group = self.view

– CREATE display objects and add them to ‘group’ here.

background = display.newImage( “invasionBackground.png” )
background.x = display.contentWidth - 240
background.y = display.contentHeight - 160
background:scale(1,1)

bear = display.newImage( “polarbear.png”, 130, 60 )
bear:scale(0.30,0.30)
bear.rotation = rotation

leftButton = display.newImageRect( “rotateLeft.png”, 103, 50 )
leftButton.x = display.contentWidth - 465
leftButton.y = display.contentHeight - 30

rightButton = display.newImageRect( “rotateRight.png”, 103, 50 )
rightButton.x = display.contentWidth - 360
rightButton.y = display.contentHeight - 30

back = display.newImageRect( “back.png”, 50, 50 )
back.x = display.contentWidth - 490
back.y = display.contentHeight - 290

group:insert(background)
group:insert(bear)
group:insert(leftButton)
group:insert(rightButton)
group:insert(back)
end
– Called immediately after scene has moved onscreen:
function scene:enterScene( event )
local group = self.view

– INSERT code here (e.g. start timers, load audio, start listeners, etc.)

local function tapBack( event )
storyboard.gotoScene( “Menu”, “zoomOutInFade”, 240 )
print( “Back Button Tapped” )
end
local function rotateRight()
bear.rotation = bear.rotation + 6
end
local function rotateLeft()
bear.rotation = bear.rotation - 6
end

local function pressLeft(event)
if event.phase == “began” then
leftTimer = timer.performWithDelay(1, rotateLeft, 0)
elseif event.phase == “ended” then
timer.cancel(leftTimer)
bear.rotation = bear.rotation
end
end
local function pressRight(event)
if event.phase == “began” then
rightTimer = timer.performWithDelay(1, rotateRight, 0)
elseif event.phase == “ended” then
timer.cancel(rightTimer)
bear.rotation = bear.rotation
end
end
local function shoot(event)
print( “Shooting” )
end
local function pressShoot(event)
if event.phase == “began” then
shootTimer = timer.performWithDelay(100, shoot, 0)
elseif event.phase == “ended” then
timer.cancel(shootTimer)
print( “Not shooting” )
end
end

background:addEventListener( “touch”, pressShoot )
back:addEventListener( “tap”, tapBack )
leftButton:addEventListener( “touch”, pressLeft )
rightButton:addEventListener( “touch”, pressRight )
end
– Called when scene is about to move offscreen:
function scene:exitScene( event )
local group = self.view

– INSERT code here (e.g. stop timers, remove listeners, unload sounds, etc.)

bear.rotation = 0
storyboard.purgeScene( “InvasionPlay” )
end
– Called prior to the removal of scene’s “view” (display group)
function scene:destroyScene( event )
local group = self.view


– INSERT code here (e.g. remove listeners, widgets, save state, etc.)


end

– END OF YOUR IMPLEMENTATION

– “createScene” event is dispatched if scene’s view does not exist
scene:addEventListener( “createScene”, scene )

– “enterScene” event is dispatched whenever scene transition has finished
scene:addEventListener( “enterScene”, scene )

– “exitScene” event is dispatched before next scene’s transition begins
scene:addEventListener( “exitScene”, scene )

– “destroyScene” event is dispatched before view is unloaded, which can be
– automatically unloaded in low memory situations, or explicitly via a call to
– storyboard.purgeScene() or storyboard.removeScene().
scene:addEventListener( “destroyScene”, scene )


return scene [import]uid: 123066 topic_id: 22808 reply_id: 322808[/import]

Hey there, this thread is about turrets but applies to your “Bear” as well :slight_smile:

http://developer.anscamobile.com/forum/2011/04/20/how-rotate-turret-dragging

Really handy stuff, take a look. [import]uid: 52491 topic_id: 22808 reply_id: 91054[/import]

hi, if you know AS3 (which is not that different from corona/LUA) I recommend to look at the Advanced Weapon System class by FAS http://www.freeactionscript.com/2011/08/advanced-weapon-system/

-finefin [import]uid: 70635 topic_id: 22808 reply_id: 91096[/import]

I dont know any programming language. I only did the beginners course in the ncss challenge (python) which isnt really useful for this. Corona is my first attempt at “actual” programming. [import]uid: 123066 topic_id: 22808 reply_id: 91149[/import]

Here’s a simple function for shooting a projectile.

Just change the names to suit and call “shoot()” when the screen is pressed. The code presumes you’re using physics.

If you get stuck, just reply and I’ll explain/help as best I can.

local function shoot (event)  
 local arrow = display.newRect(0, 0, 5, 20)   
 arrow.x = player1.x + player1.contentWidth / 3  
 arrow.y = player1.y + player1.contentHeight / 2 + 3  
 arrow:setFillColor(255, 0, 0)  
 physics.addBody(arrow, "kinematic", {density = 10.0, friction = 0, bounce = 0})  
 arrow:setLinearVelocity( 0, 250)  
 arrow.myName = "arrow"  
 player:insert(arrow)  
   
 arrow.collision = onLocalCollision  
 arrow:addEventListener( "collision", arrow )  
end  

EDIT: It’d help if I had actually read your post about the rotating bear…

I’ll try and come up with something. [import]uid: 67933 topic_id: 22808 reply_id: 91167[/import]

The link to my rotating bear thread is here: http://developer.anscamobile.com/forum/2012/03/03/hold-down-button

I am having problems with what you told me to copy, when I press my shoot button it says that the velocity is a nil value? [import]uid: 123066 topic_id: 22808 reply_id: 91227[/import]

hey Spider, I really liked your snippet and was able to implement it in mine. It works flawlessly, except for one small issue. When my projectile is fired, everything is normal. However, when I press my button listener while the first projectile is in motion, it removes the first projectile and fires a second projectile. Does anyone know how to remedy this? I’ve tried concatenation but apparently can’t get the hang of it. Any help would be appreciated! Here’s my code:

[lua]–This spawns the projectile for use
local onShotSpawn = function(object)
for a = 1, 5, 1 do
bolt = display.newRect(0, 0, 15, 5)
bolt.name = (“bolt”… a)
bolt.id = a
bolt.x = 800
bolt.y = 600
bolt:setFillColor(255, 0, 0)
physics.addBody(bolt, “kinematic”, {density = 10.0, friction = 0, bounce = 0})
bolt.myName = “bolt”
levelGroup:insert(bolt)
end
end[/lua]

[lua]–Fire projectile!
local function fireRifle (event)
for a = 1, 1 do
if event.phase == “began” then
bolt.x = player.x + 30
bolt.y = player.y - 15
bolt:setLinearVelocity( 350, 0)
print(“FIRE!”)

end
if player.direction == DIRECTION_LEFT then
if event.phase == “began” then
bolt.x = player.x - 30
bolt.y = player.y - 15
bolt:setLinearVelocity( -350, 0)
print(“FIRE!”)
end

end
end
end

buttonB:addEventListener(“touch”, fireRifle)[/lua] [import]uid: 135394 topic_id: 22808 reply_id: 117063[/import]