Please don’t judge me yet…
believe me I have read the documentations many times, I have tried many sample codes, and I still don’t understand the – purgeScene ( ).
Please be patient with me, I really want to learn and I just need this to finish my app.
I post before to anton2111, that it works! I was really happy. Then I do another scene
with the same code and it doesn’t work, I’m going crazy!
I’m including a video so you I can explain myself better
http://www.youtube.com/watch?v=HplAVPcuObs&feature=youtu.be
I will explain the best I can:
The level1 scene has 1 object with physics, and 4 rec, with display.newRect
there is one spot, on the clearTop, (let’s say x=100, y=10) that I don’t have any rec, so the object can “escape” through there.
When I go to level 2, another scene, the display.newRect now is in a different position
so the “escape” spot now is in the clearLeft, in (x = 1, y=280) .
I know that in level2 scene the “escape” spot now has “pysics” on (x.100, y=10)
When I come back to level1 view the “memory” of the program has that information (physics on x=100,y=10)
that’s why --I guess-- now I can not “escape” in the level1 scene, because it’s still in the memory of the program.
–NOW how do I fix this?
----------------------------------FIRST POSSIBILITY----------------------------------------
I’m in “escape1” (scene1) first view. (I remember Ron told me, I purge my Scenes before I go to them)
so before I go to “escape2”, in “escape1.lua” I place
– remove previous scene’s view
storyboard.purgeScene( “escape2” ) – where?
1.- At the very top of the file? Where I requiere widgets and physics?
2.- at the beginning of createScene? right after local screenGroup?
3.- at the beginning of enterScene?
then, in “escape2.lua” I purgeScene (“escape3”) and so on?
--------------------------------SECOND POSSIBILITY----------------------------------------
Before I go to them" –
In “escape2” as soon as I createScene, I purgeScene (“escape1”)
so I’m purging “escape1” then I createScene escape2?
I just don’t understand this, if you explain really clear and put the code exactly as it should work, I believe this will help a lot of people, not just me. I’m including the complete code for “escape1” and “escape2”
Thank you very much for helping me really understand the purgeScene ( ) thing.
--------------------- “escape1.lua”--------------------------------------------------------------
local storyboard = require( “storyboard” )
local widget = require “widget”
local scene = storyboard.newScene()
local physics = require (“physics”)
physics.start()
local image, text1, text2, memTimer
local ladyBug
local clearFloor
local clearLeft
local clearTop
local clearRightTop
local clearRightBottom
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
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
– remove previous scene’s view
storyboard.purgeScene( “home” )
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.6})
ladyBug:addEventListener (“touch”, dragBody)
end
--------------------------------------------------------------EXIT SCENE----------
function scene:exitScene()
ladyBug:removeEventListener (“touch”, dragBody)
display.remove ( ladyBug )
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
--------------------- “escape2.lua”--------------------------------------------------------------
local storyboard = require( “storyboard” )
local widget = require “widget”
local scene = storyboard.newScene()
local physics = require (“physics”)
physics.start()
local image, text1, text2, memTimer
local ladyBug
local clearFloor
local clearLeft
local clearTop
local clearRight
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
– remove previous scene’s view
storyboard.purgeScene( “escape1” )
image = display.newImage( “backgroundEscape2.png” )
screenGroup:insert( image )
image.x = 512
image.y = 384
text1 = display.newText( “Help the ant escape!”, 0, 0, nil, 20 )
text1:setTextColor( 255 )
text1.x = 777
text1.y = 180
screenGroup:insert( text1 )
text2 = display.newText( “Level 2”, 0, 0, nil, 20 )
text2:setTextColor( 255 )
text2.x = 55
text2.y = 50
screenGroup:insert( text2 )
clearFloor = display.newRect(1,760,1020,5) – blue
--clearFloor.strokeWidth = 1
clearFloor:setFillColor(0, 0, 255)
clearFloor:setStrokeColor(0, 255, 0)
screenGroup:insert( clearFloor )
physics.addBody (clearFloor, “static”)
clearLeft = display.newRect(10, 1, 5, 768) – red
--clearLeft.strokeWidth = 1
clearLeft:setFillColor(255, 0, 0)
clearLeft:setStrokeColor(0, 255, 0)
screenGroup:insert( clearLeft )
physics.addBody (clearLeft, “static”)
clearTop = display.newRect(1, 20, 1020, 5) – purple
--clearTop.strokeWidth = 1
clearTop:setFillColor(100, 13, 98)
clearTop:setStrokeColor(0, 255, 0)
screenGroup:insert( clearTop )
physics.addBody (clearTop, “static”)
clearRight = display.newRect(1020, 1, 5, 768) – green
--clearRight.strokeWidth = 1
clearRight:setFillColor(0, 255, 0)
clearRight:setStrokeColor(0, 255, 0)
screenGroup:insert( clearRight )
physics.addBody (clearRight, “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 (“ant.png”)
ladyBug.x = 200
ladyBug.y = 600
physics.addBody (ladyBug, “dynamic”, {density=3, friction=-0.7 , bounce = 0.6})
ladyBug:addEventListener (“touch”, dragBody)
end
--------------------------------------------------------------EXIT SCENE----------
function scene:exitScene()
ladyBug:removeEventListener (“touch”, dragBody)
display.remove ( ladyBug )
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