08-29-2015, 09:55 PM
Are you unsure of of to set up the code for a health system, or just specifically rendering hearts appropriately?
How do you track the current health? Since each heart has a smallest value of 1/4, you can use integers to represent health. For every 4 whole numbers, you have 1 whole heart. Doing this makes it easier to render than using decimal numeric types and comparing decimal values like .25, .5, .75, etc. You can use the % (remainder) operator like so:
wholeHearts = totalHealth / 4; //integer math, so 3/4 = 0 whole hearts.
fractionalHearts = totalhealth % 4; //Ex) 5 % 4 = 1, 4 % 4 = 0, 3 % 4 = 3
For the fractionalHearts, you render whatever sprite represents the fraction, 1 -> 1/4, 2 -> 1/2, 3 -> 3/4.
To render new rows of hearts, you can probably iterate over all your hearts when rendering while tracking the total. If the total is greater than the max row size, then you reset the render position and offset in the YAxis.
How do you track the current health? Since each heart has a smallest value of 1/4, you can use integers to represent health. For every 4 whole numbers, you have 1 whole heart. Doing this makes it easier to render than using decimal numeric types and comparing decimal values like .25, .5, .75, etc. You can use the % (remainder) operator like so:
wholeHearts = totalHealth / 4; //integer math, so 3/4 = 0 whole hearts.
fractionalHearts = totalhealth % 4; //Ex) 5 % 4 = 1, 4 % 4 = 0, 3 % 4 = 3
For the fractionalHearts, you render whatever sprite represents the fraction, 1 -> 1/4, 2 -> 1/2, 3 -> 3/4.
To render new rows of hearts, you can probably iterate over all your hearts when rendering while tracking the total. If the total is greater than the max row size, then you reset the render position and offset in the YAxis.