好吧,我来了,一个刚接触Lua语言的人。我的问题是我不能得到一个代码,因为我希望它运行。
因此,我希望我的代码为每个点击显示1个新图像。
因此,如果我点击3次,屏幕上会显示3张图片。
这是我到目前为止的代码,相信我,我在谷歌上搜索了又搜索,但我找不到任何可以帮助我的东西,所以我想这是我的最后一个解决方案。
function screenTap()
local randomPicture = display.newImage("pictures/Boy.png")
randomPicture.x = 160;
randomPicture.y = 250;
randomPicture.width = 250;
randomPicture.height = 250;
end
display.currentStage:addEventListener("tap",screenTap)
当我点击时,此代码将显示一张图片。(点击一下即可显示图片)。我的问题是:每次点击我想获得一张图片。所有图片仍将显示在屏幕上。所以,如果我点击1000次,我可以有1000张照片。
您应该清理之前的图像。要执行此操作,请将对picture
变量的引用移动到外部范围。然后,您应该保留当前图像的索引,这样您就可以在每次点击时迭代pictures
数组。或者,您可以使用math.random
在每次点击时获得随机图像索引。
-- Keep reference to current image, needed only if you want to move or delete this picture in the future
local picture = nil
-- Array with image names, you should have 3 images with exact name in your resources
local pictures = {"Boy", "Girl", "Animal"}
-- Keep index of current image, needed to iterate through images array every tap
local pictureId = 0
-- Add `event` into arguments, so now you can receive more info from this event
function screenTap( event )
-- If you don't need to clear previous image, remove this lines
if picture ~= nil then
picture:removeSelf()
picture = nil
end
-- Avery call of this function create a new image with current picture index
picture = display.newImage("pictures/" .. pictures[idx] .. ".png")
-- Use `event` to get point where user tap, and move image on that position
picture.x = event.x
picture.y = event.y
picture.width = 250
picture.height = 250
-- Increment index of the current picture
pictureId += 1
-- If it is greater then or equal number of pictures — reset it to zero
if pictureId >= #pictures then
pictureId = 0
end
end
display.currentStage:addEventListener("tap", screenTap)