开发者

Getting a button reference?

开发者 https://www.devze.com 2023-03-07 06:34 出处:网络
I\'m working on an app in Lua + Corona. As a complete beginner, I\'ve managed to hack together a little script for a carousel, but now I\'ve got a question.

I'm working on an app in Lua + Corona. As a complete beginner, I've managed to hack together a little script for a carousel, but now I've got a question.

function forwardButtonPress() 
    if carousel.getCurImage() < #myImages then
        carousel.slideToImage(carousel.getCurImage() + 1)
    end

end

function backButtonPress()
    if carousel.getCurImage() > 1 then
        carousel.slideToImage(carousel.getCurImage() - 1)
    end
end

--Here's where we do the actual initilization of the page.

local fwbutton = display.newImage("buttonArrow.png") 
fwbutton.x = 260
fwbutton.y = 120
fwbutton:addEventListener("tap", forwardButtonPress )

local bkbutton = display.newImage("buttonBackArrow.png") 
bkbutton.x = 60
bkbutton.y = 120
bkbutton:addEventListener("tap", backButtonPress )

If you look at the code, you'll see that I have two buttons, a back button and a forward one. Those are for sliding the images. So, say you get to the end of the carousel. The script takes care of making sure that it doesn't go past the end already, but how do I access the 开发者_StackOverflow中文版button to set the alpha to zero or fade it? It's linear, so I can't just put the button above its event function, so that the event function can reference the button... is there a way to pass the event function a reference to the button?


You can forward declare the event handler functions like this at the top of the file:

local forwardButtonPress
local backButtonPress

Then create your buttons and attach the event handlers (This is your code copied & pasted):

local fwbutton = display.newImage("buttonArrow.png") 
fwbutton.x = 260
fwbutton.y = 120
fwbutton:addEventListener("tap", forwardButtonPress )

local bkbutton = display.newImage("buttonBackArrow.png") 
bkbutton.x = 60
bkbutton.y = 120
bkbutton:addEventListener("tap", backButtonPress )

Add a function to manage setting the appearance of the buttons when either button is clicked:

local function setButtons()
    if carosel.getCurImage() < #myImages then
        fwbutton.alpha = 1.0
    else
        fwbutton.alpha = 0.5
    end

    if carosel.getCurImage() > 1 then
        bkbutton.alpha = 1.0
    else
        bkbutton.alpha = 0.5
    end
end

Now, you can write the function implementations, which will be able to deal with the buttons via the setButtons function:

forwardButtonPressed = function()
    if carousel.getCurImage() < #myImages then
        carousel.slideToImage(carousel.getCurImage() + 1)
    end
    setButtons()
end

backButtonPress = function()
    if carousel.getCurImage() > 1 then
        carousel.slideToImage(carousel.getCurImage() - 1)
    end
    setButtons()
end

Disclaimer: I'm not able to test this now, so there might be a syntax error somewhere, but organizing the code this way will work for what you're doing.


You can create/define the buttons above the function, and attach the EventListener below, no? If not, I don't really understand the problem.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号