0
votes

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. 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
1

1 Answers

1
votes

Do you mean leftNavBtn because leftHam doesn't exist anywhere.

You are creating leftNavBtn in scene:create but are attempting to use it before calling that function anywhere (the leftNavBtn:addEventListener( "touch", test )) line.

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