2016-10-18 15 views
1

私はスタート画面を持っているゲームを構築しています。スタートボタンをクリックすると、最初のレベルになります。いくつかのことが起こり、その後私は2番目のレベルに移動します。LUA:シーンが作成されていません。なにが問題ですか?

しかし、私は問題に遭遇しており、解決策を見つけることができないようです。 私はスタートボタンをクリックすると、何かをプリントアウトするので、作成機能に詰まっているようです。私はあなたが作成機能に何かを配置する必要はなく、すべてをSHOW FUNCTIONに置く必要があると言われました。私は誤解しましたか?

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 
    } 
) 

私はそれがここに であると私は問題に実行しているところである最初のレベルに私を取る必要があり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 
+0

あなたがあなたの機能に抱かれた後に何が印刷されるかを知ることは有益だろうと思いませんか? – Piglet

答えて

1

さて、あなたはscene:addEventListener("show", scene)あるべきリスナーscene:addEventListener("enter", scene)を追加する(変更を示すために入力します)

あなたのリスナーであっても、解雇されていません。

Composerは、次のイベントを使用します。create, destroy, show, hide

関連する問題