Question about flip values every other time

Started by
7 comments, last by kingpinzs 16 years, 9 months ago
I am trying to get a verible to switch its value every time it goes through the loop. I am not surew what it is called but I call it flip flop verible. I just not sure how to look it up. Can any one help with this delima?
Advertisement
Is it a bool variable?

Try var = !var;

If you have a loop that is controlled by an integer that gets incremented every iteration, you could alternatively use the modulus operator:

for( int i = 0 ; i < SOMETHING ; ++i ) {    bool trueEverySecondIteration = i % 2;}


If you don't think either of these suit your purposes, post your code and explain what you need it to do.
I need to turn Check_Box to true the first time, false the second time then back.

if(( my >= 95) && (my <= 117) && (mx >= 95) && (mx <= 117) && ClickLeft())  {     Check_Box = true;	  }


So rip-off what you have posted is what I need but need to be acheved in a differnt way.
Yeah what your after is a boolean variable and the easiest way to make it flip flop is to assign it to what it is not so

bool X = true;
X = !X; // X = false
X = !X; // X = true

ect...

hope that helps
____________________________"This just in, 9 out of 10 americans agree that 1 out of 10 americans will disagree with the other 9"- Colin Mochrie
Thanks for the help that part works but I geuss that is not what I needed. I need to be able to tell when the mouse is clicked then released. Because I can detect when it is clicked but it will change the state rapidly. I am using Direct input and I can detect if the mouse button is down or up but not if it was down first then up.

Can any one help with this? Thanks again
Then you should have stated your problem like that from the beginning.

While usual mouse API will provide 3 callbacks (mouse_down, mouse_up and mouse_clicked), the DI API works differently in that it only provides you with current state.

This is the difference between polling and events.

You need to introduce your own concept of mouse click, since API doesn't understand it.

struct MouseDown {                        1)  target t;  bool down;}...MouseDown md;                              2)bool leftMouseDown = pollMouseState();if (leftMouseDown) {                       3)  md.down = true;  md.target = findTarget(mouse_x, mouse_y);} else {  if (md.down) {                           4)    md.down = false;    Target t = findTarget(mouse_x, mouse_y);    if (t == md.t) {      md.t.foo();                          5)    }  } }


1) Is the structure that will hold mouse button state over lifetime of application. It's used to handle sequence of events that we define as "Mouse Clicked"

2) This instance of the structure, needs to be global with respect to mouse handler.

3) If mouse is currently down, remember that. Also, remember where the mouse was clicked. Target here would be some GUI element, button, listbox, window, ....

4) Ok, LMB is not down right now. Check if LMB was down last time we polled the input. If so, then "Mouse Clicked" has occured.

5) Did the user press the LMB on the same button on which it has now been released (press and hold LMB on any button in GUI, then move cursor away and release it to understand why), if yes, then we confirm that "Mouse Clicked" is valid, so we do whatever we want with the target object.

mouse_x and mouse_y are the locations of the cursor at the time of event. findTarget searches through our screen components to find if there is a control at current cursor position. Target will be our component, which will have a callback (foo()), that gets called when, and only when, user performs a full click on it.


While this gives you full context control, the findTarget isn't needed, but for future needs, you might as well tell it to return global handler object for mouse clicks.
Antheus I belive I understand your code. Just a one issues

turn++;if ((my >= 95) && (my <= 117) && (mx >= 95) && (mx <= 117) && (ClickLeft()) ){        Check_Box =( turn % 2 == 1 ? true : false );	}


I just Need ClickLeft to say false tell I lift off the moue button then it turns to false.

Never minbd I see the error of my ways.
No matter what I do it wont work because it either will be false or true all the time tell I click again. what I need is were I can click once and it will say I was clicked. Like your target pieace of code. But I am not sure how to start something like that.
the above code is in the main loop so it is ran every frame. I thinck I need it in a sequence like say if clickLeft = true then false then it auto defaults to true again so it as to go back false before it works.

Oh what a dumb @$$ I am all I need to do is say if clickLeft = true then Check_Box = false

then ClickLeft = false tell I release the button and press it again.

It helped to just wright this out plus the code you showed me got me on the right track I beleive Just let me know if I am going in the wrong direction.
never mind my ramblings my thoughts are all screwed up. What I thought wont work either.
After all the help I got on Gamedev and long hours I finaly got it solved. Thanks alot to every one that helped because with out you I could not of thought of a way to solve my issue. Thanks again.

bool ClickLeft(){ 	Click_Left = (mouse_state.rgbButtons[0] & 0x80) ? true : false;if (Click_Left == true) { //Click                        md.down = false;  Old_Click = true;}   else if (Old_Click == true && Click_Left == false )  {     //Click    md.down = true;    Old_Click = false;	   } else if (Click_Left == false && Old_Click == false){	md.down = false;	Old_Click = false;} 	return md.down;}


if there is a way I could optimize the above code just let me know thanks.

BIG FAT NOTE:
solved my issue by wrighting out what needs to be done in plain english then figured out what verbles I would need then
figured what all restraints I would need then wrote the code from that

so big ass reminder do this every time for every issue


I never really did that before now I will do that every time.

This topic is closed to new replies.

Advertisement