我正在创造一个游戏,玩家必须收集硬币以获得更高的分数。有3个硬币,当你收集一个硬币时,它会显示在UI中的“硬币条”上。硬币有3个空洞(比如Cut the Rope和其他游戏)。目前,当玩家收集第二枚硬币时,条形图中的第二个洞被填满。我想要按顺序填充孔,以便收集没有收集第一枚硬币的第二枚硬币将填满第一个洞。
以下是代码:
------ Coin Bar
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 )
答案 0 :(得分:0)
尝试用以下方法替换硬币栏中的所有内容:
------Coin Bar
local coin_bar = {}
for i=1,3 do
coin_bar[i] = display.newImage ("coin_bar.png", 325+(i*60), 37)
end
看看是否能解决您的问题。
答案 1 :(得分:0)
为什么不将收集的属性添加到跟踪上次使用位置的硬币条并使用它?即:
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