I am learning JavaScript with the p5js library and have decided to make a very basic game.
I simply want the game to end when the condition of an if statement is met. Such as
if (x == y) {
>>>endcode here<<<
}
however I need to add a tolerance of 50 to each value
so the code would be something like
if (x == y (give or take 50) ){
>>>endcode here<<<
}
I am unsure of how to add the tolerance to the statement so I have e here for some help. Thanks :)
I am learning JavaScript with the p5js library and have decided to make a very basic game.
I simply want the game to end when the condition of an if statement is met. Such as
if (x == y) {
>>>endcode here<<<
}
however I need to add a tolerance of 50 to each value
so the code would be something like
if (x == y (give or take 50) ){
>>>endcode here<<<
}
I am unsure of how to add the tolerance to the statement so I have e here for some help. Thanks :)
Share Improve this question edited Apr 27, 2019 at 11:33 Nick Parsons 51.2k6 gold badges57 silver badges78 bronze badges asked Apr 27, 2019 at 11:30 test_subject_8055test_subject_8055 651 silver badge6 bronze badges2 Answers
Reset to default 7Check to see if the absolute value of the difference is less than 50:
if (Math.abs(x - y) <= 50) {
// etc
}
I'm assuming you want to permit, eg 50 and 99, but prohibit 50 and 101. If you want the allow a difference of up to 100, then pare against 100 instead of 50:
if (Math.abs(x - y) <= 100) {
// etc
}
Or check upper and lower borders which you might want to change borders nonsymmetrically
if (x =< y+50 and x >= y-50 ){
>>>endcode here<<<
}
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745535335a4631879.html
评论列表(0条)