basic composer question

Hi,

I am learning corona to make ebooks and I am trying to learn composer to do it. I got stuck right out of the gate and I can’t seem to find anything out there.

this is main.lua

local composer = require( “composer” )
–composer.Gesture = require(“dmc_gesture”)
–composer.MultiTouch = require(“dmc_multiTouch”)
–system.activate(“multitouch”)

display.setStatusBar( display.HiddenStatusBar )

composer.gotoScene( “scenetemplate”)

and this is my scenetemplate.lua ( which I am using as my scene for test)

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 background = display.newImageRect( “background.png”, 2048, 1536 )
    background.x = 1022
    background.y = 766
    
    sceneGroup:insert(background)

    local drawer_merged = display.newImageRect( “drawer_merged.png”, 606, 911 )
    drawer_merged.x = 2254
    drawer_merged.y = 766
    
    sceneGroup:insert(drawer_merged)
end

– “scene:show()”
function scene:show( event )

    local sceneGroup = self.view
    local phase = event.phase

    if ( phase == “will” ) then
        transition.to( drawer_merged, { time=1000, x=1796, transition=easing.outBounce} )
    
    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

The objects draw to the screen but the animation will not run. What I am doing wrong here.

Hi doozystudios,

I have not used composer yet but from looking at the documentation, it looks like you are calling it early.

If you are referring to the transition.to when you say animation, try moving it here:

-- "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. transition.to( drawer\_merged, { time=1000, x=1796, transition=easing.outBounce} ) end end

The “did” phase is when you should start animations, timers, audio, etc.

Again, I haven’t used composer yet so I’m just making a guess based on the documentation. Let us know if this works.

Eric

Hi Eric,

Thanks for the reply. It doesn’t work when I move it there.

Does the transtion.to know that the x value in the brackets refers to the drawer_merged.x?

The transition.to normally knows which x value to read.

Ah, I think I see the problem. You are making drawer_merged a local variable in your “function scene:create( event )”. Try making it a local forward reference and put it above your “function scene:create( event )” where it says “-- local forward references should go here”.

Like this:

-- local forward references should go here local drawer\_merged -- ------------------------------------------------------------------------------- -- "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 background = display.newImageRect( "background.png", 2048, 1536 ) background.x = 1022 background.y = 766 sceneGroup:insert(background) drawer\_merged = display.newImageRect( "drawer\_merged.png", 606, 911 ) drawer\_merged.x = 2254 drawer\_merged.y = 766 sceneGroup:insert(drawer\_merged) end

The reason for this is because you had it local to the create scene function. When it is local inside a function, it can only be accessed inside of the function. This is how scope works with Lua. For more info on scope watch this https://www.youtube.com/watch?v=2ATlcGP2zMY

Scope is something that may seem small but if you learn it early, you save yourself a lot of headaches.

Hope this helps.

Thank you,

I realize I have a lot to learn here.Unfortunetly it did not work :frowning: . Very frustrating.

There isn’t much on composer here but i want to use it to manage the scenes.

Hi,

So I placed the transition.to  code in the  scene:create  function and it worked. This seems very weird since an animation is not a creation but a modification of a scene object. I am more confused than relieved.

Are you sure that you did as blondiemc9 adviced? Moved the definition of drawer_merged and created it in the scene:create like:

[lua]drawer_merged = display.newImageRect( “drawer_merged.png”, 606, 911 )[/lua]

and not as a function scoped local:

[lua]local drawer_merged = display.newImageRect( “drawer_merged.png”, 606, 911 )[/lua]

?

Thank you Sape and blondiemc9,

You are correct. I totally missed that and now it works correctly. thank you very much!!

Hi doozystudios,

I have not used composer yet but from looking at the documentation, it looks like you are calling it early.

If you are referring to the transition.to when you say animation, try moving it here:

-- "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. transition.to( drawer\_merged, { time=1000, x=1796, transition=easing.outBounce} ) end end

The “did” phase is when you should start animations, timers, audio, etc.

Again, I haven’t used composer yet so I’m just making a guess based on the documentation. Let us know if this works.

Eric

Hi Eric,

Thanks for the reply. It doesn’t work when I move it there.

Does the transtion.to know that the x value in the brackets refers to the drawer_merged.x?

The transition.to normally knows which x value to read.

Ah, I think I see the problem. You are making drawer_merged a local variable in your “function scene:create( event )”. Try making it a local forward reference and put it above your “function scene:create( event )” where it says “-- local forward references should go here”.

Like this:

-- local forward references should go here local drawer\_merged -- ------------------------------------------------------------------------------- -- "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 background = display.newImageRect( "background.png", 2048, 1536 ) background.x = 1022 background.y = 766 sceneGroup:insert(background) drawer\_merged = display.newImageRect( "drawer\_merged.png", 606, 911 ) drawer\_merged.x = 2254 drawer\_merged.y = 766 sceneGroup:insert(drawer\_merged) end

The reason for this is because you had it local to the create scene function. When it is local inside a function, it can only be accessed inside of the function. This is how scope works with Lua. For more info on scope watch this https://www.youtube.com/watch?v=2ATlcGP2zMY

Scope is something that may seem small but if you learn it early, you save yourself a lot of headaches.

Hope this helps.

Thank you,

I realize I have a lot to learn here.Unfortunetly it did not work :frowning: . Very frustrating.

There isn’t much on composer here but i want to use it to manage the scenes.

Hi,

So I placed the transition.to  code in the  scene:create  function and it worked. This seems very weird since an animation is not a creation but a modification of a scene object. I am more confused than relieved.

Are you sure that you did as blondiemc9 adviced? Moved the definition of drawer_merged and created it in the scene:create like:

[lua]drawer_merged = display.newImageRect( “drawer_merged.png”, 606, 911 )[/lua]

and not as a function scoped local:

[lua]local drawer_merged = display.newImageRect( “drawer_merged.png”, 606, 911 )[/lua]

?

Thank you Sape and blondiemc9,

You are correct. I totally missed that and now it works correctly. thank you very much!!