textNumber

Non-platform specific questions.

textNumber

Postby Super Pieman » Sun Nov 25, 2007 7:49 pm

I am making a shooter game and your maximum ammo for one gun is 154. I have made it so that you can only pick up the ammo when you are not full. For some reason when you pick up the ammo it shows waht you would get for half a second and then goes down to 154. Here is the script.
Code: Select all
if (textNumber>154)
{
     textNumber=154;
}


Is there a way to make it so that it immediatly shows 154.
Current Projects:
*Lonely (2%)
**LinkSys (1%)

*To be made in GE, then ported to DS.
**To be made for the DS.
User avatar
Super Pieman
 
Posts: 57
Joined: Sat Nov 03, 2007 3:27 pm
Location: Manotick (Ontario, Canada)
Score: 2 Give a positive score

Re: textNumber

Postby pixelpoop » Sun Nov 25, 2007 9:10 pm

Make sure that that code is with the code that allows you to pick up or not pick up ammo. If the code is separate then it could be that GE runs the pickup code, then advances a frame, then runs the ammo limit code. If this is the case then you would see the number shift for a brief frame.
User avatar
pixelpoop
 
Posts: 276
Joined: Tue Aug 29, 2006 9:32 pm
Score: 28 Give a positive score

Re: textNumber

Postby Fuzzy » Mon Nov 26, 2007 5:01 am

Dont do math operations on textNumber. Its prone to errors. Store your data in appropriately named variables; its just good coding practice.

Code: Select all
ammo = min(ammo, 154);


You dont need an if here. The opposite would be max(ammo, 154), which would prevent it from going under 154. you can use this to assure that ammo never falls below zero.

Code: Select all
ammo = min(max(ammo, 0), 154);


now it is not possible for ammo to be anything but a range from 0 to 154

When you are coding, ask yourself "am i SURE i always want the computer to do this?" if the answer is yes, you probably dont need an if. Basically, if the decision is already known before the program runs, then you dont want to use if. You want to tell the compiler "do this, and dont think about it".

After that, assign it to the textNumber

Code: Select all
textNumber = ammo;


and you will never see a value outside the range you want.
Mortal Enemy of IF....THEN(and Inspector Gadget)

Still ThreeFingerPete to tekdino
User avatar
Fuzzy
 
Posts: 1068
Joined: Thu Mar 03, 2005 9:32 am
Location: Plymostic Programmer
Score: 95 Give a positive score


Return to General

Who is online

Users browsing this forum: No registered users and 1 guest

cron