【问题标题】:images wont unload - please assist图像无法卸载 - 请协助
【发布时间】:2023-03-06 15:42:01
【问题描述】:

我这里有一些代码,当单击图像(这是我的按钮)时,会随机出现一个新图像。这是因为我创建了一个表格,里面有一些图像。

local animalPic

local button = display.newImageRect ("images/animalBtn.jpg", 200, 200)
button.x = 250
button.y = 50

local myPics = {"images/animal1.png", "images/animal2.png"}

function button:tap (event)


    local idx = math.random(#myPics) 
    local img = myPics[idx] 
    local animalPic = display.newImage(img)
    animalPic.x = contentCenterX
    animalPic.y = contentCenterY
end

button:addEventListener ("tap", button)

它的问题是当我单击按钮时图形不断堆积。正确的行为应该是 -

单击按钮并在删除前一个图像时显示图像。我该如何融入这种行为?我已经尝试了 removeSelf 命令,但它不起作用......感谢任何帮助。

【问题讨论】:

    标签: image lua coronasdk


    【解决方案1】:

    每次输入函数时都要声明animalPic。您应该声明一次,然后将其删除并用另一个替换它。 应该是:

    local animalPic
    
    function button:tap (event)
        local idx = math.random(#myPics) 
        local img = myPics[idx] 
        animalPic:removeSelf()
        animalPic = nil
        animalPic = display.newImage(img)
        animalPic.x = contentCenterX
        animalPic.y = contentCenterY
    end
    

    【讨论】:

      【解决方案2】:

      当您调用 display.newImage() 时,您正在添加一个新图像。问题是您需要删除/隐藏原始文件。也许你真的需要两个对象——一个用于当前图像,一个用于点击事件。当点击事件发生时,隐藏旧图像并显示新图像。另一种方法是将所有图像加载到它们自己的 imageRects 中,然后打开和关闭它们。

      【讨论】:

      • 我不清楚你的意思?你能提供代码示例吗?我仍然认为自己是初学者.....谢谢。 :D
      猜你喜欢
      • 1970-01-01
      • 2022-01-08
      • 1970-01-01
      • 2021-11-26
      • 2011-06-02
      • 2020-07-10
      • 2018-12-01
      • 2018-09-29
      • 2018-12-08
      相关资源
      最近更新 更多