如何在Corona中根据点击次数更改图像?

我正在使用Corona SDK,目前有以下内容。当您点击图像时,数字会增加。我想知道如何在达到某个点击次数时更改图像?

display.setStatusBar(display.HiddenStatusBar)

local newButton = display.newImage("button.png", 0, 0)
newButton.x = display.contentWidth - 60
newButton.y = display.contentHeight - 62.5

local number = 0
local textField = display.newText(number, 30, 30, native.systemFont, 25)

local function moveButtonRandom(event)
    number = number + 1
    textField:removeSelf()
    textField = display.newText(number, 30, 30, native.systemFont, 25)
end

newButton:addEventListener("tap", moveButtonRandom)
点赞
用户1870706
用户1870706

最好的做法是使用一个精灵,在增加到一定点时,告诉精灵播放下一帧。

这里有多个有关使用 imageSheets 和精灵的教程: http://coronalabs.com/resources/tutorials/images-audio-video-animation/

Rob

2014-06-09 01:05:50
用户3251969
用户3251969

你可以在函数中创建一个 if-then 决策语句,并使用数字作为参数。当语句为真时,切换图片。

例如:如果数字大于等于 3,则 --[[在 3 次点击后移除现有图片并添加新图片的代码在这里执行--]] End

祝你好运

2014-06-10 07:37:24
用户1078537
用户1078537

例如,如果您想在达到15次点击时显示一张图片,则可以修改您的点击功能,使数字被图像替换:

local function moveButtonRandom(event)
   number = number + 1

   -- 在这里进行 if 检查

   if number >= 15 then
       -- 点击次数为15或更多,将数字替换为图像
       textField:removeSelf()
       textField = display.newImage("yourImage.png")
   else
       -- 以“正常”方式执行操作
       textField:removeSelf()
       textField = display.newText(number, 30, 30, native.systemFont, 25)
   end
end

如果您不希望图像替换文本,则可以在文件顶部创建一个新的本地变量,并在 if 检查内进行分配。

2014-06-11 18:56:51