Welcome to Dream.In.Code
Getting Help is Easy!

Join 118,307 Programmers for FREE! Ask your question and get quick answers from experts. There are 1,690 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



Problem with If statement

 
Reply to this topicStart new topic

Problem with If statement, If statement condition not working

Minty
post 31 Jul, 2008 - 06:55 AM
Post #1


New D.I.C Head

*
Joined: 30 Jul, 2008
Posts: 4


My Contributions


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
CODE
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
CODE

    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.
CODE

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.
User is offlineProfile CardPM

Go to the top of the page


AmitTheInfinity
post 31 Jul, 2008 - 07:29 AM
Post #2


D.I.C Addict

Group Icon
Joined: 25 Jan, 2007
Posts: 818



Thanked 20 times

Dream Kudos: 125
My Contributions


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!
User is offlineProfile CardPM

Go to the top of the page

Tom9729
post 31 Jul, 2008 - 07:41 AM
Post #3


Debian guru

Group Icon
Joined: 30 Dec, 2007
Posts: 1,429



Thanked 10 times

Dream Kudos: 325
My Contributions


This isn't so much a Game Programming question as it is a general programming question methinks. smile.gif
User is offlineProfile CardPM

Go to the top of the page

gabehabe
post 31 Jul, 2008 - 07:45 AM
Post #4


T3H R0XX0R!

Group Icon
Joined: 6 Feb, 2008
Posts: 3,516



Thanked 73 times

Dream Kudos: 2350

Expert In: (X)HTML, CSS, Batch Scripting, C, C++

My Contributions


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.smile.gif
User is online!Profile CardPM

Go to the top of the page

Trogdor
post 31 Jul, 2008 - 09:46 AM
Post #5


D.I.C Regular

Group Icon
Joined: 6 Oct, 2006
Posts: 487



Thanked 2 times

Dream Kudos: 125
My Contributions


and you use 1 and 0 in a boolean. not wrong, just bad style.
User is offlineProfile CardPM

Go to the top of the page

Minty
post 2 Aug, 2008 - 03:52 AM
Post #6


New D.I.C Head

*
Joined: 30 Jul, 2008
Posts: 4


My Contributions


oh no the smi colin, I guess its time to kick myself biggrin.gif
Thanks a lot guys, I've amended it and it seems to be working, cheers icon_up.gif

Oh and sorry if the question is in the wrong forum, I wasnt sure which one would be best, thanks though.
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 10/10/08 11:40AM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month