Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Corona composer query
Started by IndieEnthusiast Feb 06 2019 06:07 AM

- - - - -
9 replies to this topic
[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

IndieEnthusiast

[GLOBAL: userInfoPane.html]
IndieEnthusiast
  • Contributor

  • 217 posts
  • Corona SDK

Last night, I created a credits button which when clicked goes to a scene showing credit panel

However when I clicked credit button twice, the scene was not refreshed it was showing all black

The issue is scene was not being reloaded. I added most of the code inside show:create code block.

What do I need to do to fix this problem?



[TOPIC: post.html]
#2

horacebury

[GLOBAL: userInfoPane.html]
horacebury
  • Corona Geek

  • 3,069 posts
  • Corona SDK

Can you paste a slimmed down version of the scene?

 

I'd suspect you're not adding the objects where they should be - that or they are being removed and you're forgetting to add them back again.



[TOPIC: post.html]
#3

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 25,461 posts
  • Enterprise

Resetting scenes is something you're responsible for.  There are basically two ways:

 

1. the lazy, easy route:  simply call composer.removeScene("yourSceneName") before you call composer.gotoScene("yourSceneName")

2. the more computationally efficient, but more code way: in your credit scene's scene:show() position all your objects to where they should start at and start any animations.

 

Composer is designed to cache scenes to save time by not loading all your images multiple times. As long as you can afford the memory, having the scene already loaded saves CPU time. Objects have to be positioned when they are loaded anyway, so re-positioning them when you go to the scene a second or third time, isn't an extra cost.

Your credits scene probably isn't a heavy scene so reloading it probably isn't a big impact and at the end of the day, programmer efficiency can be as important CPU efficiency. You may find simply removing the scene just before you go to it, the best thing for you.

 

Rob


  • horacebury likes this

[TOPIC: post.html]
#4

IndieEnthusiast

[GLOBAL: userInfoPane.html]
IndieEnthusiast
  • Contributor

  • 217 posts
  • Corona SDK

I am kind of frustrated with this :(

 

Let me provide a bit of code to make things easier, this is the credits.lua file

local composer = require( "composer" )
local scene = composer.newScene()

---------------------------------------------------------------------------------
-- BEGINNING OF YOUR IMPLEMENTATION
---------------------------------------------------------------------------------


-- Called when the scene's view does not exist:
function scene:create( event )
	local sceneGroup = self.view



  -- Add display object
   creditmessage = [[   This is the first venture from SillyGames...

   I would like to mention openart and the freeassets...

   credit goes to Kenny for his assets...

   I want to thank my family and my wife for the support...

   Please support this game !! Thanks...]]



  credittext = display.newText( creditmessage, display.contentCenterX, 600, "Raleway-Bold.ttf", 25 )
  credittext:setFillColor( 0, 0.5, 1 )


  scrollSpeed = -1
	print( "\n1: create event")

end

function scene:show( event )

	local phase = event.phase

  	if "did" == phase then
      print( "1: show event, phase did" )
    end


    local function move(event)
       -- move backgrounds to the left by scrollSpeed, default is 2
       credittext.y = credittext.y + scrollSpeed

       -- Set up listeners so when backgrounds hits a certain point off the screen,
       -- move the background to the right off screen
       if (credittext.y) < -150 then
         Runtime:removeEventListener("enterFrame", move )
         print("out of screen")
         exitmenubutton = display.newImage("Assets/button/mainmenuexit.png")
         exitmenubutton.width = 200; exitmenubutton.height=50
         exitmenubutton.x = 200
         exitmenubutton.y = 400

         local function exitMainMenuTouchListener( event )

               if ( event.phase == "began" ) then
                 print( "object touched = " .. tostring(event.target) ) -- "event.target" is the touched object
                 exitmenubutton.alpha = 0.5
                 composer.gotoScene( "mainmenu" )

               end -- if end

          return true
         end -- local function end

         exitmenubutton:addEventListener( "touch", exitMainMenuTouchListener ) -- Add a "touch" listener to the object



       end -- if end

    end -- local function move event end

    -- Create a runtime event to move backgrounds
    Runtime:addEventListener( "enterFrame", move )





end -- function scene:show( event )


function scene:hide( event )

  local phase = event.phase

  	if "will" == phase then

  	end

end

function scene:destroy( event )
	print( "((destroying scene 1's view))" )
end

---------------------------------------------------------------------------------

-- Listener setup
scene:addEventListener( "create", scene )
scene:addEventListener( "show", scene )
scene:addEventListener( "hide", scene )
scene:addEventListener( "destroy", scene )

---------------------------------------------------------------------------------

return scene

This is the mainmenu.lua file :

-- =============================================================
-- Copyright Roaming Gamer, LLC. 2008-2017 (All Rights Reserved)
-- =============================================================
--  scenes/splash.lua
-- =============================================================
local composer       = require( "composer" )
local scene          = composer.newScene()

composer.removeScene( "credits" )

----------------------------------------------------------------------
-- scene:create( event ) - Called on first scene open ONLY (unless
-- the scene has been manually or automatically destroyed.)
----------------------------------------------------------------------
function scene:create( event )
  local sceneGroup = self.view

  _G.menubackground = display.newImage( "Assets/Menu/menu.png" )
  menubackground.x = display.contentCenterX - 80
  menubackground.y = display.contentCenterY

  _G.newgamebutton = display.newImage("Assets/button/Newgame.png")
  newgamebutton.width = 200; newgamebutton.height=50
  newgamebutton.x = display.contentCenterX
  newgamebutton.y = display.contentCenterY

  _G.creditbutton = display.newImage("Assets/button/credits.png")
  creditbutton.width = 200; newgamebutton.height=50
  creditbutton.x = display.contentCenterX
  creditbutton.y = display.contentCenterY+150

  sceneGroup:insert( menubackground )
  sceneGroup:insert( newgamebutton )
  sceneGroup:insert( creditbutton)

local function newgameButtonTouchListener( event )

  if ( event.phase == "began" ) then

    --composer.gotoScene( "scene1" )
    -- Code executed when the button is touched
    print( "object touched = " .. tostring(event.target) ) -- "event.target" is the touched object
    newgamebutton.alpha = 0.5

  elseif ( event.phase == "moved" ) then
    -- Code executed when the touch is moved over the object
    print( "touch location in content coordinates = " .. event.x .. "," .. event.y )
  elseif ( event.phase == "ended" ) then
    -- Code executed when the touch lifts off the object
    print( "touch ended on object " .. tostring(event.target) )
    newgamebutton.alpha = 1

    local options = {
      effect = "fade",
      time = 800 }
  composer.gotoScene( "scene1", options )
  end
  return true
end
newgamebutton:addEventListener( "touch", newgameButtonTouchListener ) -- Add a "touch" listener to the object

local function creditButtonTouchListener( event )

  if ( event.phase == "began" ) then

    --composer.gotoScene( "scene1" )
    -- Code executed when the button is touched
    print( "object touched = " .. tostring(event.target) ) -- "event.target" is the touched object
    creditbutton.alpha = 0.5

  elseif ( event.phase == "moved" ) then
    -- Code executed when the touch is moved over the object
    print( "touch location in content coordinates = " .. event.x .. "," .. event.y )
  elseif ( event.phase == "ended" ) then
    -- Code executed when the touch lifts off the object
    print( "touch ended on object " .. tostring(event.target) )
    creditbutton.alpha = 1

    local options = {
      effect = "fade",
      time = 800 }
  composer.gotoScene( "credits", options )
  end
  return true
end
creditbutton:addEventListener( "touch", creditButtonTouchListener ) -- Add a "touch" listener to the object

end

----------------------------------------------------------------------
--          Custom Scene Functions/Methods
----------------------------------------------------------------------


----------------------------------------------------------------------
-- scene:willShow( event ) - Replaces the scene:show() method.  This
-- method is called during the "will" phase of scene:show().
----------------------------------------------------------------------
function scene:willShow( event )
   local sceneGroup = self.view
end

----------------------------------------------------------------------
-- scene:didShow( event ) - Replaces the scene:show() method.  This
-- method is called during the "did" phase of scene:show().
----------------------------------------------------------------------
function scene:didShow( event )
   local sceneGroup = self.view
end

----------------------------------------------------------------------
-- scene:willHide( event ) - Replaces the scene:hide() method.  This
-- method is called during the "will" phase of scene:hide().
----------------------------------------------------------------------
function scene:willHide( event )
   local sceneGroup = self.view
end

----------------------------------------------------------------------
-- scene:didHide( event ) - Replaces the scene:hide() method.  This
-- method is called during the "did" phase of scene:hide().
----------------------------------------------------------------------
function scene:didHide( event )
   local sceneGroup = self.view
end
----------------------------------------------------------------------
-- scene:destroy( event ) - Called automatically by Composer scene library
-- to destroy the contents of the scene (based on settings and memory constraints):
-- https://docs.coronalabs.com/daily/api/library/composer/recycleOnSceneChange.html
--
-- Also called if you manually call composer.removeScene()
-- https://docs.coronalabs.com/daily/api/library/composer/removeScene.html
----------------------------------------------------------------------
function scene:destroy( event )
   local sceneGroup = self.view
end


---------------------------------------------------------------------------------
-- Scene Dispatch Events, Etc. - Generally Do Not Touch Below This Line
---------------------------------------------------------------------------------

-- This code splits the "show" event into two separate events: willShow and didShow
-- for ease of coding above.
function scene:show( event )
   local sceneGroup  = self.view
   local willDid  = event.phase
   if( willDid == "will" ) then
      self:willShow( event )
   elseif( willDid == "did" ) then
      self:didShow( event )
   end
end

-- This code splits the "hide" event into two separate events: willHide and didHide
-- for ease of coding above.

scene:addEventListener( "create", scene )
scene:addEventListener( "show", scene )
scene:addEventListener( "hide", scene )
scene:addEventListener( "destroy", scene )
---------------------------------------------------------------------------------
return scene

Please note once the credit text disappears, then only the exit to main menu appears

 

What Issue am I seeing?

 

- The exit to main menu button shows up in the menu scene

- When I press credit button again, nothing shows, all black screen

 

 

Video link : https://screencast-o-matic.com/watch/cqn1ro3Gmw



[TOPIC: post.html]
#5

XeduR @Spyric

[GLOBAL: userInfoPane.html]
XeduR @Spyric
  • Contributor

  • 616 posts
  • Corona SDK

I just read the credits.lua quick and you have a few big issues here.

For starters,
1) You need to insert display objects into display groups or directly to the scene group, so that they will be cleared when you transition to other scenes.
2) Just a preference, but you should consider using locals over globals
3) Since you have globals AND you don't remove them and set them to nil manually, they will probably resume their lives until the end of time once you leave the scene. When you return to the scene, you'll create a brand new set of globals overwriting the previous ones, etc.



[TOPIC: post.html]
#6

ldurniat

[GLOBAL: userInfoPane.html]
ldurniat
  • Contributor

  • 389 posts
  • Corona SDK

Hi @IndieEnthusiast,

 

I agree with advices of @XeduR @Spyric and @Rob Miracle but that is not all

 

1. Did you notice text 'out of screen' appear twice in console? The scene:show is called twice so I think you create two different 'move' functions with the same name and call them both. Try replace 

function scene:show( event )

local phase = event.phase

  if "did" == phase then
    print( "1: show event, phase did" )
  end

  local function move(event)
  ...   
  end -- local function move event end

  -- Create a runtime event to move backgrounds
  Runtime:addEventListener( "enterFrame", move )
end

with 

function scene:show( event )

local phase = event.phase

  if "did" == phase then
    print( "1: show event, phase did" )
    local function move(event)
    ...   
    end -- local function move event end

    -- Create a runtime event to move backgrounds
    Runtime:addEventListener( "enterFrame", move )
  end
end 

2. composer.removeScene( "credits" ) in mainmenu.lua is called only once when you load scene first time that is why it don't remove credits scene.

 

Read more 

 

Have a nice day:)

 

ldurniat



[TOPIC: post.html]
#7

IndieEnthusiast

[GLOBAL: userInfoPane.html]
IndieEnthusiast
  • Contributor

  • 217 posts
  • Corona SDK

Alright I will go through the changes and check what happens, thanks for the input guys. Sorry for pestering you guys with silly questions.



[TOPIC: post.html]
#8

IndieEnthusiast

[GLOBAL: userInfoPane.html]
IndieEnthusiast
  • Contributor

  • 217 posts
  • Corona SDK

Thanks Idurniat, I fixed the issue with double out of screen issue. Followed your instructions, I think the whole block was being called twice. Thanks!



[TOPIC: post.html]
#9

IndieEnthusiast

[GLOBAL: userInfoPane.html]
IndieEnthusiast
  • Contributor

  • 217 posts
  • Corona SDK

The issue was solved by adding the exit button to the "sceneGroup". From this I learned two valuables :

  • Avoid globals where possible
  • add all display objects to the sceneGroup
  • the scene creation objects should not be included in scene:show code block

My encountering this issues, I am learning quite a lot and finding the correct way to solve it. Thanks.



[TOPIC: post.html]
#10

XeduR @Spyric

[GLOBAL: userInfoPane.html]
XeduR @Spyric
  • Contributor

  • 616 posts
  • Corona SDK

Locals perform better than globals generally, but there are times when using globals can be the better option. It really depends on the use case. Still, you need to exercise caution with globals.

I personally include 99.99% of my code in scenes within the scene:create function. The other functions, like scene:show have different phases, e.g. "will" and "did", so you'd need to place your code inside if statements to run them only once during a specific phase.




[topic_controls]
[/topic_controls]