Thank you Rob for explaining that, I’m beginning to understand, just a little bit.
thank you for the --[–code–]-- thing I was wondering how you guys did that. Thanks.
The app start at home.lua and has 4 buttons –
-
play
-
learn
-
songs
-
game – in the game I have all the physics. I call the “escape1” “escape2” and so on.
---------main.lua-------
display.setStatusBar( display.HiddenStatusBar ) local storyboard = require "storyboard" storyboard.purgeOnSceneChange = true storyboard.gotoScene( "home" )
I start the app with the home.lua file. In there I have the 4 buttons.
-----------home.lua ------------
local storyboard = require( "storyboard" ) local widget = require "widget" local scene = storyboard.newScene() local wrongSound = audio.loadSound( "wrongSound.mp3") local buttonPlay local function buttonPlay() storyboard.gotoScene( "play1", "crossFade", 500 ) return true end local function buttonLearn() storyboard.gotoScene( "learn1", "crossFade", 500 ) return true end local function buttonSongs() storyboard.gotoScene( "list1", "crossFade", 500 ) return true end local function playGame() storyboard.gotoScene( "escape1", "crossFade", 500 ) return true end -- --==\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*[CREATE SCENE]\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*++-- -- function scene:createScene( event ) local group = self.view local background = display.newImage( "backgroundHome.png" ) buttonPlay = widget.newButton{ defaultFile="buttonPlayHomeRelease.png", overFile="buttonPlayHomeOver.png", onRelease = buttonPlay } buttonPlay.x = 240 buttonPlay.y = 377 buttonPlay:scale (.9, .9) buttonLearn = widget.newButton{ defaultFile="buttonLearnHomeRelease.png", overFile="buttonLearnHomeOver.png", onRelease = buttonLearn } buttonLearn.x = 810 buttonLearn.y = 377 buttonLearn:scale (.9, .9) buttonSongs = widget.newButton{ defaultFile="buttonSongsHomeRelease.png", overFile="buttonSongsHomeOver.png", onRelease = buttonSongs } buttonSongs.x = 240 buttonSongs.y = 550 buttonSongs:scale (.9, .9) buttonGame = widget.newButton{ defaultFile="buttonGameHomeRelease.png", overFile="buttonGameHomeOver.png", onRelease = playGame } buttonGame.x = 810 buttonGame.y = 550 buttonGame:scale (.9, .9) ---------------------------------------------------------------------insert into group---- group:insert ( background ) group:insert ( buttonPlay ) group:insert ( buttonLearn ) group:insert ( buttonSongs ) group:insert ( buttonGame ) end -- --==\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*[ENTER SCENE]\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*++-- -- function scene:enterScene( event ) local group = self.view end -- --==\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*[EXIT SCENE]\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*++-- -- function scene:exitScene( event ) local group = self.view end -- --==\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*[DESTROY SCENE]\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*++-- -- function scene:destroyScene( event ) local group = self.view if buttonPlay then buttonPlay:removeSelf() buttonPlay = nil end if buttonLearn then buttonLearn:removeSelf() buttonLearn = nil end if buttonSongs then buttonSongs:removeSelf() buttonSongs = nil end if buttonGame then buttonGame:removeSelf() buttonGame = nil end end -- --==\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*[EVENT LISTENER]\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*++-- -- ---------------------------------------------------------------------------------- scene:addEventListener( "createScene", scene ) scene:addEventListener( "enterScene", scene ) scene:addEventListener( "exitScene", scene ) scene:addEventListener( "destroyScene", scene ) return scene
All of the scenes without physics, they work fine. The problem is getting them to work
physics and no physics. All of the files without physics works pretty much the same
– a background, a few buttons, and an object for animation with transition.to like this
----------------------- level1Q14--------
local storyboard = require( "storyboard" ) local widget = require "widget" local scene = storyboard.newScene() local wrongSound = audio.loadSound( "wrongSound.mp3") local ladyBug local ladyBug1 local ladyBug2 local ladyBug3 -- --==\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*[FUNCTIONS TO GO TO ANOTHER SCENE]\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*++-- -- local function buttonHome() storyboard.gotoScene( "home", "crossFade", 1000 ) return true end local function buttonApple() storyboard.gotoScene( "learn1", "crossFade", 1000 ) return true end local function buttonBulbHome() storyboard.gotoScene( "ending", "crossFade", 1000 ) return true end local function buttonRound1() storyboard.gotoScene( "wrong1", "crossFade", 1000 ) audio.play(wrongSound) return true end local function buttonRound2() storyboard.gotoScene( "wrong1", "crossFade", 1000 ) audio.play(wrongSound) return true end local function buttonRound3() storyboard.gotoScene( "level1Q15", "crossFade", 1000 ) return true end local function buttonRound4() storyboard.gotoScene( "wrong1", "crossFade", 1000 ) audio.play(wrongSound) return true end -- --==\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*[CREATE SCENE]\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*++-- -- function scene:createScene( event ) local group = self.view local background = display.newImage( "backgroundLevel1Q14.png" ) buttonHome = widget.newButton{ defaultFile="buttonHome.png", onRelease = buttonHome } buttonHome.x = 522 buttonHome.y = 655 buttonApple = widget.newButton{ defaultFile="buttonApple.png", onRelease = buttonApple } buttonApple.x = 420 buttonApple.y = 710 buttonBulbHome = widget.newButton{ defaultFile="buttonBulbHomeRelease.png", overFile="buttonBulbHomeOver.png", onRelease = buttonBulbHome } buttonBulbHome.x = 615 buttonBulbHome.y = 685 buttonRound1 = widget.newButton{ defaultFile="buttonHalfRest.png", onRelease = buttonRound1 } buttonRound1.x = 166 buttonRound1.y = 312 buttonRound2 = widget.newButton{ defaultFile="buttonWholeRest.png", onRelease = buttonRound2 } buttonRound2.x = 510 buttonRound2.y = 432 buttonRound3 = widget.newButton{ defaultFile="buttonQuarterRest.png", onRelease = buttonRound3 } buttonRound3.x = 800 buttonRound3.y = 322 buttonRound4 = widget.newButton{ defaultFile="buttonEightRest.png", onRelease = buttonRound4 } buttonRound4.x = 233 buttonRound4.y = 579 ---------------------------------------------------------------------insert into group---- group:insert ( background ) group:insert ( buttonHome ) group:insert ( buttonApple ) group:insert ( buttonBulbHome ) group:insert ( buttonRound1 ) group:insert ( buttonRound2 ) group:insert ( buttonRound3 ) group:insert ( buttonRound4 ) end -- --==\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*[ENTER SCENE]\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*++-- -- function scene:enterScene( event ) local group = self.view ladyBug = display.newImage( "bat.png" ) ladyBug.x = 600 ladyBug.y = 800 ladyBug:scale (.4, .4) ladyBug.rotation = ( -10 ) transition.to(ladyBug, {x=200 , y=112, time=4000}) ladyBug1 = display.newImage( "bat.png" ) ladyBug1.x = 50 ladyBug1.y = 800 ladyBug1:scale (.4, .4) ladyBug1.rotation = ( 87 ) transition.to(ladyBug1, {x=900 , y=112, time=3300}) ladyBug2 = display.newImage( "bat.png" ) ladyBug2.x = 1000 ladyBug2.y = 400 ladyBug2:scale (.4, .4) ladyBug2.rotation = ( -50 ) transition.to(ladyBug2, {x=23 , y=300, time=3300}) ladyBug3 = display.newImage( "bat.png" ) ladyBug3.x = 666 ladyBug3.y = -400 ladyBug3:scale (.4, .4) ladyBug3.rotation = ( 190 ) transition.to(ladyBug3, {x=800 , y=700, time=3300}) group:insert ( ladyBug ) group:insert ( ladyBug1 ) group:insert ( ladyBug2 ) group:insert ( ladyBug3 ) end -- --==\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*[EXIT SCENE]\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*++-- -- function scene:exitScene( event ) local group = self.view display.remove(ladyBug) display.remove(ladyBug1) display.remove(ladyBug2) display.remove(ladyBug3) end -- --==\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*[DESTROY SCENE]\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*++-- -- function scene:destroyScene( event ) local group = self.view if buttonHome then buttonHome:removeSelf() buttonHome = nil end if buttonApple then buttonApple:removeSelf() buttonApple = nil end if buttonBulbHome then buttonBulbHome:removeSelf() buttonBulbHome = nil end if buttonRound1 then buttonRound1:removeSelf() buttonRound1 = nil end if buttonRound2 then buttonRound2:removeSelf() buttonRound2 = nil end if buttonRound3 then buttonRound3:removeSelf() buttonRound3 = nil end if buttonRound4 then buttonRound4:removeSelf() buttonRound4 = nil end end -- --==\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*[EVENT LISTENER]\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*++-- -- ---------------------------------------------------------------------------------- scene:addEventListener( "createScene", scene ) scene:addEventListener( "enterScene", scene ) scene:addEventListener( "exitScene", scene ) scene:addEventListener( "destroyScene", scene ) return scene
And all of the “escape1” lua files with physics they work the same
– a background, the walls to prevent the object from leaving the screen
and a “magic spot” where I have no rects, so the object (or bee) can escape.
-------------------------- escape1.lua -----------
local storyboard = require( "storyboard" ) local widget = require "widget" local scene = storyboard.newScene() local physics = require ("physics") local image, text1, text2, memTimer local ladyBug local clearFloor local clearLeft local clearTop local clearRightTop local clearRightBottom local buttonB local buttonNext local function movePlace() storyboard.gotoScene( "home", "crossFade", 500 ) return true end local function nextLevel() storyboard.gotoScene( "escape2", "crossFade", 300 ) return true end local function dragBody ( event ) local body = event.target local phase = event.phase local stage = display.getCurrentStage ( ) if "began" == phase then stage:setFocus ( body, event.id ) body.isFocus = true body.tempJoint = physics.newJoint ( "touch", body, event.x, event.y ) elseif body.isFocus then if "moved" == phase then body.tempJoint:setTarget ( event.x, event.y ) elseif "ended" == phase or "cancelled" == phase then stage:setFocus (body, nil) body.isFocus = false body.tempJoint:removeSelf ( ) end end return true end --------------------------------------------------------------CREATE SCENE---------- function scene:createScene( event ) local screenGroup = self.view physics.start() image = display.newImage( "backgroundEscape1.png" ) screenGroup:insert( image ) image.x = 512 image.y = 384 text1 = display.newText( "Help the Butterfly escape!", 0, 0, nil, 20 ) text1:setTextColor( 255 ) text1.x = 777 text1.y = 180 screenGroup:insert( text1 ) text2 = display.newText( "Level 1", 0, 0, nil, 20 ) text2:setTextColor( 255 ) text2.x = 55 text2.y = 50 screenGroup:insert( text2 ) clearFloor = display.newRect(1,770,1020,5) --clearFloor.strokeWidth = 1 clearFloor:setFillColor(0, 0, 255) clearFloor:setStrokeColor(0, 255, 0) screenGroup:insert( clearFloor ) physics.addBody (clearFloor, "static") clearLeft = display.newRect(-5, 1, 5, 768) --clearLeft.strokeWidth = 1 clearLeft:setFillColor(255, 0, 0) clearLeft:setStrokeColor(0, 255, 0) screenGroup:insert( clearLeft ) physics.addBody (clearLeft, "static") clearTop = display.newRect(2, -5, 350, 5) --clearTop.strokeWidth = 1 clearTop:setFillColor(100, 13, 98) clearTop:setStrokeColor(0, 255, 0) screenGroup:insert( clearTop ) physics.addBody (clearTop, "static") clearRightTop = display.newRect(580, -5, 500, 5) --visible (1000, 1, 5, 240) --clearRightTop.strokeWidth = 1 clearRightTop:setFillColor(255, 255, 0) clearRightTop:setStrokeColor(0, 255, 0) screenGroup:insert( clearRightTop ) physics.addBody (clearRightTop, "static") clearRightBottom = display.newRect(1030, 1, 5, 768) --visible (1000, 301, 5, 468) --clearRightBottom.strokeWidth = 1 clearRightBottom:setFillColor(255, 100, 0) clearRightBottom:setStrokeColor(0, 255, 0) screenGroup:insert( clearRightBottom ) physics.addBody (clearRightBottom, "static") end -------------------------------------------------------------------------------------- --------------------------------------------------------------ENTER SCENE---------- function scene:enterScene( event ) local group = self.view buttonB = widget.newButton{ defaultFile="buttonHome.png", onRelease = movePlace } buttonB.x = 500 buttonB.y = 705 group:insert ( buttonB ) buttonNext = widget.newButton{ defaultFile="buttonNext.png", onRelease = nextLevel } buttonNext.x = 900 buttonNext.y = 705 group:insert ( buttonNext ) ladyBug = display.newImage ("butterflySmall.png") ladyBug.x = 200 ladyBug.y = 600 physics.addBody (ladyBug, "dynamic", {density=3, friction=-0.7 , bounce = 0.5}) ladyBug:addEventListener ("touch", dragBody) end -------------------------------------------------------------------------------------- --------------------------------------------------------------EXIT SCENE---------- function scene:exitScene() ladyBug:removeEventListener ("touch", dragBody) display.remove ( ladyBug ) physics.stop() end -------------------------------------------------------------------------------------- --------------------------------------------------------------DESTROY SCENE---------- function scene:destroyScene( event ) if buttonB then buttonB:removeSelf() buttonB = nil end if buttonNext then buttonNext:removeSelf() buttonNext = nil end end --------------------------------------------------------------------------------- -- END OF YOUR IMPLEMENTATION --------------------------------------------------------------------------------- scene:addEventListener( "createScene", scene ) scene:addEventListener( "enterScene", scene ) scene:addEventListener( "exitScene", scene ) scene:addEventListener( "destroyScene", scene ) --------------------------------------------------------------------------------- return scene
That’s it. I know that even a simple app, can have a lot of things going on.
I’m learning a lot, but I know that I still need a long way to go.
I just want to thank you again, for taking the time to help me out with this app.
I hope apple approve it once it’s finished, and I will keep learning
to make better apps to help kids learn music in a nice way.
Thanks
Victor