当触摸“下一页”按钮时,如何删除先前的图像并加载新的图像

我是编程新手,尤其是对于 Corona SDK(Lua)而言。我需要帮忙! 问题是: 我有一个包含10个图像的数组和一个按钮,当点击按钮时,我需要删除先前的图像并显示存储在数组中的下一个图像。 我已经做了所有的事情,但是在点击下一个图像时,下一个图像出现得很好,但是之前的图像没有从屏幕上移除,我想要移除它, 还有一件事是在完成第10个图像后,我想从第一个图像开始,就像一个循环。

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 )
点赞
用户869951
用户869951

我认为你不需要 Next 函数。如果 di1 指的是 di,那么 removeSelf() 应该足以将其从视图中消失。此外,我没有看到初始化 di 的代码在第一次点击之前执行。你应该像下面的代码一样:

local nextIndex = 1

local dotted = {....}

local di -- 避免使用全局变量

local function loadingImages1()
    di = display.newImageRect(dotted[nextIndex],150,300);
    ... 设置 x、y、height、width 然后:
    -- 更新下一个索引,如有必要循环回1
    nextIndex = nextIndex + 1
    if nextIndex > #dotted then
        nextIndex = 1
    end
end

loadingImages1() -- 运行一次以初始化

local function onObjectTap( self,event )
    di:removeSelf() -- 移除 di
    loadingImages1()
    return true
end
2014-02-12 16:58:31