I am creating my first iPhone game and cannot figure out how to reload my game.
I am using coronaSDK and have tried to use their composer API. When the player dies a button with "play again" appears and when he touches it I direct him to a scene called "reload.lua" with composer.gotoScene("reload").
In this scene I have the following code:
function scene:show(event)
local sceneGroup = self.view
local phase = event.phase
if (phase == "will") then
elseif (phase == "did") then
local replay = display.newImage('star1.png', 100, 300)
composer.removeScene("level1")
composer.gotoScene("level1")
end
end
However, this only adds level1 on top of the existing one and does not remove the one that has been used. Any ideas on how I could successfully remove level1 or reload my game?
You should read this article slowly and carefully.
The source explains:
For Composer to “manage” your scene and the display objects within, all of these objects must be inserted into the scene’s view display group or a child display group of it.
The group is referenced in your code by the line at the very top of the scene:show function:
local sceneGroup = self.view
You must add all display objects into this group, for example your local replay should be:
local replay = display.newImage('star1.png', 100, 300)
sceneGroup:insert( replay )
Once above design is followed you can start utilising the other features of the composer as you request.
While this is based on Composer's older brother Storyboard, the concepts still apply to Composer. It explains the various issues with trying to reload scenes, recommendations on how to manage it:
http://coronalabs.com/blog/2013/08/20/tutorial-reloading-storyboard-scenes/
Related
When I'm using
composer.gotoscene("main")
An error pops up with the message:
"Attempt to concatenate global 'sceneName' stack traceback".
If I use another page to redirect instead of main.lua (example "scene2") then it works.
All of the code for the game should be in its own Composer scene rather than main.lua. If the Composer scene with the play button is called HomeScene.lua, in main.lua you would put
local composer = require "composer"
composer.gotoScene( "HomeScene" )
This makes the scene with the play button load and appear first. If you want to initialize your game scene before the player can have a chance to press Play and start the game, you can add composer.loadScene("GameScene") in main.lua as well. This just creates the scene and stores it without showing it.
In HomeScene.lua, if the play button is a DisplayObject called playButton, you would do something like
local composer = require "composer"
local function handleTouch( event )
if event.phase == "began" then
composer.gotoScene( "GameScene" )
end
end
playButton:addEventListener( "touch", handleTouch )
Hope this helps.
So Ive been wondering if this is possible to do, as it is important for my game.
Basically I have a menu.lua file (game plays only from this one file) but before the game loads up, I initiate this code which loads up a new lua file (tutorial.lua)
storyboard.showOverlay("tutorial", {effect="fromRight", isModal=true, time=50})
Now when I use this code to get back to menu.lua, the game does nothing.
storyboard.hideOverlay({"fromTop"})
The reason why is because my game begins from calling beginGame() from menu.lua. So I would like to ask if I can somehow call beginGame() from menu.lua once storyboard.hideOverlay code complete from within tutorial.lua
Cheers.
Corona no longer supports Storyboard implementation., So start using Corona Composer library. Corona Docs
In composer refer this to start the game when overlay hides. hideOverlay
Hi i have problem in composer API
i have two scene the first one is welcome scene and the second one is how to play scene
my problem is when i go from the welcome scene to how to play scene and showing some object on it
and then i go back to welcome scene and re open how to play scene
so how to play scene show me the last state and it doesn't reloaded from the zero
so how could i delete the how to play scene or rest it before i go to welcome scene
when i call scene:destroy() in the function which move me to welcome scene i got an exception
here is my code in welcome scene
local howtoplay=display.newImage("HOW TO PLAY" .. suffix,param.centerX,param.centerY+117)
sceneGroup:insert( howtoplay )
local function Play( action )
composer.gotoScene("level",options)
end
howtoplay:addEventListener( 'touch' ,How)
local function How( action)
and this how to play code
backbutton=display.newImageRect("Back.png",H*(30/100),W*(16/100))
backbutton.x=paddingX+(H*(30/100))/2
backbutton.y=H*(8/100)
local function back( action)
composer.gotoScene("wlc")
end
sceneGroup:insert(backbutton)
backbutton:addEventListener( 'touch', back)
in your wlc.lua try putting composer.removeScene("howToPlayScene") inside
function scene:show( event )
composer.removeScene("howToPlayScene")
end
This works for me all the time.
I currently using the game template created by Corona SDK, to develop my game, what i trying to do is something very simple, i want the crate to stay where it was spawned, and only move after the crate is tapped. To achieve that I tried to remove the gravityScale of the crate. This is what i have done:
local function crateTap( event )
print( "gravityScale : " .. event.target.gravityScale)
event.target.gravityScale = 1
print( "gravityScale : " .. event.target.gravityScale)
end
local crate = display.newImageRect( "crate.png", 45, 45 )
crate.x, crate.y = 160, 20
crate.rotation = 15
-- add physics to the crate
physics.addBody( crate, { density=1.0, friction=0.3, bounce=0.3 } )
crate.gravityScale = 0
crate:addEventListener( "tap", crateTap )
The application loads ok, and if i click really fast on the crate, like in the first second on the screen, the crate will fall off, but if i wait a little and click, nothing will happen. And i have no idea why.
Here's the complete level1.lua file http://pastebin.com/yjxmGqw5
Thanks
Then the issue is elsewhere in your code. There is nothing wrong with the code as shown. Gravity scale can be changed at any time, and takes effect as soon as the event handler returns. The fact that it works early on shows this. That it then stops working indicates that something else happens somewhere after a second of play, like perhaps the physics engine gets paused by another event, or something in an enterFrame event handler.
Im a beginner to the Corona SDK.
I have 2 scenes A & B.
In scene A i have a button with a OnRelease event.
This button is created and added to the group in the scene Create event.
Clicking the button takes me to scene B ( storyboard.gotoScene("B") ).
In scene B i have a touch event on an box (crate image).
The touch listener is added in the scene Started event, and removed in the scene Exited event.
Clicking on the crate takes me back to A ( storyboard.gotoScene("A") ).
So here's the real annoying issue:
After going back to A, all events in this scene are now disabled.
i.e. I can no longer click on the button any more (no event).
Will provide code snippet if I am missing information above.
Thank you.
* Update *
After taking about a break from this, i went back today and started debugging this again. I found the issue fairly quickly. The problem had something to do with my touched event handler (which causes a transition from scene B to A).
snippet below that caused the issue:
function testTouched( event )
-- process cue-touched event...
--local t = event.target -- commenting this was the fix.
local phase = event.phase
if "began" == phase then
print(" -> back to menu")
--display.getCurrentStage():setFocus( t ) -- commenting this was the fix.
--t.isFocus = true -- commenting this was the fix.
storyboard.gotoScene( "menu", "flipFadeOutIn", 500 )
end
-- Stop further propagation of touch event
return true
From what I see here,
enterScene
Dispatched when storyboard.gotoScene() is called, immediately after
the transition has completed. So if you specified a transition effect,
this event is dispatched as soon as the effect has ended. Adding
listeners, or app/game-specific logic should be placed in the listener
function for this event.
exitScene
When storyboard.gotoScene() is called, an “exitScene” event will be
dispatched to the currently shown scene before the transition occurs.
Cleanup duties, such as removing event listeners, stopping timers,
etc. should be placed in the listener function for this event.
Add event listeners in enterScene() event rather than createScene() and remove them at exitScene()?
Edit: I think you would need to display.getCurrentStage():setFocus(nil) in exitScene().