我尝试使用数字为我的游戏添加得分。我的分数由5个数字组成,如下图所示:
要显示起始分数(00000),我使用了5张图片(scoreB.png是数字零的图像):
score_nul_1 = display.newImage( "scoreB.png")
score_nul_1.x = score_title.x+180---center the sprite horizontally
score_nul_1.y = 35 --center the sprite vertically
screenGroup:insert(score_nul_1)
score_nul_1.isVisible=true
score_nul_2 = display.newImage( "scoreB.png")
score_nul_2.x = score_title.x+220---center the sprite horizontally
score_nul_2.y = 35 --center the sprite vertically
screenGroup:insert(score_nul_2)
score_nul_2.isVisible=true
score_nul_3 = display.newImage( "scoreB.png")
score_nul_3.x = score_title.x+263---center the sprite horizontally
score_nul_3.y = 35 --center the sprite vertically
screenGroup:insert(score_nul_3)
score_nul_3.isVisible=true
score_nul_4 = display.newImage( "scoreB.png")
score_nul_4.x = score_title.x+306---center the sprite horizontally
score_nul_4.y = 35 --center the sprite vertically
screenGroup:insert(score_nul_4)
score_nul_4.isVisible=true
score_nul_5 = display.newImage( "scoreB.png")
score_nul_5.x = score_title.x+349---center the sprite horizontally
score_nul_5.y = 35 --center the sprite vertically
screenGroup:insert(score_nul_5)
score_nul_5.isVisible=true
然后,我使用精灵将数字从1改为9:
local sheetData_scorea = { width=43, height=53, numFrames=10, sheetContentWidth=430, sheetContentHeight=53}
local mySheet_scorea = graphics.newImageSheet( "scoreA.png", sheetData_scorea )
local sequenceData_scorea =
{
{name = "0", frames={10}, loopCount=0, time=1000 },
{name = "1", frames={1}, loopCount=0, time=1000 },
{name = "2", frames={2}, loopCount=0, time=1000 },
{name = "3", frames={3}, loopCount=0, time=1000 },
{name = "4", frames={4}, loopCount=0, time=1000 },
{name = "5", frames={5}, loopCount=0, time=1000 },
{name = "6", frames={6}, loopCount=0, time=1000 },
{name = "7", frames={7}, loopCount=0, time=1000 },
{name = "8", frames={8}, loopCount=0, time=1000 },
{name = "9", frames={9}, loopCount=0, time=1000 },
{name = "10", frames={10}, loopCount=0, time=1000 },
}
score_a1 = display.newSprite( mySheet_scorea, sequenceData_scorea)
score_a1.x = score_title.x+306--center the sprite horizontally
score_a1.y = 35 --center the sprite vertically
screenGroup:insert(score_a1)
score_a1.isVisible=false
要更新分数,我使用此功能:
if mydata.score >=100 and mydata.score<1000 then
score_nul_4.isVisible=false
score_nul_3.isVisible=false
end
if mydata.score < 100 then
score_a.isVisible=true
score_nul_4.isVisible=false
score_a.x = score_title.x+306
score_a:setSequence(i)
score_a:play()
i = i +1
end
if mydata.score>=100 then
i=1
score_a.x = score_title.x+263
score_a.isVisible=true
score_a:setSequence(i)
score_a:play()
score_a1.isVisible=true
score_a1:setSequence(b)
score_a1:play()
b=b+1
end
if mydata.score>=200 then
i=2
score_a.x = score_title.x+263
score_a.isVisible=true
score_a:setSequence(i)
score_a:play()
score_a1.isVisible=true
score_a1:setSequence(c)
score_a1:play()
c=c+1
end
if mydata.score>=300 then
i=3
score_a.x = score_title.x+263
score_a.isVisible=true
score_a:setSequence(i)
score_a:play()
score_a1.isVisible=true
score_a1:setSequence(d)
score_a1:play()
d=d+1
end
if mydata.score>=400 then
i=4
score_a.x = score_title.x+263
score_a.isVisible=true
score_a:setSequence(i)
score_a:play()
score_a1.isVisible=true
score_a1:setSequence(e)
score_a1:play()
e=e+1
end
if mydata.score>=500 then
i=5
score_a.x = score_title.x+263
score_a.isVisible=true
score_a:setSequence(i)
score_a:play()
score_a1.isVisible=true
score_a1:setSequence(f)
score_a1:play()
f=f+1
end
if mydata.score>=600 then
i=6
score_a.x = score_title.x+263
score_a.isVisible=true
score_a:setSequence(i)
score_a:play()
score_a1.isVisible=true
score_a1:setSequence(g)
score_a1:play()
g=g+1
end
if mydata.score>=700 then
i=7
score_a.x = score_title.x+263
score_a.isVisible=true
score_a:setSequence(i)
score_a:play()
score_a1.isVisible=true
score_a1:setSequence(h)
score_a1:play()
h=h+1
end
if mydata.score>=800 then
i=8
score_a.x = score_title.x+263
score_a.isVisible=true
score_a:setSequence(i)
score_a:play()
score_a1.isVisible=true
score_a1:setSequence(j)
score_a1:play()
j=j+1
end
if mydata.score>=900 then
i=9
score_a.x = score_title.x+263
score_a.isVisible=true
score_a:setSequence(i)
score_a:play()
score_a1.isVisible=true
score_a1:setSequence(k)
score_a1:play()
k=k+1
end
所以我的问题是:是否有可能采用更“简单”的方式来使用图像进行评分?
答案 0 :(得分:0)
是的,我可以想到一些简单的方法来简化这个。
第一种方法是将分数分成单个数字并循环显示。
因此得分为203将成为2,0,3的数组。循环通过(反向)并获取最后一个数字:3,然后分配适当的图像以更新到第3帧(或者在您的情况下可能为4)。
我现在无法实际编写代码,但是那条道路可以帮助你。
沿着这些方向,你也可以检查分数的长度,所以如果它是203,长度是3,那么你使用一个从长度(3)开始的for循环并计数直到你用多少个数字想要让它关闭那些数字。
答案 1 :(得分:0)
用10个显示对象的显示组表示5位数分数的每个数字,其中10个显示对象中只有一个在任何时刻都有visible = true:
local digits = {}
local yPos = 100
local digitWidth = 20 -- pixels occupied by each digit
function createScoreDigit(digitNum)
digits[digitNum] = display.newGroup()
function addNumber(filename)
local digit = display.newImage(filename)
digit.visible = false
digit.y = yPos
digit.x = x0Pos + (digitNum-1) * digitWidth
digits[digitNum].insert(digit)
end
addNumber('number0.jpg', 1)
addNumber('number1.jpg', 1)
...
addNumber('number9.jpg', 1)
digits[digitNum][1].visible = true -- number 0
end
createScore(1)
createScore(2)
createScore(3)
createScore(4)
createScore(5)
这将显示&#34; 0 0 0 0 0&#34;假设&#34; number0.jpg&#34;是你的数字0.当然你可以使用图像表,但想法是一样的,只是指定给定数字的图像的细节,你可以解决这个问题。
然后,当你增加分数时,你必须计算所需的新指数。例如,当得分增加10时,您将获得{0,0,0,1,0},并且您将切换正确的可见性标记:
digits[4][1].visible = false -- make "0" invisible
digits[4][2].visible = false -- make "1" visible
由于图像在所有显示组中重复使用,因此使用的纹理内存应该是最小的。