好的,我在这里,是一个不熟悉Lua语言的人。 我的问题是我无法获得代码,因为我希望它能够运行。
所以我希望我的代码每次点按1显示1张新图片。
因此,如果我点击3次,显示屏上将显示3张图片。
到目前为止,这是我的代码,相信我用Google搜索和谷歌搜索,但我找不到任何可以帮助我的东西,所以我想这是我的最后一个解决方案。
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张照片。
答案 0 :(得分:1)
您应该清理以前的图像。为此,请将对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)