• Promotion
  • useful thing i found out about yesterday i thinks

for programmers
something useful if you have a variable you want to change back and forth from 0 to 1

code that people usually use:
function toggleFrom0to1(){
if(x === 0){
x = 1;
}else{
x = 0;
}
}

thing thats useful:
var x = 0;
function toggleFrom0to1(){
x = 1-x;
}

    or you could just use a bitwise xor operator to change between both as well
    Examples:
    var a = 0;
    a = !a // this will give us false but in js false is considered as 0 rather than one
    a ^= 1; // however this is my favorite method converts all matching 0's to 1 and 1's to 0
    /*
    as you can see there really isn't much need for a function for something simplistic as this
    by using xor(^) makes it really simple by converting matching numbers that are 0 to 1 and reducing 2 1's in corresponding places to 0 so 0b001(1) ^ 0b001(1) returns [0] & 0b000(0) ^ 0b001(1) returns [1] and from what i remember bitwise operators are pretty fast so it could also be a minor boost if this is what your looking for
    */

    You can use ``` to create entire blocks of code. Like this:

    function myFunction(input) {
      return input+2;
    }

    ```js
    function myFunction(input) {
    return input+2;
    }
    ```

    ackvonhuelio Is there a way to detect if a value, for example, sprite.y, is between 2 numbers?

    I don't know how I didn't realize that already lol

    One more question: how do you round the result of a number to a closest custom integer(i.e. rounding sprite.rotation of 63 to 90, or 201 to 180)?

      esw

      function angleCap(num) {
          let angle = num % 360 // provides a number within 360
          for (var deg = 0; deg <= 360; deg += 90) {
              if (deg >= angle) { return(deg) }
          }
      }
      /*
      Note this expression should only be invoked when rounding
      However if we are doing integer rounding between two ranges maybe something like this would be better
      */
      function angleCap(num){
          return Math.round(num % 360 / 90) * 90
      } // we can also use Math.floor & Math.ceil to also change the effects of rounding

      even better:

      var x = true;

      x = !x;

      will make it false
      and if false will make true

      activate with a button or something

        Unknowing-Thorax I actually posted that before up top and another method that i do like a bit better than using the ! operator it's actually the 1st reply

        15 days later

        This code. I feel like I have seen it before somewhere. I just don't know where. hmm

        Chat