0

我正在创建一个游戏,玩家必须收集硬币才能获得更高的分数。有 3 个硬币,当您收集一个时,它会显示在 UI 的“硬币栏”上。硬币有 3 个空洞(就像在割绳子和其他游戏中一样)。目前,当玩家收集第二枚硬币时,将填满条中的第二个孔。我希望按顺序填充孔,以便收集没有收集到第一个硬币的第二个硬币将填充第一个孔。

这是代码:

------金币吧

    local coin_bar = {}
    for i=1,3 do
        coin_bar[i] = display.newImage ("coin_bar.png", 57, 57)
    end

    coin_bar[1].x = 325 
    coin_bar[1].y = 37
    coin_bar[2].x = 385 
    coin_bar[2].y = 37
    coin_bar[3].x = 445 
    coin_bar[3].y = 37

--添加硬币和处理

    local coinSprites=grabber.grabSheet("starAnim")
    local coinGroup = display.newGroup()
    local coins = {}
    isLiving = {}
    for i=1,3 do
        isLiving[i] = 1
        coins[i] = coinSprites:grabSprite("",true,{ starAnim={1,6,200,0}})
        coins[i]:playClip("starAnim")
        coinGroup:insert(coins[i])
    end

    local function coinCollect(event)
        for i=1, 3 do -- Nr of Coins
            coin_clear = false
            if isLiving[i] == 1 then
                if ball.x > coins[i].x -40 and ball.x < coins[i].x +40
                and  ball.y > coins[i].y -40 and ball.y < coins[i].y +40 then
                coins[i]:removeSelf()
                coins[i] = nil
                coins[i] = display.newImage ("coin_bar_collected.png", 57, 57)
                coins[i].x = coin_bar[i].x
                coins[i].y = coin_bar[i].y
                isLiving[i] = 0
                end
            end
        end
    end

    Runtime:addEventListener( "enterFrame", coinCollect )
4

2 回答 2

0

Why not add a collected property to coin bar that tracks the last used position and use that? Ie:

local coin_bar = {collected = 0}

...

local function coinCollect(event)
    for i=1, 3 do -- Nr of Coins
        coin_clear = false
        if isLiving[i] == 1 then
            if ball.x > coins[i].x -40 and ball.x < coins[i].x +40
            and  ball.y > coins[i].y -40 and ball.y < coins[i].y +40 then
            coins[i]:removeSelf()
            coins[i] = nil
            coins[i] = display.newImage ("coin_bar_collected.png", 57, 57)
            local bar_index = coin_bar.collected + 1
            coins[i].x = coin_bar[bar_index].x
            coins[i].y = coin_bar[bar_index].y
            coin_bar.collected = bar_index
            isLiving[i] = 0
            end
        end
    end
end
于 2012-08-09T14:47:02.983 回答
0

尝试用这个替换硬币栏中的所有东西:

------Coin Bar

local coin_bar = {}
for i=1,3 do
    coin_bar[i] = display.newImage ("coin_bar.png", 325+(i*60), 37)
end

看看能不能解决你的问题。

于 2012-08-09T13:25:58.920 回答