I'm new to Corona and Lua, so pleas be patient.
So i have a main screen, that I go to from my main file, and it works fine. Then i want to go back to that sceen from a diffrent sceen, and nothing happens (but the "win" is spaming my consol window).
The win function:
function win()
print("win!");
storyboard.gotoScene( "MSCREEN.mscreen" );
end
Function where i'm calling it:
function scene:enterFrame(inEvent)
--some other stuff
if (ball.x>display.contentWidth and failFlag==false) then
win();
end
end
and my main sceen:
local scene = storyboard.newScene();
local bg;
local text;
function scene:createScene(inEvent)
bg = display.newImage( "MSCREEN/bg.png");
bg.x = display.contentWidth / 2;
bg.y = display.contentHeight / 2;
text=display.newText( "Touch!", 0,0, fontName, 70);
text.x =display.contentWidth / 2 ;
text.y =display.contentHeight / 2 +230;
self.view:insert(bg);
self.view:insert(text);
end
function scene:enterFrame(inEvent)
tekstBujany(text);
end
function scene:touch(inEvent)
if inEvent.phase == "ended" then
storyboard.gotoScene( "JUMP.GJump" );
end
end
function scene:destroyScene(inEvent)
bg:removeSelf();
bg=nil;
text:removeSelf();
text=nil;
end -- End destroyScene().
scene:addEventListener("createScene", scene);
scene:addEventListener("destroyScene", scene);
Runtime:addEventListener("enterFrame", scene);
Runtime:addEventListener("touch",scene);
return scene;
SOLVED
so adding
storyboard.purgeOnSceneChange = true;
did the trick, not sure why.
Be careful with the win() function, it looks like it gets called a lot of times. Set another variable and change it in the method.
function win()
isNotWin = false -- so this method doesn't get called a lot of times.
print("win!")
storyboard.gotoScene( "MSCREEN.mscreen" )
end
function scene:enterFrame(inEvent)
--some other stuff
if (ball.x>display.contentWidth and failFlag==false and isNotWin == true) then
win();
end
end
The "enterFrame" event gets generated at every frame for the Runtime object, so about 30 to 60 times / sec. So win() will be called at every frame if possible. Since win() causes a transition to a new scene, if storyboard.purgeOnSceneChange = false then enterFrame gets called for each scene, leading to constant stream of win() calls and constant stream of transitions to the same scene "MSCREEN.mscreen" (this can't be good). OTOH if you set storyboard.purgeOnSceneChange = true then the previous scene, here the one with enterFrame that calls win(), is purged so win() will only get called once.
In conclusion, setting storyboard.purgeOnSceneChange = true may work but I think there is a problem with the logic you have there: a scene transition should not be called at every frame! If you are monitoring for some condition to declare a win, you should either remove the enterFrame callback in win() or ensure the condition is false after win() called once:
function scene:enterFrame(inEvent)
if (ball.x > display.contentWidth and failFlag == false) then
win();
Runtime:removeEventListener("enterFrame", scene);
end
end
With the guard variable it looks like this:
local winCalled = false
function win()
winCalled = true
...
end
function scene:enterFrame(inEvent)
if (ball.x > display.contentWidth and failFlag == false) then
win();
end
end
Related
I'm working on an app in lua using Corona SDK.
I'm trying to go from my buildscene2 to my mainbuildscene, and I am successfully calling the new scene, but the objects from buildscene2 are not being removed. I have added the code to remove the previous scene, in the mainbuildscene.lua file. I've tried adding it in scene:create() and in scene:show(). Neither worked. This exact method has worked for me when transitioning from other scenes in the same application. What I've done here is created a function that uses composer.gotoScene("mainbuildscene") and then called the function when a button is pressed. I have a bunch of random prints in there just to see if the code is being read. It is.
I've looked for other people having this problem. Plenty of people have experienced this, but none of their solutions have worked for me. I've tried moving the event listener for when the next button is pressed to inside the snapTo function and then back out. If the listener is outside, it gives me an error.
Also, I know that the problem is not when I'm calling the new scene, because I put in a random picture to test if the objects from that scene would show up, and they did, except they were overtop of the objects from buildscene2. I even tried adding a function into the second scene that, when called, destroys the first scene. Didn't work.
Unless I did something wrong, all my display objects in buildscene2 are added to the group.
Here's my buildscene2 file. This is the file I'm transitioning away from and trying to destroy.
local composer = require( "composer" )
local scene = composer.newScene()
-- -----------------------------------------------------------------------------------------------------------------
-- All code outside of the listener functions will only be executed ONCE unless "composer.removeScene()" is called.
-- -----------------------------------------------------------------------------------------------------------------
-- local forward references should go here
-- ------------------------------------------------------------------------------
-- "scene:create()"
function scene:create( event )
local sceneGroup = self.view
-- Initialize the scene here.
-- Example: add display objects to "sceneGroup", add touch listeners, etc.
local group = display.newGroup()
composer.removeScene("thirdscene")
----------EMPTY BOXES
local itemFrame1=display.newImageRect("images/selection-box.png", 200,200)
itemFrame1.x=140
itemFrame1.y=130
group:insert(itemFrame1)
local itemFrame2=display.newImageRect("images/selection-box.png", 200,200)
itemFrame2.x=387
itemFrame2.y=130
group:insert(itemFrame2)
local itemFrame3=display.newImageRect("images/selection-box.png", 200,200)
itemFrame3.x=635
itemFrame3.y=130
group:insert(itemFrame3)
---------------HEADS
local robotHead=display.newImageRect("images/robot-head.png", 195,180)
robotHead.x=137
robotHead.y=130
robotHead.rotation = 180
robotHead.headtype=1
local rabbitHead=display.newImageRect("images/rabbit-head.png", 200, 180)
rabbitHead.x=387
rabbitHead.y=130
rabbitHead.rotation = 180
rabbitHead.headtype=2
local dinoHead=display.newImageRect("images/dino-head.png", 210, 190)
dinoHead.x=665
dinoHead.y=140
dinoHead.rotation = 180
dinoHead.headtype=3
-------------------OTHER
local nextButton=display.newImageRect("images/donebutton.png", 130, 130)
nextButton.x=140
nextButton.y=710
group:insert(nextButton)
nextButton.isVisible = false
local nextText=display.newText("NEXT", 100,100, native.systemFontBold, 40)
nextText.x=140
nextText.y=710
nextText:setTextColor (0,0,0)
group:insert(nextText)
function snapTo (event)
function makeVisible (event)
nextButton.isVisible = true
end
if event.phase == "began" then
event.target.markX = event.target.x -- store x location of object
event.target.markY = event.target.y -- store y location of object
torsoPlaced=event.target.torsoName
end
function addHeadsToGroup (event)
group:insert(robotHead)
group:insert(rabbitHead)
group:insert(dinoHead)
end
if event.phase == "moved" then
local x = (event.x - event.xStart) + event.target.markX
local y = (event.y - event.yStart) + event.target.markY
event.target.x, event.target.y = x, y -- move object based on calculations above
function demolish1 (event)
robotHead.x = 1000
robotHead.y = 900
robotHead.width = 10
robotHead.length = 10
robotHead.isVisible = false
group:insert(robotHead)
local staticRobot = display.newImageRect("images/robot-head.png", 195, 180)
staticRobot.x=137
staticRobot.y=130
staticRobot.rotation = 180
group:insert(staticRobot)
end
function demolish2 (event)
rabbitHead.x = 1000
rabbitHead.y = 900
rabbitHead.width = 10
rabbitHead.length = 10
rabbitHead.isVisible = false
group:insert(rabbitHead)
local staticRabbit = display.newImageRect("images/rabbit-head.png", 200, 180)
staticRabbit.x=387
staticRabbit.y=130
staticRabbit.rotation = 180
group:insert(staticRabbit)
end
function demolish3 (event)
dinoHead.x = 1000
dinoHead.y = 900
dinoHead.width = 10
dinoHead.length = 10
dinoHead.isVisible = false
group:insert(dinoHead)
local staticDino = display.newImageRect("images/dino-head.png", 210, 190)
staticDino.x=665
staticDino.y=140
staticDino.rotation = 180
group:insert(staticDino)
end
if (event.target.headtype == 1) then
event.target.width = 220
event.target.height = 200
if event.target.y > 400 then
event.target.isVisible = false
local newHead1 = display.newImageRect("images/robot-head.png",220, 200)
newHead1.x=150
newHead1.y=500
newHead1.rotation = 270
end
addHeadsToGroup()
makeVisible()
demolish2()
demolish3()
end
if (event.target.headtype == 2) then
event.target.width = 340
event.target.height = 240
if event.target.y > 400 then
event.target.isVisible = false
local newHead2 = display.newImageRect("images/rabbit-head.png",340, 240)
newHead2.x=140
newHead2.y=500
newHead2.rotation = 270
end
addHeadsToGroup()
makeVisible()
demolish1()
demolish3()
end
if (event.target.headtype == 3) then
event.target.width = 280
event.target.height = 250
if event.target.y > 400 then
event.target.isVisible = false
local newHead3 = display.newImageRect("images/dino-head.png",280, 250)
newHead3.x=140
newHead3.y=540
newHead3.rotation = 270
end
addHeadsToGroup()
makeVisible()
demolish1()
demolish2()
end
local function nextButtonClicked2 (event)
if event.phase=="ended" then
function sceneChange (event)
composer.gotoScene("mainbuildscene")
print("jererefs")
end
print("egsijegij")
sceneChange()
end
end
nextButton:addEventListener("touch", nextButtonClicked2)
end
end--end of snapTo
robotHead:addEventListener( "touch", snapTo)
rabbitHead:addEventListener( "touch", snapTo)
dinoHead:addEventListener("touch", snapTo)
end -- end
-- "scene:show()"
function scene:show( event )
local sceneGroup = self.view
local phase = event.phase
if ( phase == "will" ) then
-- Called when the scene is still off screen (but is about to come on screen).
elseif ( phase == "did" ) then
-- Called when the scene is now on screen.
-- Insert code here to make the scene come alive.
-- Example: start timers, begin animation, play audio, etc.
end
end
-- "scene:hide()"
function scene:hide( event )
local sceneGroup = self.view
local phase = event.phase
if ( phase == "will" ) then
-- Called when the scene is on screen (but is about to go off screen).
-- Insert code here to "pause" the scene.
-- Example: stop timers, stop animation, stop audio, etc.
elseif ( phase == "did" ) then
-- Called immediately after scene goes off screen.
end
end
-- "scene:destroy()"
function scene:destroy( event )
local sceneGroup = self.view
-- Called prior to the removal of scene's view ("sceneGroup").
-- Insert code here to clean up the scene.
-- Example: remove display objects, save state, etc.
end
---------------------------------------------------------------------------------
-- Listener setup
scene:addEventListener( "create", scene )
scene:addEventListener( "show", scene )
scene:addEventListener( "hide", scene )
scene:addEventListener( "destroy", scene )
-- ------------------------------------------------------------------ -------------
return scene
Here's my mainbuildscene file, the file I'm trying to load. It's loading in but the previous scene isn't going away.
local composer = require( "composer" )
local scene = composer.newScene()
-- -----------------------------------------------------------------------------------------------------------------
-- All code outside of the listener functions will only be executed ONCE unless "composer.removeScene()" is called.
-- -----------------------------------------------------------------------------------------------------------------
-- local forward references should go here
-- -------------------------------------------------------------------------------
-- "scene:create()"
function scene:create( event )
local sceneGroup = self.view
function removeTheScene (event)
print("this is not working, unless it is...?")
composer.removeScene("buildscene2")
end
removeTheScene()
-- Initialize the scene here.
-- Example: add display objects to "sceneGroup", add touch listeners, etc.
local group=display.newGroup()
--
--
local picture=display.newImageRect("images/bee-torso.png", 100, 100)
picture.x=display.contentCenterX
picture.y=display.contentCenterY
group:insert(picture)
end
-- "scene:show()"
function scene:show( event )
local sceneGroup = self.view
local phase = event.phase
if ( phase == "will" ) then
-- Called when the scene is still off screen (but is about to come on screen).
elseif ( phase == "did" ) then
-- Called when the scene is now on screen.
-- Insert code here to make the scene come alive.
-- Example: start timers, begin animation, play audio, etc.
end
end
-- "scene:hide()"
function scene:hide( event )
local sceneGroup = self.view
local phase = event.phase
if ( phase == "will" ) then
-- Called when the scene is on screen (but is about to go off screen).
-- Insert code here to "pause" the scene.
-- Example: stop timers, stop animation, stop audio, etc.
elseif ( phase == "did" ) then
-- Called immediately after scene goes off screen.
end
end
-- "scene:destroy()"
function scene:destroy( event )
local sceneGroup = self.view
-- Called prior to the removal of scene's view ("sceneGroup").
-- Insert code here to clean up the scene.
-- Example: remove display objects, save state, etc.
end
-- -------------------------------------------------------------------------------
-- Listener setup
scene:addEventListener( "create", scene )
scene:addEventListener( "show", scene )
scene:addEventListener( "hide", scene )
scene:addEventListener( "destroy", scene )
-- -------------------------------------------------------------------------------
return scene
Please let me know what I did wrong or give me any advice you can. Thanks for reading.
You must add them to "sceneGroup". You are adding all the object into your own group but they should be added to the "sceneGroup".
In buildscene2.lua remove these lines:
local group = display.newGroup()
composer.removeScene("thirdscene")
And make sure you add all your display object into the sceneGroup, and not the group:
Change this group:insert(itemFrame1) to this sceneGroup:insert(itemFrame1) but for all your display objects and on all your scenes.
If you're planning to reuse the scene (i.e. being able to go back and forth) you don't need to use the removeScene because Corona handles all the display objects belonging to the scenes i.e. when you go from Scene A to Scene B Corona will automatically move away the display objects belonging to Scene A from the screen and move the display objects belonging to Scene B to the screen.
FuelGaugeArrow:rotate( 75 )
local FuelDecreaseAngle = 150 / FuelLevelMax
local function BurnFuel(event)
--transition.to( FuelGaugeArrow, { rotation = FuelGaugeArrow.rotation - FuelDecreaseAngle } )
FuelGaugeArrow.rotation = FuelGaugeArrow.rotation - FuelDecreaseAngle
FuelLevel = FuelLevel -1
if FuelLevel <=0 then
OutOfFuel()
end
end
local FuelBurn = timer.performWithDelay( 1000, BurnFuel , 0)
The FuelGaugeArrow rotation value is updated by another function (when the rocket picks up a fuel barrel, it increases the value):
local function ScrollExtras(event)
if FuelGaugeArrow.rotation + FuelIncreaseAngle <= 75 then
FuelGaugeArrow.rotation = FuelGaugeArrow.rotation + FuelIncreaseAngle
else
FuelGaugeArrow.rotation = 75
end
end
Runtime:addEventListener( "enterFrame", ScrollExtras )
This works fine in the code above, but when I use the commented-out code, i.e. when I try to use transition to move the arrow, it only works when there is no transition going on at that precise moment. The updated value is not picked up by the BurnFuel function when transition is in progress.
I'm sure there's a better way to do this, so please enlighten me.
You either have to cancel previous transition before starting new, or wait till previous ends before starting new. The transition.to() returns a reference to a transition object which can be paused etc. For example,
local fuelTrans
local function BurnFuel(event)
if fuelTrans ~= nil then
fuelTrans.cancel()
fuelTrans = transition.to( FuelGaugeArrow, ... )
end
...
end
The second method (wait till first complete) is probably not what you want, plus it involves setting onComplete parameter to a listener, but if you prefer that it is explained in transition.to.
the storyboard.gotoScene("facebook", "fade", 400) is not working if i tap the button, and i dont get any error messages in the terminal. What am i doing wrong ?
-- requires
display.setStatusBar( display.HiddenStatusBar )
_W = display.contentWidth; --Returns Screen Width
_H = display.contentHeight; --Returns Screen Height
local storyboard = require ("storyboard")
local scene = storyboard.newScene()
-- background
function scene:createScene(event)
local screenGroup = self.view
background = display.newImage("restart.png")
screenGroup:insert(background)
button = display.newImage("share2.png")
button.x = display.contentWidth / 2
button.y = display.contentHeight -400
end
function listener(event)
if event.phase == "began" then
print(event.name.." occurred")
storyboard.gotoScene("facebook", "fade", 400)
end
end
function scene:enterScene(event)
storyboard.purgeScene("game")
button:addEventListener( "tap", listener )
end
function scene:exitScene(event)
button:removeEventListener( "tap", listener )
end
function scene:destroyScene(event)
end
scene:addEventListener("createScene", scene)
scene:addEventListener("enterScene", scene)
scene:addEventListener("exitScene", scene)
scene:addEventListener("destroyScene", scene)
return scene
The "tap" event and "touch" events are different and they get different "phases" passed to the event handler. The way you have your event handler programmed, you're expecting "touch" events (began, ended, moved). The tap event doesn't really generate any phases, either you were tapped or not.
Either change these two lines:
button:addEventListener( "tap", listener )
button:removeEventListener( "tap", listener )
to:
button:addEventListener( "touch", listener )
and
button:removeEventListener( "touch", listener )
or you can change your listner to:
function listener(event)
print(event.name.." occurred")
storyboard.gotoScene("facebook", "fade", 400)
end
Try this:
storyboard.gotoScene("facebook", {effect = "fade", time=400})
Or:
local options =
{
effect = "fade",
time = 400,
}
storyboard.gotoScene("facebook", options)
http://docs.coronalabs.com/api/library/storyboard/gotoScene.html
It looks like corona does not like it when you call a Scene 'facebook' or renamed facebook to 'postmyscore' and it works
I had the same problem since Coronas last update. I fixed the problem by removing the if check for the event phase. Just comment out the event.phase check in your listener function:
function listener(event)
--if event.phase == "began" then
print(event.name.." occurred")
storyboard.gotoScene("facebook", "fade", 400)
end
facebook.lua is already integrated in the inbuild API , so it may generate the problem , more over you are using tap event , so in listener no need to check the phase or you may use touch event.
i am new to using loqSprite, i am trying to drag a loqSprite sprite object but its not getting done , however it calls its listener only once and then after neither its touch listner is getting called nor even it gives any error, the sprite is playing. Also i thought that might my drag/listener function might be buggy but when i tried the same dragging (movePen() function ) on the inbult corona's Sprite object it works fine. what i am missing i dont know . Can anyone please help me .... below is the code snippet. thanks
local function movePen(event)
local targetObj= event.target;
if event.phase == 'began' then
display.getCurrentStage():setFocus(targetObj);
targetObj.isFocus = true;
targetObj.y = event.y;
elseif event.phase == 'moved' then
targetObj.x = event.x;
targetObj.y = event.y;
elseif event.phase == 'ended' then
display.getCurrentStage():setFocus(nil);
targetObj.isFocus = false;
end
return true;
end --end of touch/move function
local spriteFactoryForPen = loqsprite.newFactory('penAnimation')
local penSpriteAnim = spriteFactoryForPen:newSpriteGroup('pen_write')
penSpriteAnim.x = 100
penSpriteAnim.y = 200
local function spriteEvent (e) --listener to play in loop
if(e.phase == "end") then
penSpriteAnim:play()
end
end -- end of sprit event function
penSpriteAnim:addEventListener("touch", movePen); -- adding listener to move pen object
penSpriteAnim:addEventListener("sprite", spriteEvent) -- adding listener to play in loop
penSpriteAnim:play('pen_write') -- playing pen Sprite
First of all there is no need to call penSpriteAnim:play() in loop. Because it will automatically play in loop untill you don't call the penSpriteAnim:pause() function.
For your touch listener, You should declare all the local variable on top of the page.
I am not sure about this, but hope this will work. Because lua is compiling top to bottom.
I have a "seed" object which has an instance method seed:fall() which is called by my update function (which runs every frame). I've got a "touch" event listener on it so the user can drag it around. When it's being dragged, however, it's still trying to fall, which makes the drag interaction glitchy.
I've added an instance variable to my seed "class" called seed.falling. The fall() function now checks that seed.falling is true before moving the seed down the screen. The next step is to set seed.falling to false when the drag starts, and then set it back to true when the drag stops. I can't figure out this last part though.
Any ideas anyone? Is there a "stop dragging" event i could set a listener for, to switch seed.falling back on? Is there a nicer way of achieving what i want?
physics.start()
physics.setGravity(0,1)
local dd = display.newRect(400,100,200,200)
physics.addBody(dd,"dynamic")
dd:addEventListener("touch", function(event)
if event.phase == "began" then
dd.bodyType = "static"
elseif event.phase == "moved" then
dd.x,dd.y = event.x,event.y
elseif event.phase == "ended" then
dd.bodyType = "dynamic"
end
end)
I think this case is what you want?
Just for the record, here's how i solved this.
Basically i have an attribute "seed.falling" which the seed:fall method checks before moving the seed. And i set that attribute to false if we're not at the "ended" phase of the drag event, which stops the seed falling.
function Seed:new(x,y)
print("Seed:new, x = " .. (x or nil) .. ", y = " .. (y or nil) )
local seed = display.newImage("seed_icon.png")
seed.x = x
seed.y = y
seed.name = 'seed'
seed.falling = true
function seed:fall()
if(self.falling) then
self.y = self.y + 1
end
end
function seed:drag(event)
seed.x = event.x
seed.y = event.y
if(event.phase == "ended") then
seed.falling = true
else
seed.falling = false
end
end
seed:addEventListener("touch", drag)
return seed
end
function drag(event)
seed = event.target
seed:drag(event)
end
It's not a very good solution i think as it leaves the seed stranded on the screen sometimes - possibly when you drag a seed over another falling seed.