使用得分模块添加得分。

我正在制作一个游戏,通过点击图像来收集积分。点击图像并产生新的图像没有问题,但是将积分添加到分数时会引起一些问题。当我点击图像时,得分不会更新为相应的积分。我正在使用这个得分模块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()
点赞
用户4324015
用户4324015

使用 score_mod.add(images[event.target.imgNumber].points) 代替 score\_mod.score = score\_mod.score + images\[event.target.imgNumber\].points

2014-12-24 09:56:42