Resources > Coding

Can I get some help.

(1/3) > >>

Theforeshadower:
Maybe it's the stress, but my heart drawing code isn't working 100%. 


--- Code: ---var i, ii;var fraction;var modValue;
fraction = global.life div 4;
modValue = global.life mod 4;
draw_set_font(font0);
for (i = 0; i < global.hearts ; i += 1)
{
    draw_text(view_xview+32,view_yview,global.life);
    draw_text(view_xview+32,view_yview+16,global.life mod 4)
   
    if (i <= 9)
        draw_sprite(sprHeart,0,x+92+(6*(i+1)),y+2);
    if (i > 9)
        draw_sprite(sprHeart,0,x+92+(6*(i-9)),y+9);
}

for (ii = 0; ii < fraction ; ii += 1)
{
    if (ii <= 9)
        draw_sprite(sprHeart,4,x+92+(6*(ii+1)),y+2);
    if (ii > 9)
        draw_sprite(sprHeart,4,x+92+(6*(ii-9)),y+9);
}

if (round(global.life / 4) <= 10) //(fraction <= 10)
    if (modValue > 0)
        draw_sprite(sprHeart,modValue,x+92+(6*fraction),y+2);
    else
        draw_sprite(sprHeart,4,x+92+(6*fraction),y+2);
else
    if (modValue > 0)
        draw_sprite(sprHeart,modValue,x+92+(6*fraction),y+9);
--- End code ---

The code has been changed a few times but yields the same results.  The last heart before life is 0 is getting drawn in the wrong place as is the current heart if you are about to increase to the next heart.

Unable to post screenshots at this moment.

Been screwing with this for an hour now.  Maybe someone who hasn't stared at it can see the problem.


I will also add that sprite fonts aren't working for me.  I am quite sure I have them set up correctly, but all I am getting drawn is a black block the size of the sprite instead of the proper image.

Cassyblanca:
Definitely need a screenshot to see exactly what's happening, and a mockup of what it should look like.

Some things:

1) Why on earth are you using two separate index variables? 'i' is never used after the first loop, so just re-use it.

2) Looking over your first loop - I'll start by pointing out that I'm assuming you are intending to have 10 hearts per row: rather than doing i-9 (which puts index 10 in the same place as index 1), do i - 10.

3) I'm assuming your second loop (fraction) is supposed to only draw the fractional heart? In which case, you're just drawing the fractional hearts over your full hearts (assuming that's what the first part is).

4) If you're nesting code branches (your if/else statements), use brackets. Yes, your indentation makes it easy for those of us reading it to understand what each one is supposed to tie to, but it can be somewhat ambiguous for compilers and doesn't give people unfamiliar with the GML compiler an easy way of telling if it's affecting your code.

Overall, I recommend you scrap the code, and take some time to work out the logic on paper. Figure out exactly what it is you want to do, and then execute. Again, this may be the effect of making a multitude of changes trying to get it to work, but this looks disorganized to me. And take the time to get some screenshots, because your description of your problem is clear as mud.

Theforeshadower:
1. I do it for myself.  Just helps me with readability.

2.  I did the i-10 to start.  It wasn't achieving what it was supposed to.

3.  Fraction is the amount of full hearts drawn UPTO the current heart.  So, fraction will always be 1 less than the current heart or equal to the current heart if the health is divisible perfectly by 4.  I shoulda renamed it to divValue.  Never got around to it.

4.  I never use brackets for single line if/else statements.  Seem very readable to me.  First time, I heard of a problem about doing so.  I been reading code done mostly in Java or C# done like that for years.  To each his own.

I already know exactly what I want to do: 

draw all the empty hearts first(as in the total hearts say, 12)
then draw full hearts upto the current heart
finally, draw the final heart which could be quarter, half, three quarters, or full


That simple.

Cassyblanca:
2.  I did the i-10 to start.  It wasn't achieving what it was supposed to.

You still haven't specified clearly what it's supposed to do or what it is doing instead, except with very vague descriptions. In any case, I spotted my error regarding this part, because I hadn't noticed your i + 1.

3.  Fraction is the amount of full hearts drawn UPTO the current heart.  So, fraction will always be 1 less than the current heart or equal to the current heart if the health is divisible perfectly by 4.  I shoulda renamed it to divValue.  Never got around to it.

divValue is just as meaningless as "fraction." You really need to develop better variable naming practices.


--- Quote ---draw all the empty hearts first(as in the total hearts say, 12)
then draw full hearts upto the current heart
finally, draw the final heart which could be quarter, half, three quarters, or full
--- End quote ---

This is why you need to provide screenshots and an explanation of what you're trying to do the first time. In any case, you still haven't clearly defined your problem, so I'm still shooting at an invisible target with trying to find possible errors in your code.

Theforeshadower:
Stupid naming conventions aside, the code was almost functioning as intended.  I was just trying to code at a bad time.

The code is near identical, however, you can see the changes near the bottom.
I commented out the lines that are going to be removed.  The 6*fraction has been changed.  Fraction now gets added by 1.  The hearts/life displayed on the HUD now function as I intended.  I will test some more to make sure it can't be broken, but it seems to be working.


--- Code: ---var i, ii;var fraction;var modValue;
fraction = global.life div 4;
modValue = global.life mod 4;
draw_set_font(font0);
for (i = 0; i < global.hearts ; i += 1)
{
    draw_text(view_xview+32,view_yview,global.life);
    draw_text(view_xview+32,view_yview+16,global.life mod 4)
   
    if (i <= 9)
        draw_sprite(sprHeart,0,x+92+(6*(i+1)),y+2);
    if (i > 9)
        draw_sprite(sprHeart,0,x+92+(6*(i-9)),y+9);
}

for (ii = 0; ii < fraction ; ii += 1)
{
    if (ii <= 9)
        draw_sprite(sprHeart,4,x+92+(6*(ii+1)),y+2);
    if (ii > 9)
        draw_sprite(sprHeart,4,x+92+(6*(ii-9)),y+9);
}

if (round(global.life / 4) <= 10) //(fraction <= 10)
    if (modValue > 0)
        draw_sprite(sprHeart,modValue,x+92+(6*(fraction+1)),y+2);
    //else
        //draw_sprite(sprHeart,4,x+92+(6*fraction),y+2);
else
    if (modValue > 0)
        draw_sprite(sprHeart,modValue,x+92+(6*(fraction+1)),y+9);
    //else
        //draw_sprite(sprHeart,4,x+92+(6*fraction),y+9);
--- End code ---


I plan on cleaning up some things you mentioned such as better variables names and adding comments as well.  The reason,I guess, as to why it seems like a weird way of handling the heart system is that I chose to base the hearts on whole numbers instead of decimals.  Even in LTTP, you can be hit without taken visible damage to your hearts when you have the Red Tunic.  The later Zelda games changed this by having quarters of the heart displayed.

I decided it was easier to make the life system run as 4 life to each heart.  I could have been done with decimals such as .25 .5 .75 then 1 for a full heart.  I chose to use whole numbers so that I can directly draw the image_index based on mod value if there is one.

Anyway, I probably make no sense.  It is what it is.  Even though I didn't start over, I did take a step back to re-evaluate based on your suggestions.  Thank you.

Navigation

[0] Message Index

[#] Next page


Contact Us | Legal | Advertise Here
2013 © ZFGC, All Rights Reserved



Go to full version