简体   繁体   English

Corona SDK-创建具有不同HP值的Health Bar

[英]Corona SDK - Create Health Bar with different HP value

I have a rectangle image I use for a Health Bar fill. 我有一个用于健康栏填充的矩形图像。 As your health goes down, the width of the bar decreases until you get to 0. For this part of the code everything works fine, the bar's width goes down and it stays in the same x position as its width is being changed. 随着您的健康状况下降,条形图的宽度会减小,直到变为0。对于此部分代码,一切正常,条形图的宽度减小,并且在更改其宽度时保持在相同的x位置。

My problem is when I create monster with different HP value, I will get different width of the Health Bar. 我的问题是,当我创建具有不同HP值的怪物时,我会得到不同宽度的生命条。 How can I create the same size rectangle with different HP value? 如何创建具有不同HP值的相同大小的矩形? Thank you so much. 非常感谢。

 -- Create Health Bar 
MonsterBar = display.newRect( 20, 160, 26, 10 )
MonsterBar:setReferencePoint(display.BottomLeftReferencePoint)
MonsterBar:setFillColor( 80,150, 0 )

-- Monster get hit

MonsterBar.width = MonsterBar.width - scoreHUD.text
MonsterBar:setReferencePoint(display.BottomLeftReferencePoint)

You need to use a percentile intermediate value, that you can then use in your progress bar. 您需要使用百分比中间值,然后可以在进度栏中使用该中间值。

For example, compute a monster health percent like that, making sure HP Current is <= HP Max: 例如,计算出这样的怪物生命值百分比,确保HP Current <= HP Max:

local nMonsterHPPercentile = (nMonsterHPCurrent / nMonsterHPMax) * 100

Once you have the percentile value, adapt your rectangle to be created with a max size of "100" for example (or 200, 300 etc but you will need to multiply the monster percentile) and display the individual rectangles according to calculated percentile... For example, if you got 62% for nMonsterHPPercentile, and the rectangle has width 100, do a loop that adds an individual hp rectangle to the bar for each 10%. 一旦有了百分位值,就可以修改要创建的矩形,最大尺寸为“ 100”(或200、300等,但您需要乘以怪物百分位),然后根据计算出的百分位显示各个矩形。例如,如果nMonsterHPPercentile占62%,并且矩形的宽度为100,则执行循环,为每10%的矩形添加一个单独的hp矩形。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM