我正在制作游戏,按下图像来收集点数。按下图像并重新生成新图像是没有问题的,但是为分数添加点会导致一些问题。当我按下图像时,分数不会随着点数的增加而更新。我使用此评分模块http://coronalabs.com/blog/2013/12/10/tutorial-howtosavescores/
local score_mod = require( "score" )
math.randomseed( os.time() )
local scoreText = score_mod.init({
fontSize = 70,
font = native.systemFont,
x = display.contentCenterX + 50,
y = display.contentCenterY + 170,
filename = "scorefile.txt"
})
scoreText:setTextColor( (232/255), (216/255), (32/255) )
local function Game ()
local images ={
{name = "Icon.png", points = 1},
{name = "Icon-mdpi.png", points = 2},
{name = "Icon-xhdpi.png", points = 3},
{name = "Icon-ldpi.png", points = 4},
{name = "Icon-Small-50.png", points = 5}
}
local numRows = 3
local numCols = 2
local blockWidth = display.contentCenterX / 2.2
local blockHeight = display.contentCenterY / 2
local row
local col
local imgDataArray = {}
function imagePress (event)
if event.phase == "began" then
local x = event.target.x
local y = event.target.y
event.target:removeSelf()
score_mod.score = score_mod.score + images[event.target.imgNumber].points
function nextImages(x, y)
local nextRandomImg = math.random(1,5)
local nextImage = display.newImage(images[nextRandomImg].name, x, y)
nextImage:addEventListener( "touch", imagePress )
nextImage.imgNumber = nextRandomImg
table.insert(imgDataArray, image)
end
local nextDelay = function() return nextImages(x, y) end
timer.performWithDelay( 2000, nextDelay, 1 )
end
return true
end
function makeImage()
for row = 1, numRows do
for col = 1, numCols do
local x = (col - 1) * blockWidth + 120
local y = (row + 1) * blockHeight - 160
local randomImage = math.random(1, 5)
local image = display.newImage(images[randomImage].name, x, y)
image.imgNumber = randomImage
image.imgX = x
image.imgY = y
image:addEventListener( "touch", imagePress )
table.insert(imgDataArray, image)
end
end
end
makeImage()
end
Game()
非常感谢!
答案 0 :(得分:1)
使用 score_mod.add(images [event.target.imgNumber] .points)而不是score_mod.score = score_mod.score + images [event.target.imgNumber] .points