LUA:未创建场景.怎么了?



我正在创造一款拥有开始屏幕并点击开始按钮的游戏,它将带我进入第一个关卡。有些事情发生了,然后我会被带到第二个层次。

然而,我遇到了问题,似乎找不到解决方案。当我点击开始按钮时,它似乎卡在创建函数中,因为它打印出了一些东西。有人告诉我,你不需要在create函数中放置任何东西,只需要将所有东西放在SHOW函数中。我被误导了吗?

START SCENE

local composer = require("composer")
local widget = require("widget")
local options = {effect = "fade", time = 800}
local startBtn;
local function start(event)
    -- load first scene
    composer.gotoScene( "level1", options);
    startBtn:removeSelf();
    print("Start Game")
end
startBtn = widget.newButton(
    {
    left = 75,
    top = 100,
    id = "startBtn",
    label = "Start",
    onEvent = start
    }
)

当我点击开始按钮,它应该带我到这里的第一层这就是我遇到问题的地方。

local composer = require( "composer" );
local scene = composer.newScene();
local widget = require ("widget");
function scene:create( event )
    local sceneGroup = self.view;
end
function scene:show( event )
    local sceneGroup = self.view
    local phase = event.phase
    local params = event.params;
    print(params);
    if (phase == "will") then
        print("Will")
    elseif (phase == "did") then
        print("Did")
        local bg = display.newImage ("bg.png",  
        display.contentCenterX, display.contentCenterY);

        ------- ALEX KIDD  ---------------------------------
        local options =
        {
            frames = {
                { x = 1, y = 2, width = 16, height = 25}, --frame 1       
                { x = 18, y = 2, width = 16, height = 25}, --frame 2        
                { x = 35, y = 2, width = 16, height = 25}, --frame 3        
                { x = 52, y = 2, width = 16, height = 25}, --frame 4
                { x = 1, y = 54, width = 16, height = 24},  --ready1
                { x = 19, y = 54, width = 16, height = 24}, --ready2
                { x = 37, y = 54, width = 29, height = 24}, -- rock
                { x = 67, y = 54, width = 33, height = 24}, -- scissor
                { x = 101, y = 54, width = 33, height = 24},  -- paper       
            }
        };
        local sheet = graphics.newImageSheet( "kidd.png", options );

        -- Create animation sequence for animation
        local seqData = {
            {name = "normal", start=1 , count = 4, time = 800},
            {name = "faster", frames={1,2,3,4}, time = 400},
            {name = "shake", frames={5,6}, time = 500},
            {name = "rock", frames={7}},
            {name = "paper", frames={9}},
            {name = "scissor", frames={8}},
        }
        local alex = display.newSprite (sheet, seqData);
        alex.x = display.contentCenterX-100;
        alex.y = display.contentCenterY+83;
        alex.anchorX = 1;
        alex.anchorY = 1;

        ---------- JANKEN ---------------------------------
        local jankenOpt =
        {
            frames = {
                {x= 154, y= 13, width= 39, height= 48 }, -- shake1
                {x= 195, y= 13, width= 39, height= 48 }, -- shake2
                {x= 236, y= 13, width= 32, height= 48 }, -- set
                {x= 270, y= 13, width= 16, height= 48 }, --r/p/s
                {x= 287, y= 13, width= 16, height= 48 }, --r/p/s
                {x= 305, y= 13, width= 15, height= 48 }, --r/p/s
                {x= 69, y= 13, width= 41, height= 48 }, --flap1
                {x= 110, y= 13, width= 40, height= 48 }, --flap2
            }
        };
        local jankenSheet = graphics.newImageSheet( "chars.png", jankenOpt );
        -- Create animation sequence janken
        local seqDataJanken = {
            {name = "flap", frames={7,8}, time = 500},
            {name = "shake", frames={1,2}, time = 500},    
            {name = "set", frames={3}, time = 10, loopCount=1},        
        }
        local janken = display.newSprite (jankenSheet, seqDataJanken);
        janken.x = display.contentCenterX+100;
        janken.y = display.contentCenterY+83;
        janken.anchorX = 1;
        janken.anchorY = 1;

        -------------- button setup
        local btnOpt =
        {
            frames = {
                { x = 3, y = 2, width=70, height = 22}, --frame 1       
                { x = 78, y = 2, width=70, height = 22}, --frame 2        
            }
        };
        local buttonSheet = graphics.newImageSheet( "button.png", btnOpt );
        local function foo (wow) 
            alex:play();        
            janken:play();
        end
        local function bar (wow)
            alex:pause();    
            janken:pause();    
            --alex:setFrame(2);     --keep it at this frame 
            -- NEXT: generate more buttons
        end
        -- Function to handle button event
        local function go( event )
            -- event.target is the button widget
            print (event.phase, event.target.id)
            transition.to(alex, {time=2000, x=170, onStart = foo, onComplete = bar})
        end
        local btnGo = widget.newButton(
            {
                x = 200,
                y = 20,    
                id = "btnGo",
                label = "Go!",
                labelColor = { default={ 0, 0, 0 }, over={ 0, 0, 0 } },    
                sheet = buttonSheet,
                defaultFrame = 1,
                overFrame = 2,
                onPress = go,
            }
        );
        -------------- play buttons
        local function shoot (buttonID) 
            local randomHand = math.random(4,6);
            -- position Janken and draw his hands
            janken:setSequence("set"); 
            hand = display.newImage (jankenSheet, randomHand, display.contentCenterX+61, display.contentCenterY+60);
            if (buttonID == "btnRock") then
                alex:setSequence("rock");  -- just show rock for now
            elseif (buttonID == "btnScissor") then  
                alex:setSequence("scissor");  -- just show rock for now
            else
                alex:setSequence("paper");  -- just show rock for now
            end
        end

        local function play (event)
            if (event.phase == "ended") then
                alex:setSequence ("shake");
                alex:play();
                janken:setSequence("shake");
                janken:play(); 

                local t = timer.performWithDelay (1500, function() shoot(event.target.id) end, 1);

                print (event.target.id);   -- which button was it?
            end
        end

        local btnRock = widget.newButton(
            {
                x = 80,
                y = 20,    
                id = "btnRock",
                label = "Rock",
                labelColor = { default={ 0, 0, 0 }, over={ 0, 0, 0 } },    
                sheet = buttonSheet,
                defaultFrame = 1,
                overFrame = 2,
                onEvent = play,
            }
        );
        local btnPaper = widget.newButton(
            {
                x = 80,
                y = 50,    
                id = "btnPaper",
                label = "Paper",
                labelColor = { default={ 0, 0, 0 }, over={ 0, 0, 0 } },    
                sheet = buttonSheet,
                defaultFrame = 1,
                overFrame = 2,
                onEvent = play,
            }
        );
        local btnScissor = widget.newButton(
            {
                x = 80,
                y = 80,    
                id = "btnScissor",
                label = "Scissor",
                labelColor = { default={ 0, 0, 0 }, over={ 0, 0, 0 } },    
                sheet = buttonSheet,
                defaultFrame = 1,
                overFrame = 2,
                onEvent = play,
            }
        );
        local scoreAlex = display.newText ( {text="Alex: 0", x=230, y=60, fontSize=20});
        scoreAlex:setFillColor (0,0,0);
        scoreAlex.anchorX = 1;
    end
end
function scene:hide( event )
    local sceneGroup = self.view
    local phase = event.phase
    if ( phase == "will" ) then
        transition.cancel(enemy);
    elseif ( phase == "did" ) then
    end
end

scene:addEventListener( "create", scene )
scene:addEventListener( "enter", scene )
scene:addEventListener( "hide", scene )
return scene

您要添加的监听器scene:addEventListener( "enter", scene )应该是scene:addEventListener( "show", scene ) (Change enter to show)

您的监听器甚至没有被触发,因为它没有添加正确的名称。

Composer使用以下事件:create, destroy, show, hide。

最新更新