Register    Login    Forum    Search    FAQ

Board index » Everything




Post new topic Reply to topic  [ 9 posts ] 
Author Message
 Post subject: Question about video 4.3
 Post Posted: October 6th, 2017, 12:50 am 
 

Joined: October 6th, 2017, 12:41 am
Posts: 5
I just started watching these videos very inexperience with coding but trying my best so go easy on me.

I would like to know when we set the code to stop the instant increase in velocity while holding the key down that this code doesn't work.

So I have (bool StopIncrease = false;)

if (wnd.kbd.KeyIsPressed(VK_UP)) {
if (StopIncrease) { <------from my understanding this doesn't run unless true right?
StopIncrease = false;
}
else <-------- So this runs making StopIncrease = true;
{
vy = vy - 1;
StopIncrease = true;
}
}

So my questions is when it runs

if(StopIncrease) <---this is true now?
{
StopIncrease = false;
}

So why does it not set it to false the next time around?


Top 
 Profile  
Reply with quote  
 Post Posted: October 6th, 2017, 1:10 am 
 

Joined: June 26th, 2012, 5:38 pm
Posts: 766
Location: USA
if statements check to see if what is in the () evaluates to true. Operators such as == < > <= >= ! all return boolean values. If you simply put a variable inside the such as if(var) then it checks to see if var == true.

In summary
if(var) same as if(var == true)
if(!var) same as if(var == false)

_________________
Computer too slow? Consider running a VM on your toaster.


Top 
 Profile  
Reply with quote  
 Post Posted: October 6th, 2017, 1:23 am 
 

Joined: October 6th, 2017, 12:41 am
Posts: 5
So when my program runs and StopIncrease = true;
shouldn't this make it false? for the next frame?

if(StopIncrease){
StopIncrease = false;
}


Top 
 Profile  
Reply with quote  
 Post Posted: October 6th, 2017, 1:31 am 
Site Admin
User avatar

Joined: December 31st, 2011, 4:53 pm
Posts: 3493
Location: Japan
Yes, that will make it false. And then the next frame after that it will be made true. It will toggle with a period of 2 frames (frequency of 0.5 frames) ;)

_________________
Chili


Top 
 Profile  
Reply with quote  
 Post Posted: October 6th, 2017, 1:40 am 
 

Joined: October 6th, 2017, 12:41 am
Posts: 5
if (wnd.kbd.KeyIsPressed(VK_UP)) {
if (StopIncrease) {
}
else {
vy = vy - 1;
StopIncrease = true;
}
}
else
{
StopIncrease = false;
}

So the way you did it Chili does it toggle within (one) frame instead of (two)?


Top 
 Profile  
Reply with quote  
 Post Posted: October 6th, 2017, 2:00 am 
Site Admin
User avatar

Joined: December 31st, 2011, 4:53 pm
Posts: 3493
Location: Japan
It will not toggle at all. It will wait until key has been released before resetting StopIncrease to false.

_________________
Chili


Top 
 Profile  
Reply with quote  
 Post Posted: October 6th, 2017, 2:09 am 
 

Joined: October 6th, 2017, 12:41 am
Posts: 5
I think I get it, so the way I did won't stop the cursor from getting faster and faster because it doesn't care if my button has been released or not so it keeps adding more and more.

While your way says if the button has not been released don't add anymore.

right?


Top 
 Profile  
Reply with quote  
 Post Posted: October 6th, 2017, 3:15 am 
Site Admin
User avatar

Joined: December 31st, 2011, 4:53 pm
Posts: 3493
Location: Japan
Yup, it remembers if the button has been pressed in a previous frame, and does not add anymore.

Not until it is released, which resets the inhibiting variable and allows a subsequent press to trigger another add.

_________________
Chili


Top 
 Profile  
Reply with quote  
 Post Posted: October 6th, 2017, 11:20 pm 
 

Joined: October 6th, 2017, 12:41 am
Posts: 5
Thank you for the explanation!


Top 
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
 
Post new topic Reply to topic  [ 9 posts ] 

Board index » Everything


 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for: