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]
