Hi again!
Sorry to say I still get some strange result. It seems to have something to do when I load the physics in the enterScene. Even if I destroy the objects it does not complete the structure.
Here is an example for you guys. Change out scene1.lua in you’r storyboard example and touch the background.
[code]
–
– scene1.lua
local storyboard = require( “storyboard” )
local scene = storyboard.newScene()
– Load physics
local physics = require(“physics”)
physics.start()
physics.setDrawMode( “hybrid” )
– BEGINNING OF YOUR IMPLEMENTATION
local image, text1, text2, text3, memTimer, test
– Touch event listener for background image
local function onSceneTouch( self, event )
if event.phase == “began” then
storyboard.reloadScene()
return true
end
end
– Called when the scene’s view does not exist:
function scene:createScene( event )
local screenGroup = self.view
image = display.newImage( “bg.jpg” )
screenGroup:insert( image )
image.touch = onSceneTouch
text1 = display.newText( “Scene 1”, 0, 0, native.systemFontBold, 24 )
text1:setTextColor( 255 )
text1:setReferencePoint( display.CenterReferencePoint )
text1.x, text1.y = display.contentWidth * 0.5, 50
screenGroup:insert( text1 )
text2 = display.newText( "MemUsage: ", 0, 0, native.systemFont, 16 )
text2:setTextColor( 255 )
text2:setReferencePoint( display.CenterReferencePoint )
text2.x, text2.y = display.contentWidth * 0.5, display.contentHeight * 0.5
screenGroup:insert( text2 )
text3 = display.newText( “Touch to continue.”, 0, 0, native.systemFontBold, 18 )
text3:setTextColor( 255 ); text3.isVisible = false
text3:setReferencePoint( display.CenterReferencePoint )
text3.x, text3.y = display.contentWidth * 0.5, display.contentHeight - 100
screenGroup:insert( text3 )
print( “\n1: createScene event”)
end
– Called immediately after scene has moved onscreen:
function scene:enterScene( event )
local screenGroup = self.view
print( “1: enterScene event” )
– Create physics
test = scene:createTest()
screenGroup:insert( test )
– remove previous scene’s view
storyboard.purgeScene( “scene4” )
– Update Lua memory text display
local showMem = function()
image:addEventListener( “touch”, image )
text3.isVisible = true
text2.text = text2.text … collectgarbage(“count”)/1000 … “MB”
text2.x = display.contentWidth * 0.5
end
memTimer = timer.performWithDelay( 1000, showMem, 1 )
end
function scene:createTest()
– Testing physics
local group = display.newGroup()
local ceiling = display.newRect (0, 0, display.contentWidth, 1)
ceiling:setFillColor(255,255,255,0)
– Add body
physics.addBody (ceiling, “static”, {bounce = 0.8, friction = 10})
group:insert(ceiling)
– Set some vars
local prevBody = ceiling
local x = 100
local y = 0
local w = 1
local h = 10
local currentY = y
local currentX = x
– Add rope
for i = 1, 6 do
local rope = display.newRect (x, y, w, h)
rope:setFillColor( 255, 255, 255)
physics.addBody( rope, { density=400, friction=0.8, bounce = 0 })
local joint = physics.newJoint( “pivot”, prevBody, rope, x, y )
group:insert(rope)
y = y + h
prevBody = rope
end
–Add head
local head = display.newCircle( x, y+12, 12 )
local r = head.height *0.4
group:insert(head)
physics.addBody( head, “dynamic”, { density=20, friction=1.0, bounce=.2, radius=r })
head.linearDamping = math.random(1,10)
head.angularDamping = .6
local headJoint = physics.newJoint( “pivot”, prevBody, head, x, y )
headJoint.dampingRatio = 1
y=y+24
prevBody=head
– Add body
local torso = display.newRect (x, y, 2, 50)
torso:rotate( headJoint.jointAngle )
physics.addBody( torso, { density=10, friction=0.8, bounce = 0 })
local joint = physics.newJoint( “weld”, head, torso, head.x, y )
group:insert(torso)
– Add clean up
group.remove = function()
if group.numChildren then
for i = group.numChildren, 1, -1 do
if group[i] then
group[i]:removeSelf()
group[i] = nil
end
end
end
end
return group
end
– Called when scene is about to move offscreen:
function scene:exitScene( event )
– Remove physics
test.remove()
print( “1: exitScene event” )
– remove touch listener for image
image:removeEventListener( “touch”, image )
– cancel timer
timer.cancel( memTimer ); memTimer = nil;
– reset label text
text2.text = "MemUsage: "
end
– Called prior to the removal of scene’s “view” (display group)
function scene:destroyScene( event )
print( “((destroying scene 1’s view))” )
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
[/code] [import]uid: 106083 topic_id: 24632 reply_id: 100388[/import]