如何在触摸下一步按钮时删除以前的图像并加载新图像



我是编程新手,尤其是Corona SDK(Lua)。我需要帮助!问题是:我在数组中有一个按钮,点击按钮时我需要删除上一个图像并显示存储在数组中的下一个图像。我做了所有的事情,但是在点击下一个图像时,下一个图像来得很好,但是上一个图像没有从屏幕上删除,我想删除它,还有一件事是完成第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 )

我认为你不需要下一个函数。你应该通过 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

最新更新