#### Archived

This topic is now archived and is closed to further replies.

# Help with moving a sprite - and stopping...

This topic is 6019 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi, Well I have a Sprite Class that I wrote all by myself and it works fine... but I''m running into a little error and I can''t figure why this is happening... Take a good look at this code...
  Player->x = 0; Player->y = 0; if(Player->facingleft = false) { Player->Set_Animation_Sprite(1,0); } if(Player->facingleft = true) { Player->Set_Animation_Sprite(2,0); } if(g_keyboard[DIK_RIGHT]) { Player->Set_Animation_Sprite(3,0); Player->facingleft = false; Player->xv +=1; } if(g_keyboard[DIK_LEFT]) { Player->Set_Animation_Sprite(4,0); Player->facingleft = true; Player->xv -=1; } Player->Animate_Sprite(); Player->Move_Sprite(); Player->Draw_Sprite(g_lpddsback); 
All the functions work fine... and this might as well be some psuedocode... but here''s the kicker. In the constructor and a function I call to init the Player it sets the facingleft (which is a bool) to false. So it starts off facing right - right ? No - it decides to start facing left. Every single time the game loop loops through it always goes back to setting the bool facingleft = true; Which I specifically want to go false when the user presses right. The input works fine hence there is nothing wrong with the input stuff above. It does play the desired animations when pressing the right or left key. The animations set in the if(facingleft = true) and if(facingleft = false) however don''t seem to work (because for some reason facingleft is always equal to true). Also as another point - why do I have to add the velocites to the current velocity (which is in the case 0). And not just make it equal to the velocity I want. It wouldn''t work that way... Any help? Thanks, Destroyer

##### Share on other sites
Try this:
...
if(Player->facingleft == false)
..
if(Player->facingleft == true)
...
What you did, was setting Player->facingleft to true in your second if-statement. That''s the reason it was always true.

##### Share on other sites
Infact the solution above is only half correct. You must use the "==" operator in both "if" statements at the beginning. What you are doing is setting the the "Player..." values to "true" & "false" instead of checking.

Hope this helps..

Indy
Indsoft UK

##### Share on other sites
Erm, isn''t that exactly what the AP said?

A quick question Destroyer, purely out of interest. If you know how to write a class, use pointers, functions etc, how come you didn''t spot the = / == issue straight away?

Insomnia

##### Share on other sites
quote:
Original post by Insomnia
A quick question Destroyer, purely out of interest. If you know how to write a class, use pointers, functions etc, how come you didn''t spot the = / == issue straight away?

Well, I''ve been programming in C for about 1 1/2 years, C++ for about 1/2 year and I''m ok using them both. I still make those kinds of errors all the time

Mark

##### Share on other sites
quote:
Original post by Insomnia
A quick question Destroyer, purely out of interest. If you know how to write a class, use pointers, functions etc, how come you didn''t spot the = / == issue straight away?

Well, I''ve been programming in C for about 1 1/2 years, C++ for about 1/2 year and I''m ok using them both. I still make those kinds of errors all the time

Mark

##### Share on other sites
Whoops! Uhhh... I need some sleep - geez that was down right awful of me to do... heh...

I''m really sorry... I''ve been programming in C/C++ for almost a year now (not quite) and oh man - that''s horrible thing to miss - I can''t believe I did that

The funny part is... I had someone else look over the code too and he was stumped as well

Dumb , dumb, dumb...

Thanks though...

Destroyer

--------------------------------
In a world with out boundaries -
who needs gates or windows ?
--------------------------------

##### Share on other sites
How about a sticky note on your monitor:" = IS NOT =="

Or always use a definition like:

#define ISTRUE ==

I''m sure I''ll find millions of other helpfull little tricks for you. Just wait

##### Share on other sites
hehehe that one is a CLASSIC!
:D

i remember when i was writing my first big c++ prog, and i had to spend 6 hours and eventually stumbled upon that same error hehe.

That was when i was coming away from ada, which uses the ''='' to test for equality aswell...

here in castle camelot we eat ham and jam and spam alot!