首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Corona SDK游戏场景

Corona SDK游戏场景
EN

Stack Overflow用户
提问于 2016-01-08 00:07:08
回答 1查看 309关注 0票数 0

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

我尝试的代码如下:

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

发布于 2016-01-08 06:16:24

在Corona中,场景文件的扩展名需要为".lua“。我觉得"csscene“跟可可有关。

下面的代码将起作用。

备注:

请确保文件以矩形结尾(,scene1.luascene2.lua).

  • start_button现在是一个矩形,但您可以很容易地将其更改为image.
  • start_button is
    • .lua )。这是场景将使用的组,即,当您更改场景时,sceneGroup中的所有对象都将得到正确处理。
    • 我以不同的方式添加eventListener,请阅读文档以获取更多信息:

main.lua:

代码语言:javascript
复制
local composer = require( "composer" )
composer.gotoScene("scene1")

scene1.lua:

代码语言:javascript
复制
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:

代码语言:javascript
复制
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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34659813

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档