Transition.to is not working properly

Hello…

I have this function

local function explosion()     if hasCollidedCircle( obj1, obj2 ) then     -- do the stuff when they are touching                  obj1.isVisible = false         transition.cancel()         obj1:pause()         createExplosion()         transition.to(bomb, {time = 1000, delay = 100, alpha = 0})         Runtime:removeEventListener("enterFrame", explosion )         Runtime:removeEventListener("touch", screenTouched)     else         obj1.alpha = 1     end          if hasCollidedCircle( obj1, obj3 ) then     -- do the stuff when they are touching                  obj1.isVisible = false         transition.cancel()         obj1:pause()         createExplosion()         transition.to(bomb, {time = 1000, delay = 100, alpha = 0})         Runtime:removeEventListener("enterFrame", explosion )         Runtime:removeEventListener("touch", screenTouched)     else         obj1.alpha = 1     end end Runtime:addEventListener("enterFrame", explosion )

When obj1 and obj2 touch

transition.to ---- alpha = 0 is not working

I can see the sprite of the explosion

but when obj1 and obj3 touch

transition.to ---- alpha = 0 works fine!

But I think both of them are exactly the same

what or where can I find the problem?

Thanks

Victor

Can you make a sample project of your problem? 

–SonicX278

Yes… here is the entire code…

I have the sprite explosion – “bomb”

is just a sprite with a explosion spritesheet

in one function the transition works fine

in the other it doesn’t.

It supposed to go alpha = 0, to make the sprite invisible after playing.

see the complete code. Thanks

-- // HOW TO MAKE AN APP 2016 -- // By Victor M. Barba -- // Copyright © 2016 by Victor M. Barba -- // All Rights Reserved -------------------------------------------------- local composer = require( "composer" ) local scene = composer.newScene() local sequenceTroth local trothImage local here local obj1 local obj2 local bomb local speed = 2500 local speed3 = 2300 local wait = 1500 local counter = 0 local timerBullet -- ------------------------------------------------------------------------------ -- "scene:create()" function scene:create(event) local sceneGroup = self.view &nbsp;&nbsp; &nbsp;local R = math.random(1, 99) &nbsp;&nbsp; &nbsp;local G = math.random(1, 99) &nbsp;&nbsp; &nbsp;local B = math.random(1, 99) &nbsp;&nbsp; &nbsp;local bgRectangle = display.newRect(sceneGroup, 0, 0, display.contentWidth, display.contentHeight) &nbsp;&nbsp;&nbsp; bgRectangle.x = display.contentCenterX &nbsp;&nbsp;&nbsp; bgRectangle.y = display.contentCenterY &nbsp;&nbsp; &nbsp;bgRectangle:setFillColor((R/100), (G/100), (B/100)) &nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; local buttonBack = display.newImageRect(sceneGroup, "images/backButton.png", 150, 150) &nbsp;&nbsp;&nbsp; buttonBack.x = 100 &nbsp;&nbsp;&nbsp; buttonBack.y = 60 &nbsp;&nbsp;&nbsp; local function buttonBackListener() &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;Runtime:removeEventListener("enterFrame", explosion ) &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;--Runtime:removeEventListener("touch", screenTouched) &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;timer.cancel( timerBullet ) &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;composer.gotoScene("menu3") &nbsp;&nbsp;&nbsp; end &nbsp;&nbsp;&nbsp; buttonBack:addEventListener("tap", buttonBackListener) &nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; local codeButton = display.newImageRect(sceneGroup, "images/seeButton.png", 150, 150) &nbsp;&nbsp;&nbsp; codeButton.x = display.contentWidth - 100 &nbsp;&nbsp;&nbsp; codeButton.y = 100 &nbsp;&nbsp;&nbsp; local function codeButtonListener() &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;Runtime:removeEventListener("enterFrame", explosion ) &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;Runtime:removeEventListener("touch", screenTouched) &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;timer.cancel( timerBullet ) &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;composer.gotoScene("programApp16") &nbsp;&nbsp;&nbsp; end &nbsp;&nbsp;&nbsp; codeButton:addEventListener("tap", codeButtonListener) &nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; sequenceTroth = { &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;{ name="smile", start=1, count=12, time=6000 }, &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;{ name="surprise", start=5, count=2, time=1000 }, &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;{ name="sad", start=2, count=6, time=1600 }, &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;{ name="happy", start=1, count=4, time=2000 } &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;} &nbsp;&nbsp; &nbsp;trothImage = graphics.newImageSheet( "images/spriteTroth.png", { width=200, height=200, numFrames=12 } ) &nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; function createTroth() &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;obj1 = display.newSprite( trothImage, sequenceTroth ) &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;sceneGroup:insert(obj1) &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;obj1.x = display.contentCenterX &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;obj1.y = display.contentHeight - 60 &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;obj1:scale(.6, .6) &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;obj1.setSequence = "smile" &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;obj1:play() &nbsp;&nbsp; &nbsp;end &nbsp;&nbsp; &nbsp;createTroth() &nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; sequenceExplosion = { &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;{ name="boom", start=1, count=48, time=1000, loopCount=1 } &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;} &nbsp;&nbsp; &nbsp;explosionImage = graphics.newImageSheet( "images/explosionSpritesheet.png", { width=400, height=400, numFrames=48 } ) &nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; function createExplosion() &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;bomb = display.newSprite( explosionImage, sequenceExplosion ) &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;sceneGroup:insert(bomb) &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;bomb.x = obj1.x &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;bomb.y = obj1.y &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;bomb:scale(.6, .6) &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;bomb.setSequence = "boom" &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;bomb:play() &nbsp;&nbsp; &nbsp;end end -- ------------------------------------------------------------------------------ -- "scene:show()" function scene:show( event ) local sceneGroup = self.view local phase = event.phase &nbsp;&nbsp;&nbsp; if (phase == "will") then---------------------------------------------------- &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; elseif (phase == "did") then---------------------------------------------------- &nbsp;&nbsp;&nbsp; local floor = display.newRect(sceneGroup, 0, 0, display.contentWidth, 20) &nbsp;&nbsp;&nbsp; floor.x = display.contentCenterX &nbsp;&nbsp;&nbsp; floor.y = display.contentHeight &nbsp;&nbsp; &nbsp;floor:setFillColor(.97, .92, .10) &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;local leftWall = display.newRect(sceneGroup, 0, 0, 20, display.contentHeight) &nbsp;&nbsp;&nbsp; leftWall.x = 0 &nbsp;&nbsp;&nbsp; leftWall.y = display.contentCenterY &nbsp;&nbsp; &nbsp;leftWall:setFillColor(.97, .92, .10) &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;local rightWall = display.newRect(sceneGroup, 0, 0, 20, display.contentHeight) &nbsp;&nbsp;&nbsp; rightWall.x = display.contentWidth &nbsp;&nbsp;&nbsp; rightWall.y = display.contentCenterY &nbsp;&nbsp; &nbsp;rightWall:setFillColor(.97, .92, .10) &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;obj2 = display.newImageRect(sceneGroup, "images/bullet.png", 200, 300) &nbsp;&nbsp; &nbsp;obj2:scale(.4, .4) &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;obj3 = display.newImageRect(sceneGroup, "images/bullet.png", 200, 300) &nbsp;&nbsp; &nbsp;obj3:scale(.4, .4) &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;local function resetBullets() &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;obj2.x = math.random(10, 1400) &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;obj2.y = -100 &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;obj3.x = math.random(10, 1400) &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;obj3.y = -100 &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;counter = counter + 1 &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;if counter == 20 then &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; Runtime:removeEventListener("enterFrame", explosion ) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;Runtime:removeEventListener("touch", screenTouched) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;timer.cancel( timerBullet ) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;local topText = display.newText( sceneGroup, "You did it, Next Level!", 100, 200, native.systemFont, 60 ) &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;topText.x = display.contentCenterX &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;topText.y = display.contentCenterY &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;topText:setFillColor( 0, 0, 0 ) &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;end &nbsp;&nbsp; &nbsp;end &nbsp;&nbsp; &nbsp;resetBullets() &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;local function sendBullet() &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;transition.to(obj2, {time = speed, delay = wait, y = 1000, onComplete = resetBullets}) &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;transition.to(obj3, {time = speed3, delay = wait, y = 1000, onComplete = resetBullets}) &nbsp;&nbsp; &nbsp;end &nbsp;&nbsp; &nbsp;timerBullet = timer.performWithDelay(3000, sendBullet, 10) &nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; -- Circle-based collision detection local function hasCollidedCircle( obj1, obj2 ) &nbsp;&nbsp;&nbsp; if ( obj1 == nil ) then&nbsp; -- Make sure the first object exists &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return false &nbsp;&nbsp;&nbsp; end &nbsp;&nbsp;&nbsp; if ( obj2 == nil ) then&nbsp; -- Make sure the other object exists &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return false &nbsp;&nbsp;&nbsp; end &nbsp;&nbsp;&nbsp; if ( obj3 == nil ) then&nbsp; -- Make sure the other object exists &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return false &nbsp;&nbsp;&nbsp; end &nbsp;&nbsp;&nbsp; local dx = obj1.x - obj2.x &nbsp;&nbsp;&nbsp; local dy = obj1.y - obj2.y &nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; local dx3 = obj1.x - obj3.x &nbsp;&nbsp; &nbsp;local dy3 = obj1.y - obj3.y &nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; local distance = math.sqrt( dx\*dx + dy\*dy ) &nbsp;&nbsp;&nbsp; local distance3 = math.sqrt( dx3\*dx3 + dy3\*dy3 ) &nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; local objectSize = (obj2.contentWidth/2) + (obj1.contentWidth/2) &nbsp;&nbsp;&nbsp; local objectSize3 = (obj3.contentWidth/2) + (obj1.contentWidth/2) &nbsp;&nbsp;&nbsp; if ( distance \< objectSize ) then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return true &nbsp;&nbsp;&nbsp; end &nbsp;&nbsp;&nbsp; if ( distance3 \< objectSize3 ) then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return true &nbsp;&nbsp;&nbsp; end &nbsp;&nbsp;&nbsp; return false end hasCollidedCircle( obj1, obj2, obj3 ) local function explosion() &nbsp;&nbsp;&nbsp; if hasCollidedCircle( obj1, obj2 ) then &nbsp;&nbsp;&nbsp; -- do the stuff when they are touching &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; obj1.isVisible = false &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; transition.cancel() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; obj1:pause() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; createExplosion() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; transition.to(bomb, {time = 1000, delay = 100, alpha = 0}) --// THIS IS NOT WORKING! &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Runtime:removeEventListener("enterFrame", explosion ) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Runtime:removeEventListener("touch", screenTouched) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; timer.cancel( timerBullet ) &nbsp;&nbsp;&nbsp; else &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; obj1.alpha = 1 &nbsp;&nbsp;&nbsp; end &nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; if hasCollidedCircle( obj1, obj3 ) then &nbsp;&nbsp;&nbsp; -- do the stuff when they are touching &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; obj1.isVisible = false &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; transition.cancel() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; obj1:pause() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; createExplosion() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; transition.to(bomb, {time = 1000, delay = 100, alpha = 0})--// THIS WORKS FINE!!! &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Runtime:removeEventListener("enterFrame", explosion ) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Runtime:removeEventListener("touch", screenTouched) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; timer.cancel( timerBullet ) &nbsp;&nbsp;&nbsp; else &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; obj1.alpha = 1 &nbsp;&nbsp;&nbsp; end end Runtime:addEventListener("enterFrame", explosion ) function screenTouched( event ) &nbsp;&nbsp;&nbsp; if ( event.phase == "began" ) then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;here = event.x &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;transition.to(obj1, {time = 1000, x = here}) &nbsp;&nbsp;&nbsp; elseif ( self.isFocus ) then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if ( event.phase == "moved" ) then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; elseif ( event.phase == "ended" or event.phase == "cancelled" ) then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; end &nbsp;&nbsp;&nbsp; end &nbsp;&nbsp;&nbsp; return true end &nbsp;&nbsp;&nbsp; local invisibleRectangle = display.newRect(sceneGroup, 0, 0, display.contentWidth, 400) &nbsp;&nbsp;&nbsp; invisibleRectangle.x = display.contentCenterX &nbsp;&nbsp;&nbsp; invisibleRectangle.y = display.contentCenterY &nbsp;&nbsp; &nbsp;invisibleRectangle:setFillColor(.97, .92, .10) &nbsp;&nbsp; &nbsp;invisibleRectangle:addEventListener("touch", screenTouched) &nbsp;&nbsp;&nbsp; end end -- ------------------------------------------------------------------------------ -- "scene:hide()" function scene:hide( event ) local sceneGroup = self.view local phase = event.phase &nbsp;&nbsp;&nbsp; if ( phase == "will" ) then &nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; elseif ( phase == "did" ) then &nbsp;&nbsp;&nbsp; end end -- ------------------------------------------------------------------------------ -- Listener setup scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) -- ------------------------------------------------------------------------------ return scene

Can you make a sample project of your problem? 

–SonicX278

Yes… here is the entire code…

I have the sprite explosion – “bomb”

is just a sprite with a explosion spritesheet

in one function the transition works fine

in the other it doesn’t.

It supposed to go alpha = 0, to make the sprite invisible after playing.

see the complete code. Thanks

-- // HOW TO MAKE AN APP 2016 -- // By Victor M. Barba -- // Copyright © 2016 by Victor M. Barba -- // All Rights Reserved -------------------------------------------------- local composer = require( "composer" ) local scene = composer.newScene() local sequenceTroth local trothImage local here local obj1 local obj2 local bomb local speed = 2500 local speed3 = 2300 local wait = 1500 local counter = 0 local timerBullet -- ------------------------------------------------------------------------------ -- "scene:create()" function scene:create(event) local sceneGroup = self.view &nbsp;&nbsp; &nbsp;local R = math.random(1, 99) &nbsp;&nbsp; &nbsp;local G = math.random(1, 99) &nbsp;&nbsp; &nbsp;local B = math.random(1, 99) &nbsp;&nbsp; &nbsp;local bgRectangle = display.newRect(sceneGroup, 0, 0, display.contentWidth, display.contentHeight) &nbsp;&nbsp;&nbsp; bgRectangle.x = display.contentCenterX &nbsp;&nbsp;&nbsp; bgRectangle.y = display.contentCenterY &nbsp;&nbsp; &nbsp;bgRectangle:setFillColor((R/100), (G/100), (B/100)) &nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; local buttonBack = display.newImageRect(sceneGroup, "images/backButton.png", 150, 150) &nbsp;&nbsp;&nbsp; buttonBack.x = 100 &nbsp;&nbsp;&nbsp; buttonBack.y = 60 &nbsp;&nbsp;&nbsp; local function buttonBackListener() &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;Runtime:removeEventListener("enterFrame", explosion ) &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;--Runtime:removeEventListener("touch", screenTouched) &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;timer.cancel( timerBullet ) &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;composer.gotoScene("menu3") &nbsp;&nbsp;&nbsp; end &nbsp;&nbsp;&nbsp; buttonBack:addEventListener("tap", buttonBackListener) &nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; local codeButton = display.newImageRect(sceneGroup, "images/seeButton.png", 150, 150) &nbsp;&nbsp;&nbsp; codeButton.x = display.contentWidth - 100 &nbsp;&nbsp;&nbsp; codeButton.y = 100 &nbsp;&nbsp;&nbsp; local function codeButtonListener() &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;Runtime:removeEventListener("enterFrame", explosion ) &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;Runtime:removeEventListener("touch", screenTouched) &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;timer.cancel( timerBullet ) &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;composer.gotoScene("programApp16") &nbsp;&nbsp;&nbsp; end &nbsp;&nbsp;&nbsp; codeButton:addEventListener("tap", codeButtonListener) &nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; sequenceTroth = { &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;{ name="smile", start=1, count=12, time=6000 }, &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;{ name="surprise", start=5, count=2, time=1000 }, &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;{ name="sad", start=2, count=6, time=1600 }, &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;{ name="happy", start=1, count=4, time=2000 } &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;} &nbsp;&nbsp; &nbsp;trothImage = graphics.newImageSheet( "images/spriteTroth.png", { width=200, height=200, numFrames=12 } ) &nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; function createTroth() &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;obj1 = display.newSprite( trothImage, sequenceTroth ) &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;sceneGroup:insert(obj1) &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;obj1.x = display.contentCenterX &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;obj1.y = display.contentHeight - 60 &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;obj1:scale(.6, .6) &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;obj1.setSequence = "smile" &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;obj1:play() &nbsp;&nbsp; &nbsp;end &nbsp;&nbsp; &nbsp;createTroth() &nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; sequenceExplosion = { &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;{ name="boom", start=1, count=48, time=1000, loopCount=1 } &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;} &nbsp;&nbsp; &nbsp;explosionImage = graphics.newImageSheet( "images/explosionSpritesheet.png", { width=400, height=400, numFrames=48 } ) &nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; function createExplosion() &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;bomb = display.newSprite( explosionImage, sequenceExplosion ) &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;sceneGroup:insert(bomb) &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;bomb.x = obj1.x &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;bomb.y = obj1.y &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;bomb:scale(.6, .6) &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;bomb.setSequence = "boom" &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;bomb:play() &nbsp;&nbsp; &nbsp;end end -- ------------------------------------------------------------------------------ -- "scene:show()" function scene:show( event ) local sceneGroup = self.view local phase = event.phase &nbsp;&nbsp;&nbsp; if (phase == "will") then---------------------------------------------------- &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; elseif (phase == "did") then---------------------------------------------------- &nbsp;&nbsp;&nbsp; local floor = display.newRect(sceneGroup, 0, 0, display.contentWidth, 20) &nbsp;&nbsp;&nbsp; floor.x = display.contentCenterX &nbsp;&nbsp;&nbsp; floor.y = display.contentHeight &nbsp;&nbsp; &nbsp;floor:setFillColor(.97, .92, .10) &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;local leftWall = display.newRect(sceneGroup, 0, 0, 20, display.contentHeight) &nbsp;&nbsp;&nbsp; leftWall.x = 0 &nbsp;&nbsp;&nbsp; leftWall.y = display.contentCenterY &nbsp;&nbsp; &nbsp;leftWall:setFillColor(.97, .92, .10) &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;local rightWall = display.newRect(sceneGroup, 0, 0, 20, display.contentHeight) &nbsp;&nbsp;&nbsp; rightWall.x = display.contentWidth &nbsp;&nbsp;&nbsp; rightWall.y = display.contentCenterY &nbsp;&nbsp; &nbsp;rightWall:setFillColor(.97, .92, .10) &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;obj2 = display.newImageRect(sceneGroup, "images/bullet.png", 200, 300) &nbsp;&nbsp; &nbsp;obj2:scale(.4, .4) &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;obj3 = display.newImageRect(sceneGroup, "images/bullet.png", 200, 300) &nbsp;&nbsp; &nbsp;obj3:scale(.4, .4) &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;local function resetBullets() &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;obj2.x = math.random(10, 1400) &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;obj2.y = -100 &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;obj3.x = math.random(10, 1400) &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;obj3.y = -100 &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;counter = counter + 1 &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;if counter == 20 then &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; Runtime:removeEventListener("enterFrame", explosion ) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;Runtime:removeEventListener("touch", screenTouched) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;timer.cancel( timerBullet ) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;local topText = display.newText( sceneGroup, "You did it, Next Level!", 100, 200, native.systemFont, 60 ) &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;topText.x = display.contentCenterX &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;topText.y = display.contentCenterY &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;topText:setFillColor( 0, 0, 0 ) &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;end &nbsp;&nbsp; &nbsp;end &nbsp;&nbsp; &nbsp;resetBullets() &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;local function sendBullet() &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;transition.to(obj2, {time = speed, delay = wait, y = 1000, onComplete = resetBullets}) &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;transition.to(obj3, {time = speed3, delay = wait, y = 1000, onComplete = resetBullets}) &nbsp;&nbsp; &nbsp;end &nbsp;&nbsp; &nbsp;timerBullet = timer.performWithDelay(3000, sendBullet, 10) &nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; -- Circle-based collision detection local function hasCollidedCircle( obj1, obj2 ) &nbsp;&nbsp;&nbsp; if ( obj1 == nil ) then&nbsp; -- Make sure the first object exists &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return false &nbsp;&nbsp;&nbsp; end &nbsp;&nbsp;&nbsp; if ( obj2 == nil ) then&nbsp; -- Make sure the other object exists &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return false &nbsp;&nbsp;&nbsp; end &nbsp;&nbsp;&nbsp; if ( obj3 == nil ) then&nbsp; -- Make sure the other object exists &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return false &nbsp;&nbsp;&nbsp; end &nbsp;&nbsp;&nbsp; local dx = obj1.x - obj2.x &nbsp;&nbsp;&nbsp; local dy = obj1.y - obj2.y &nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; local dx3 = obj1.x - obj3.x &nbsp;&nbsp; &nbsp;local dy3 = obj1.y - obj3.y &nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; local distance = math.sqrt( dx\*dx + dy\*dy ) &nbsp;&nbsp;&nbsp; local distance3 = math.sqrt( dx3\*dx3 + dy3\*dy3 ) &nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; local objectSize = (obj2.contentWidth/2) + (obj1.contentWidth/2) &nbsp;&nbsp;&nbsp; local objectSize3 = (obj3.contentWidth/2) + (obj1.contentWidth/2) &nbsp;&nbsp;&nbsp; if ( distance \< objectSize ) then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return true &nbsp;&nbsp;&nbsp; end &nbsp;&nbsp;&nbsp; if ( distance3 \< objectSize3 ) then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return true &nbsp;&nbsp;&nbsp; end &nbsp;&nbsp;&nbsp; return false end hasCollidedCircle( obj1, obj2, obj3 ) local function explosion() &nbsp;&nbsp;&nbsp; if hasCollidedCircle( obj1, obj2 ) then &nbsp;&nbsp;&nbsp; -- do the stuff when they are touching &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; obj1.isVisible = false &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; transition.cancel() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; obj1:pause() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; createExplosion() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; transition.to(bomb, {time = 1000, delay = 100, alpha = 0}) --// THIS IS NOT WORKING! &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Runtime:removeEventListener("enterFrame", explosion ) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Runtime:removeEventListener("touch", screenTouched) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; timer.cancel( timerBullet ) &nbsp;&nbsp;&nbsp; else &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; obj1.alpha = 1 &nbsp;&nbsp;&nbsp; end &nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; if hasCollidedCircle( obj1, obj3 ) then &nbsp;&nbsp;&nbsp; -- do the stuff when they are touching &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; obj1.isVisible = false &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; transition.cancel() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; obj1:pause() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; createExplosion() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; transition.to(bomb, {time = 1000, delay = 100, alpha = 0})--// THIS WORKS FINE!!! &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Runtime:removeEventListener("enterFrame", explosion ) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Runtime:removeEventListener("touch", screenTouched) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; timer.cancel( timerBullet ) &nbsp;&nbsp;&nbsp; else &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; obj1.alpha = 1 &nbsp;&nbsp;&nbsp; end end Runtime:addEventListener("enterFrame", explosion ) function screenTouched( event ) &nbsp;&nbsp;&nbsp; if ( event.phase == "began" ) then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;here = event.x &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;transition.to(obj1, {time = 1000, x = here}) &nbsp;&nbsp;&nbsp; elseif ( self.isFocus ) then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if ( event.phase == "moved" ) then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; elseif ( event.phase == "ended" or event.phase == "cancelled" ) then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; end &nbsp;&nbsp;&nbsp; end &nbsp;&nbsp;&nbsp; return true end &nbsp;&nbsp;&nbsp; local invisibleRectangle = display.newRect(sceneGroup, 0, 0, display.contentWidth, 400) &nbsp;&nbsp;&nbsp; invisibleRectangle.x = display.contentCenterX &nbsp;&nbsp;&nbsp; invisibleRectangle.y = display.contentCenterY &nbsp;&nbsp; &nbsp;invisibleRectangle:setFillColor(.97, .92, .10) &nbsp;&nbsp; &nbsp;invisibleRectangle:addEventListener("touch", screenTouched) &nbsp;&nbsp;&nbsp; end end -- ------------------------------------------------------------------------------ -- "scene:hide()" function scene:hide( event ) local sceneGroup = self.view local phase = event.phase &nbsp;&nbsp;&nbsp; if ( phase == "will" ) then &nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; elseif ( phase == "did" ) then &nbsp;&nbsp;&nbsp; end end -- ------------------------------------------------------------------------------ -- Listener setup scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) -- ------------------------------------------------------------------------------ return scene