简体   繁体   English

处理日冕中的事件

[英]handle events in corona lua

Iam new to corona, so I dont know how to organize my code to best. 我是电晕的新手,所以我不知道如何最好地组织代码。 I am trying to regiser click when user click on leftHam image, but I dont how to do it most efficently. 当用户单击leftHam图像时,我尝试重新注册单击器,但是我没有最有效的方法。 Right now I am getting leftHam is nil although on creation it should be assinged a value. 现在我要离开了汉姆虽然在创建时应该被赋值一个零。

local composer = require( "composer" )
local widget = require( "widget" )

local scene = composer.newScene()
local _H = display.contentHeight
local _W = display.contentWidth

leftNavBtn = nil
local navbarGroup

function scene:create( event )

    local sceneGroup = self.view

    local background = display.newImage("res/bg.png" )
        background:scale( _W, _H )
        background.x = _W
        background.y = _H 

    local navbarGroup = display.newContainer(_W, _H/4)
         navbarGroup.x = _W /2
        --navbarGroup.y = 0


    local top_bar = display.newImage("res/home/top_bar.png")
        top_bar.y = top_bar.height/2
    navbarGroup:insert(top_bar)

    --local leftNavBtn = display.newImageRect("res/home/hamburger.png", 100, 100)
        leftNavBtn.y =  leftNavBtn.height/1.5
        leftNavBtn.x = - navbarGroup.width/2 + leftNavBtn.width

    leftNavBtn = display.newImageRect("res/home/hamburger.png", 100, 100)
        leftNavBtn.y =  leftNavBtn.height/1.5
        leftNavBtn.x = - navbarGroup.width/2 + leftNavBtn.width
    navbarGroup:insert(leftNavBtn)

    local rightNavBtn = display.newImageRect("res/home/hamburger.png", 100, 100)
        rightNavBtn.y =  leftNavBtn.height/1.5
        rightNavBtn.x =  navbarGroup.width/2 - leftNavBtn.width
    navbarGroup:insert(rightNavBtn)


end

    function test()
        print("clickedddddddddddd")
    end

    function leftNavBtn:touch(event)
        if event.phase == "began" then
            display.getCurrentStage( ):setFocus(self)
            self.isFocus = true

        elseif self.isFocus then
            if event.phase == "moved" then 
                print("moved")
            elseif event.phase == "ended" or event.phase == "cancelled" then
                display.getCurrentStage( ):setFocus(nil)
                self.isFocus = false

            end
        end
        return true
    end





leftNavBtn:addEventListener( "touch", test )
scene:addEventListener( "create", scene )

return scene

Do you mean leftNavBtn because leftHam doesn't exist anywhere. 您是说leftNavBtn是因为leftHam在任何地方都不存在。

You are creating leftNavBtn in scene:create but are attempting to use it before calling that function anywhere (the leftNavBtn:addEventListener( "touch", test ) ) line. 您正在scene:create中创建leftNavBtn ,但是在任何地方( leftNavBtn:addEventListener( "touch", test ) )行调用该函数之前尝试使用它。

Within scene:create you also use leftNavBtn before creating it because you commented out this line local leftNavBtn = display.newImageRect("res/home/hamburger.png", 100, 100) without commenting out the two lines after it (which set of three lines you then duplicate immediately after that). scene:create您还leftNavBtn在创建它之前使用leftNavBtn ,因为您已注释掉了这一行, local leftNavBtn = display.newImageRect("res/home/hamburger.png", 100, 100)而没有注释掉其后的两行(哪一组三行,然后您立即复制)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM