【问题标题】:Corona SDK game scenesCorona SDK 游戏场景
【发布时间】:2016-04-12 02:50:54
【问题描述】:

我查看了许多其他问题和教程,但遗憾的是,没有结果。 我正在尝试使用事件侦听器从场景 1 转到场景 2,并且我正在复制项目中给出的代码。我的按钮和工作按钮之间的唯一区别是我在 lua 文件中声明了我的按钮,并且它在 scene1.ccscene 中不存在 我是否必须将图像作为对象放在 scene1.csscene 中才能将其用作按钮?

我尝试的代码:

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
local start_button = display.newImage("START.png")    --start button

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


-- "scene:create()"
function scene:create( event )

    local sceneGroup = self.view

    -- Initialize the scene here.
    -- Example: add display objects to "sceneGroup", add touch listeners, etc.
    start_button.x=200
    start_button.y=150
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.
        function nextScene:touch ( event )
                local phase = event.phase
                if "ended" == phase then
                    composer.gotoScene( "scene2", { effect = "fade", time = 300 } )
                end
            end
        start_button:addEventListener( "touch", nextScene )
    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.
        if nextSceneButton then
            start_button:removeEventListener( "touch", nextScene )
        end
    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

【问题讨论】:

    标签: events lua coronasdk scene


    【解决方案1】:

    场景文件的扩展名需要在 Corona 中为“.lua”。我认为“csscene”与可可有关。

    下面的代码将起作用。

    注意事项:

    • 确保文件以 .lua 结尾(main.luascene1.luascene2.lua)。
    • start_button 现在是一个矩形,但您可以轻松地将其更改为图像。
    • start_button 被添加到场景组中。这是场景将使用的组,即当您更改场景时,sceneGroup 中的所有对象都将得到正确处理。
    • 我以不同的方式添加eventListener,请阅读文档以获取更多信息:https://docs.coronalabs.com/api/event/touch/index.html

    main.lua:

    local composer = require( "composer" )
    composer.gotoScene("scene1")
    

    scene1.lua:

    local composer = require( "composer" )
    
    local scene = composer.newScene()
    
    -- VARIABLES
    local start_button
    
    -- EVENTS
    local function onTouchStartButton( event )
        if event.phase == "ended" then
            composer.gotoScene( "scene2" )
        end
    end
    
    function scene:create( event )
        local sceneGroup = self.view
    
        -- Create the start_button
        start_button = display.newRect( 100, 100, 100, 100 )
    
        -- Make sure you insert the button into the sceneGroup
        sceneGroup:insert( start_button )
    
        -- Set position of start_button
        start_button.x=200
        start_button.y=150
    
        -- Add the event listener in the "create" stage instead of the show state
        -- Now the start_button is in the scene_group so there is no need to remove
        -- the touch listener under the "destroy" stag
        start_button:addEventListener( "touch", onTouchStartButton )
    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
        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
    

    scene2.lua:

    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.
    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).
            print("Now we are in scene2.lua!")
        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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-07-31
      • 2015-01-07
      • 1970-01-01
      • 1970-01-01
      • 2014-05-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多