2012-06-28 61 views
0

我想在Corona中使用StoryBoard API。但是,我的按鈕觸摸事件根本不起作用。你能看看我的代碼並幫助我嗎?Corona中的StoryBoard API

main.lua:

 local storyboard = require "storyboard" 

    storyboard.gotoScene("scene1", "fade", 500) 

SCENE1:

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


    local image 

    -- Touch event listener for background image 
     local function onSceneTouch(self, event) 
    if event.phase == "began" then 

    storyboard.gotoScene("scene2", "fade", 400 ) 

    return true 
     end 
     end 


    -- Called when the scene's view does not exist: 
    function scene:createScene(event) 
local screenGroup = self.view 

image = display.newImage("bird.png") 
screenGroup:insert(image) 

image.touch = onSceneTouch 


print("\n1: createScene event") 
    end 

    function scene:enterScene(event) 

print("1: enterScene event") 

    end 

    function scene:exitScene(event) 

    print("1: exitScene event") 

    -- remove touch listener for image 
image:removeEventListener("touch", image) 

    end 


    -- Called prior to the removal of scene's "view" (display group) 
    function scene:destroyScene(event) 

print("((destroying scene 1's view))") 
    end 


    scene:addEventListener("createScene", scene) 

    scene:addEventListener("enterScene", scene) 

    scene:addEventListener("exitScene", scene) 

    scene:addEventListener("destroyScene", scene) 


    return scene 

我的第二個場景是同一類的。但是當我觸摸圖像時,它不會進入第二場景。

回答

1

記得將事件偵聽器添加到您的圖像。

+0

謝謝你,royi。我只是不知道事件監聽器的第二種格式。我應該添加這個代碼:image:addEventListener(「touch」,image) –

2
local function onSceneTouch(self, event) 
    if event.phase == "began" then 
    storyboard.gotoScene("scene2", "fade", 400 ) 
    return true 
    end 
end 

變化 '返回true' 在onSceneTouch功能分配的任何事件圖像之前 '返回false' 等