Corona Storyboard场景的问题

时间:2014-05-04 13:43:43

标签: lua corona corona-storyboard

我正在尝试制作纸牌游戏..我正在创建一个场景,制作背景,并添加一个图像,当玩家触摸时,他/她将被转移到下一个场景。

singlePlayer场景:

local storyboard = require("storyboard")
local singlePlayer = storyboard.newScene()

local card1,card2,card3

function singlePlayer:createScene(event )
    local group = self.view
    -- body
    local bg = display.newImage("bg.png")
    bg.x = 100 ; bg.y = 50
    group:insert(bg)
end

function singlePlayer:enterScene( event )
    local group = self.view
    local count = math.random(3)

    local storyboard = require("storyboard")
    local singlePlayer = storyboard.newScene()

    local card1,card2,card3

    function singlePlayer:createScene(event )
        local group = self.view
        -- body
        local bg = display.newImage("bg.png")
        bg.x = 100 ; bg.y = 50
        group:insert(bg)
    end

    function singlePlayer:enterScene( event )
        local group = self.view
        local count = math.random(3)

        if(count == 1) then
            card1 = display.newImage("attack.png")
            card1.x = 50 ; card1.y = 150
            group:insert(card1)
        else
            card1 = display.newImage("ability.png")
            card1.x = 50 ; card1.y = 150
            group:insert(card1)
        end

        function card1:touch(event )
            print("ok")
            if(event.phase == "ended") then
                storyboard.gotoScene("opponent_scene")
            else
            end
            -- body
        end

        card1:addEventListener("touch",card1)

        -- body
    end

    function singlePlayer:exitScene(event)
        local group = self.view
        card1:removeEventListener("touch",card1)
    end

    singlePlayer:addEventListener("createScene",singlePlayer)

    singlePlayer:addEventListener("enterScene",singlePlayer)

    singlePlayer:addEventListener("exitScene",singlePlayer)

    return singlePlayer

对手场景:

local storyboard = require("storyboard")
local opponent_scene = storyboard.newScene()

function opponent_scene:createScene(event )
    print("opponent_scene created")
    -- body
end

function opponent_scene:enterScene(event )
    print("opponent_scene enter")
    local group  = self.view
    storyboard.removeScene("judge")
    local text = display.newText("Opponent's turn",150,200)
    storyboard.gotoScene("judge")
    -- body
end

function opponent_scene:exitScene(event )
    -- body
    local group = self.view
end

opponent_scene:addEventListener("createScene",opponent_scene)

opponent_scene:addEventListener("enterScene",opponent_scene)

opponent_scene:addEventListener("exitScene",opponent_scene)

return opponent_scene

法官现场:

local storyboard = require("storyboard")

local judge = storyboard.newScene()

function judge:createScene(event )
    local group = self.view
    local bg = display.newImage("destiny.png")
    storyboard.removeScene("opponent_scene")
    storyboard.gotoScene("singlePlayer")
    -- body
end

judge:addEventListener("createScene",judge)

return judge

有人会向我解释这些场景是怎么回事吗?

我想要的只是让游戏等待玩家的输入(触摸卡片)

在点击图标两次后,故事板被带到对手场景,它只是在屏幕上显示文字"对手的回合"。我想要做的是短暂地显示文本,然后将场景带到播放器场景

2 个答案:

答案 0 :(得分:0)

使用timer.delay将场景更改代码移动到延迟函数中。以下是我在短时间内更改屏幕后所做的操作。

local function onSceneTouch( self, event )
    if event.phase == "began" then

        -- write all your other code here

        -- function to change screen
        function myClosure()
           storyboard.gotoScene("opponent_scene")
        end

        -- Delay the call of closure function by 2 second (2000 milliseconds)
        timer.performWithDelay( 2000, myClosure, 1 )

    end
end

然后在触摸屏幕时调用onSceneTouch功能

答案 1 :(得分:0)

单人玩家输入代码是错误的,因为你不能拥有两个具有相同名称的全局函数,这种方法不是正确的。为什么在输入框架内调用创建场景。假设您所做的事情是正确的,那么您应该将事件监听器添加到您在entercene中创建的函数中,如前所述

singlePlayer:addEventListener("createScene",singlePlayer)

singlePlayer:addEventListener("enterScene",singlePlayer)

singlePlayer:addEventListener("exitScene",singlePlayer)

首先,请阅读故事板的文档并查看示例。

http://docs.coronalabs.com/api/library/storyboard/

但我请求你使用composer而不是storyboard:http://docs.coronalabs.com/api/library/composer/index.html