This is my code. I wan to move balloon to the cage whenever there is a tap event occurs on the cage. A mode of attack am trying to do. Help!
[lua]-----------------------------------------------------------------------------------------
– menu.lua
local composer = require( “composer” )
local scene = composer.newScene()
– include Corona’s “widget” library
local widget = require “widget”
display.setStatusBar(display.HiddenStatusBar)
centerX = display.contentWidth * .5
centerY = display.contentHeight * .5
local physics = require( “physics” )
physics.start()
physics.setGravity( 0, 1 )
–physics.setDrawMode( “normal” ) – The default Corona renderer (no collision outlines)
–physics.setDrawMode( “hybrid” ) – Overlays collision outlines on normal display objects
–physics.setDrawMode( “debug” ) – Shows collision engine outlines only
local currLevel = 0
– forward declarations and other locals
local balloon
local backBtn
local count = 0
local pop = audio.loadSound( “music/pop.wav”)
local objectSheet = require(“balloonSprite”)
local myObjectSheet = graphics.newImageSheet(“images/characters/balloonSprite.png”, objectSheet:getSheet())
– ‘onRelease’ event listener for playBtn
local function onBtnRelease(btn)
local action = btn.target.id
if action == “back” then
composer.gotoScene( “menu”, “fade”, 500 )
else
composer.gotoScene( action, “fade”, 500 )
end
return true – indicates successful touch
end
local function hitAttack(event)
local location = event.target
if event.phase == “began” then
print(“cage touched”)
elseif event.phase == “ended” then
print(“cage release”)
transition.to (balloon, {time = 500, x = location.x, y = location.y, transition = easing.inOutQuad})
end
end
local function balloonTouched(event)
local balloon = event.target
if event.phase==“ended” then
if event.x == balloon.x then
balloon:applyLinearImpulse( 0, -0.75, balloon.x, balloon.y )
elseif event.x < balloon.x then
balloon:applyLinearImpulse( 0.05, -0.75, balloon.x, balloon.y )
elseif event.x > balloon.x then
balloon:applyLinearImpulse( -0.05, -0.75, balloon.x, balloon.y )
end
end
return true
end
local function onBalloonCollision(event)
local balloon = event.other
local function removeBalloon()
balloon:removeSelf()
–physics.removeBody( balloon )
– local balloon = event.target – “event.target” references the sprite
– if ( event.phase == “ended” ) then
– --balloon:removeSelf() – play the new sequence
– end
end
if event.phase == “began” then
print(“began”)
if ((event.target.myName == “thorn”) and (event.other.myName == “balloon”))
or ((event.target.myName == “thorn”) and (event.other.myName == “blueballoon”))
or ((event.target.myName == “thorn”) and (event.other.myName == “greenballoon”)) then
balloon:setSequence(“burst”)
balloon:play()
balloon:addEventListener( “sprite”, (timer.performWithDelay( 200, removeBalloon)) )
audio.play(pop)
end
end
return true
end
local function cageRemove()
if sec == 0 then
cage:removeSelf()
end
end
function scene:create( event )
local sceneGroup = self.view
– Called when the scene’s view does not exist.
– INSERT code here to initialize the scene
– e.g. add display objects to ‘sceneGroup’, add touch listeners, etc.
– display a background image
local recttop = display.newRect( display.contentWidth/2, -50, display.contentWidth, 1 )
recttop:setFillColor( 1 )
local rectleft = display.newRect( -50, display.contentHeight/2, 1, display.contentHeight + 50 )
rectleft:setFillColor( 1 )
local rectright = display.newRect( display.contentWidth + 50, display.contentHeight/2, 1, display.contentHeight + 50 )
rectright:setFillColor( 1 )
physics.addBody(recttop, “static”)
physics.addBody(rectleft, “static”)
physics.addBody(rectright, “static”)
sceneGroup:insert(recttop, rectleft, rectright)
local background = display.newImageRect( “images/menu/background.png”, display.actualContentWidth, display.actualContentHeight )
background.anchorX = 0
background.anchorY = 0
background.x = 0 + display.screenOriginX
background.y = 0 + display.screenOriginY
sceneGroup:insert( background )
local thorn = display.newImageRect( “images/characters/thorn.png”, (display.contentWidth - 20), 50 )
thorn.x = display.contentWidth * .5
thorn.y = display.contentHeight - 25
thorn.myName = “thorn”
physics.addBody(thorn, “static”)
– create a widget button (which will loads level1.lua on release)
backBtn = widget.newButton{
label=“Back”,
id = “back”,
labelColor = { default={255}, over={128} },
fontSize = 50,
emboss = true,
default=“button.png”,
over=“button-over.png”,
width=200, height=60,
onRelease = onBtnRelease – event listener function
}
backBtn.x = 80
backBtn.y = 40
– all display objects must be inserted into group
sceneGroup:insert( background )
sceneGroup:insert( backBtn )
sceneGroup:insert( thorn )
local balloon = display.newSprite(myObjectSheet, objectSheet:getSequenceData())
–balloon:scale(2,2)
balloon.x = centerX
balloon.y = centerY
balloon:setSequence(“still”)
balloon:play()
physics.addBody( balloon, “dynamic”, { radius=50, bounce=0.3, density = 0.005 } )
balloon:addEventListener(“touch”, balloonTouched)
balloon:addEventListener( “collision”, onBalloonCollision)
thorn:addEventListener(“collision”, onBalloonCollision)
balloon.myName = “balloon” --creating a name for the balloon
sceneGroup:insert(balloon)
local red = display.newImageRect( “images/characters/balloon.png”, 112, 112 )
red.x = centerX
red.y = centerY
red.myName = “red”
physics.addBody(red, “static”)
–red:addEventListener(“tap”, hitAttack)
local cage = display.newImageRect( “images/characters/cage.png”, 120, 120 )
cage.x = centerX
cage.y = centerY
cage.myName = “cage”
physics.addBody(cage, “static”)
cage:addEventListener( “tap”, hitAttack )
local sec = 10
local timeDisplay = display.newText( sec,0,0,native.systemFrontBold,64)
timeDisplay.x = display.contentCenterX
timeDisplay.y = 100
local function updateTimerSec(event)
if sec == 0 then
currLevel = currLevel + 1
–composer.gotoScene( “menu”, “fade”, 500 )
cageRemove()
–popMessage()
end
sec = sec - 1
timeDisplay.text = ( sec )
–createBalloon(balloonCount) --end
end
timer.performWithDelay(1000, updateTimerSec, 10)
sceneGroup:insert(timeDisplay)
sceneGroup:insert(red)
sceneGroup:insert(cage)
end
function scene:show( event )
local sceneGroup = self.view
local phase = event.phase
if phase == “will” then
– Called when the scene is still off screen and is about to move on screen
elseif phase == “did” then
– Called when the scene is now on screen
– INSERT code here to make the scene come alive
– e.g. start timers, begin animation, play audio, etc.
end
end
function scene:hide( event )
local sceneGroup = self.view
local phase = event.phase
if event.phase == “will” then
– Called when the scene is on screen and is about to move off screen
– INSERT code here to pause the scene
– e.g. stop timers, stop animation, unload sounds, etc.)
elseif phase == “did” then
– Called when the scene is now off screen
end
end
function scene:destroy( event )
local sceneGroup = self.view
– Called prior to the removal of scene’s “view” (sceneGroup)
– INSERT code here to cleanup the scene
– e.g. remove display objects, remove touch listeners, save state, etc.
–[[
if playBtn then
playBtn:removeSelf() – widgets must be manually removed
playBtn = nil
end]]
end
– Listener setup
scene:addEventListener( “create”, scene )
scene:addEventListener( “show”, scene )
scene:addEventListener( “hide”, scene )
scene:addEventListener( “destroy”, scene )
return scene
[/lua]