First step: setting up 2 variables "vhearts" (virtual hearts of the code) and "heartLimit" (max. number of hearts for your player). Create them and assign them the value that you want.
Second step: powerup system. It isn't perfect, but we'll keep the number of true hearts in 2 always, so the third heart will add 1 to the virtual hearts. After that, we need to keep the true hearts in 2 again:
Code: Select all
--Detect extra hearts. It'll add an infinite number of hearts.
if ((player:mem(0x16, FIELD_WORD)) > 2) then
vhearts = vhearts + 1;
player:mem(0x16, FIELD_WORD, 2);
end
Third step: the limit. That conditional will add 1 virtual heart everytime you acquire a powerup. That's why we created a second variable. Using the next conditional, the player won't get more hearts if the virtual hearts reached the limit:
Code: Select all
--You've defined a number of hearts for some reason, no?
if (vhearts > heartLimit) then
vhearts = heartLimit;
end
Fourth step: substract hearts. When the player hurts, the number of hearts will decrease logically. Do you remember that the true hearts are always fixed in 2? Well, when the player receives a damage there is 1 true heart substracted. We can compare that single heart with the amount of virtual hearts here:
Code: Select all
--If damaged, take extra hearts from virtual hearts
if ((player:mem(0x16, FIELD_WORD)) == 1) and (vhearts > 1) then
vhearts = vhearts - 1;
end
Fifth step: keep the player alive. If we run the code as is now, the player will start the game with 1 heart only and if you have various hearts, you'll switch to small at the first hit and then you'll die. So we'll keep the player in big state when the character has more than 2 hearts:
Code: Select all
--This keeps the player alive
if (player.powerup == PLAYER_SMALL) and (vhearts > 2) then
player.powerup = PLAYER_BIG;
end
With this your heart system will work fine, but doesn't work for Toad and Peach. Also I made a small API to use this system and display it with a single line of code within your onLoad function:
Code: Select all
--How to use: _heartAPI.heartSystem(BASE TEXT, NUMBER OF HEARTS, X COORDINATES OF THE TEXT, Y COORD. OF THE TEXT, DISPLAY OR NOT).
--Example:
_heartAPI.heartSystem("TANKS:", 14, 194, 56, true);
With the second argument, we set the number of virtual hearts and the limit, obviously.
There is a video which shows how the system works:
Original link: https://www.youtube.com/watch?v=ZhdOaDn97_0
That's all! I hope this will be useful for you