Problem with If statement

If statement condition not working

Page 1 of 1

5 Replies - 851 Views - Last Post: 02 August 2008 - 03:52 AM Rate Topic: -----

#1 Minty  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 30-July 08

Problem with If statement

Post icon  Posted 31 July 2008 - 06:55 AM

Hi all, I'm having problem with an If statement in my game.

I've created a class called SPRITE to hold basic informaiton about sprites such as their location and the size of them
class SPRITE 
{
public:
	int x,y;
	int width,height;
	int movex,movey;
	int curframe,lastframe;
	int animdelay,animcount;
	int scalex, scaley;
	int rotation, rotaterate;
	int health;		// Health of enemies
	bool alive;

};


Now, I have used "new" to create a dynamic object called "fire" which will be weapons fire and I have entered data into the object using ->, for exmaple
	fire->x = 100;				//set to zero as it will always come from where the player is when fired
	fire->y = 500;
	fire->width = 16;
	fire->height = 16;
	fire->curframe = 1;
	fire->lastframe = 6;
	fire->animdelay = 3;
	fire->animcount = 0;
	fire->movex = 10;
	fire->movey = 0;
	fire->alive = 0;



When viewing the sprite in the game, everything works fine, it loads at the coordinate as it should, the height is as it should and everything else works, except for the alive variable. I am using that variable to see whether the weapon is alive, for example, once a button has been pressed, then when its alive it can fire off, here is the code for that.
if (Mouse_Button(0));
		{
			fire->alive = 1;
			if(fire->alive == 1);
			{
				fire->x += fire->movex;
				position3.x = (float)fire->x;
				position3.y = (float)fire->y;

				sprite_handler->Draw(
					fire_image, 
					&srcRect3,
					NULL,
					&position3,
					D3DCOLOR_XRGB(255,255,255)); 
			}
		}



So, once the mouse button is pressed, alive gets set to 1, and if alive is set to 1, then move the sprite accordingly and draw it. But the problem is, no matter what I set the condition to, it will ALWAYS fire, and I have checked the code and alive is not set to 1 anywhere else, infact it is set to 1 when the class is filled in. At first I thought it was some problem caused by using a dynamic class, but if that were true the rest of the filled in data would have a problem too.

Does anyone know why the if statement is failing? If its soemthing obvious I'm gonna kick myself, any help is appreciated, if ive explained it badly just let me know and ill go through it again. Thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: Problem with If statement

#2 AmitTheInfinity  Icon User is offline

  • C Surfing ∞
  • member icon

Reputation: 117
  • View blog
  • Posts: 1,559
  • Joined: 25-January 07

Re: Problem with If statement

Posted 31 July 2008 - 07:29 AM

I didn't understand this part

Quote

So, once the mouse button is pressed, alive gets set to 1, and if alive is set to 1, then move the sprite accordingly and draw it. But the problem is, no matter what I set the condition to, it will ALWAYS fire,


you said that you set alive to 1 when the button is pressed and immediately on next line you check that whether it is 1! then it's bound to be true every time!
Was This Post Helpful? 0
  • +
  • -

#3 Tom9729  Icon User is offline

  • Segmentation fault
  • member icon

Reputation: 180
  • View blog
  • Posts: 2,641
  • Joined: 30-December 07

Re: Problem with If statement

Posted 31 July 2008 - 07:41 AM

This isn't so much a Game Programming question as it is a general programming question methinks. :)
Was This Post Helpful? 0
  • +
  • -

#4 gabehabe  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1382
  • View blog
  • Posts: 10,962
  • Joined: 06-February 08

Re: Problem with If statement

Posted 31 July 2008 - 07:45 AM

Amit is right, you check immediately after setting it:

Quote

fire->alive = 1;
if(fire->alive == 1);

Plus, you have a semi-colon at the end of your "if" line.:)
Was This Post Helpful? 0
  • +
  • -

#5 Trogdor  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 15
  • View blog
  • Posts: 627
  • Joined: 06-October 06

Re: Problem with If statement

Posted 31 July 2008 - 09:46 AM

and you use 1 and 0 in a boolean. not wrong, just bad style.
Was This Post Helpful? 0
  • +
  • -

#6 Minty  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 30-July 08

Re: Problem with If statement

Posted 02 August 2008 - 03:52 AM

oh no the smi colin, I guess its time to kick myself :D
Thanks a lot guys, I've amended it and it seems to be working, cheers :^:

Oh and sorry if the question is in the wrong forum, I wasnt sure which one would be best, thanks though.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1