【问题标题】:How to remove a previous image and load new image when touching next button触摸下一个按钮时如何删除以前的图像并加载新图像
【发布时间】:2014-03-09 07:53:30
【问题描述】:

我是编程新手,尤其是 Corona SDK (Lua)。我需要帮助! 问题是: 我在数组中有 10 个图像和一个按钮,同时点击按钮我需要删除上一个图像并显示存储在数组中的下一个图像。 我做了所有的,但是当点击下一张图片时,下一张图片很好,但是上一张图片没有从屏幕上删除,我想删除它, 还有一件事是在完成第 10 张图片后,我喜欢从第 1 张图片开始,就像一个循环一样。

local Next = function()

    for j = 1, 10 do

        j=j+1

    end

    return true

end

local dotted =  {"images/1.png", "images/2.png","images/3.png","images/4.png","images/5.png",
                "images/6.png","images/7.png","images/8.png","images/9.png","images/10.png"}


local nextButton = widget.newButton{
    left = display.contentWidth/1.25,
    top = display.contentHeight - 55,
    defaultFile="images/next.png",
    width = 50, height = 50,
    onRelease = Next}


j = 1 
function loadingImages1()       
    di = display.newImageRect(dotted[j],150,300);
    di.x = calcx(40,"PER")  
    di.y = calcx(30,"PER")
    di.height = calch(60,"PER")
    di.width = calcw(20,"PER")
    j = j + 1
end

local function onObjectTap( self,event )
    --di1.removeSelf();
    di1:removeSelf();
    loadingImages1()
    return true
end
nextButton:addEventListener( "tap", onObjectTap )

【问题讨论】:

    标签: arrays lua coronasdk lua-table


    【解决方案1】:

    我认为您不需要 Next 功能。你应该 di1 你的意思是 di 然后 removeSelf() 应该足以让它从视图中消失。此外,在第一次点击之前,我没有看到任何初始化 di 的代码。你应该有类似的东西

    local nextIndex = 1
    
    local dotted = {....}
    
    local di -- avoid globals
    
    local function loadingImages1()       
        di = display.newImageRect(dotted[nextIndex],150,300);
        ... set x, y, height, width; then: 
        -- update next index, cycle back to 1 if necessary
        nextIndex = nextIndex + 1
        if nextIndex > #dotted then 
            nextIndex = 1 
        end
    end
    
    loadingImages1() -- run once to initialize
    
    local function onObjectTap( self,event )
        di:removeSelf() -- remove di
        loadingImages1()
        return true
    end
    

    【讨论】:

    • 非常感谢@Schollii!它对我有用,你能解释一下我哪里出错了吗,
    • 我怀疑问题出在 di1 的使用上,它只有一行,它应该会导致错误,但由于它没有某个地方必须有 init,所以它总是一样的图片被删除。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-15
    • 1970-01-01
    相关资源
    最近更新 更多