Hi Everyone… I know this issue has come up on more than a few occasions, but I guess the solution is deifferent for everyone.
I’m making a casual game (more of an exercise than anything else). I’m using a catapult-style launcher (poached directly from Ghosts Vs Monsters)… here is an example of the ‘touch mechanism’ (it will make sense why I’ve included this later):
local onScreenTouch = function( event ) if event.phase == "began" == false and event.xStart \> 220 and event.xStart \< 420 and event.yStart \> 700 and event.yStart \< 900 then shotOrb.x = aimball.x; shotOrb.y = aimball.y shotOrb.xScale = 2; shotOrb.yScale = 2 shotArrow.isVisible = true timerText.isVisible = true if shotOrb.isVisible == true then local xOffset = aimObject.x local yOffset = aimObject.y local distanceBetween = mCeil(mSqrt( ((event.y - yOffset) ^ 2) + ((event.x - xOffset) ^ 2) )) shotOrb.xScale = -distanceBetween \* 0.02 shotOrb.yScale = -distanceBetween \* 0.02 local angleBetween = mCeil(mAtan2( (event.y - yOffset), (event.x - xOffset) ) \* 180 / mPi) + 90 aimObject.rotation = angleBetween + 180 shotArrow.rotation = aimObject.rotation if event.phase == "ended" then shotOrb.isVisible = false shotArrow.isVisible = false local x = event.x local y = event.y local xForce = (-1 \* (x - mainObject.x)) \* 1.15 --\> 2.75 local yForce = (-1 \* (y - mainObject.y)) \* 1.15 --\> 2.75 mainObject:applyForce( xForce, yForce, mainObject.x, mainObject.y )
Simple enough… Now, I have a reload, or more accurately, a replay-the-level button. - After seacrhing for various ways to achieve this, I went with ‘put an intermediary scene between the original scene and it’s reloaded version’… I’ve taken good care to destroy any listeners that may cause future conflicts, and my intermediary scene looks like this:
local storyboard = require( "storyboard" ) local scene = storyboard.newScene() function scene:createScene( event ) local group = self.view end function scene:enterScene( event ) local group = self.view storyboard.removeScene( "level1" ) storyboard.reloadScene( "level1" ) storyboard.gotoScene("level1") end function scene:exitScene( event ) local group = self.view end function scene:destroyScene( event ) local group = self.view end scene:addEventListener( "createScene", scene ) scene:addEventListener( "enterScene", scene ) scene:addEventListener( "exitScene", scene ) scene:addEventListener( "destroyScene", scene ) return scene
OK, this isn’t perfect, but it successfully reloads the scene… However, my ‘touch event’ becomes unresponsive because corona can no longer index the x and y co-ordinates specified in my ‘catapult launcher’
Really can’t get my head around this one. If anyone can tell me where I’ve gone wrong or perhaps suggest a better way to reload a scene from scratch I’d be eternally grateful.
Thanks
Mark