2012-03-27 61 views
2

我學習科羅娜SDK,並相信新的LUA以及(我主要做Ruby和一些JavaScript)。科羅娜SDK:填補酒吧從左至右

我有一個酒吧,我想填滿,因爲用戶做的東西。

  --outer rectangle 
    powerBar = display.newRect(210, 6, 24, 9) 
    powerBar.strokeWidth = 1 
    powerBar:setStrokeColor(254,203,50) 
    powerBar:setFillColor(0,0,0,0) 

      --inner rectangle which fills up 
    powerBarFill = display.newRect(211,7,0,7) 
    powerBarFill:setFillColor(234,183,30)  

當「東西」發生,我加1 powerBarFill.width,我本以爲這使其成長從左至右:我如下設置。但是,它實際上是從中心向外擴展的,即它的x位於中心,寬度從中心延伸。

最好的方法來保持左側靜態和增長的右側?我可以設置它,使它的x位置實際位於左側而不是中間位置?似乎可能會這樣做。

歡呼提前

回答

5

創建進度條,當我碰到的這個問題爲好。問題在於矩形的參考點。正如您注意到的那樣,默認參考點位於對象的中心。您可以使用object:setReferencePoint()來更改它。我相信你想使用display.BottomLeftReferencePoint值:

powerBar:setReferencePoint(display.BottomLeftReferencePoint)

請記住,你有你設定的x,y值之前設置這個值。所以你的情況,你需要創建矩形後設置的基準點,然後分配值X,Y再次(即使你已經這樣做在newRect構造函數):

powerBar = display.newRect(210, 6, 24, 9) 
powerBar:setReferencePoint(display.BottomLeftReferencePoint) 
powerBar.x, powerBar.y = 210, 6 
2

如果它的寬度從兩側的X位置:

1)它應開始於:

Centre - (Width when it's full/2) 

2)每幀中,添加:

incrs = 1 --Amount to increase by 
width = width + incrs 
x = x + incrs/2 
+0

McStretch的解決方案可能效果最好,但該解決方案几乎適用於所有平臺。我建議接受他,因爲它會更好地回答你的問題。 – 2012-03-27 16:41:17

+0

兩者都是比我的刪除和每次是爲給予好評更新:) – 2012-03-30 14:51:06

+0

由於時間重繪填充條的臨時解決方案更好:) – 2012-03-30 15:13:01