Dec 25, 2009

Quick Tip: light switch effect

Here's a quick tip for novice programmers. Every time you want to create a "light-switch" effect for toggling a parameter on and off, or should I say true or false, 0 or 1, ... You can simply do it by typing:
p ^= 1;
This is a binary-safe Exclusive-OR assignment operator. First, it computes the value of p XOR 1 (p = p ^ 1) and then stores that value in p. If we look at the truth table of exclusive-or (XOR)

a

b

a XOR b

0

0

0

0

1

1

1

0

1

1

1

0

We see that if our b variable is 1 (true) the result of a ^ 1 (a XOR 1) is exactly the opposite of a. All in all, this operation is simply a shortcut for these assignments:
p = p ^ 1;

p = !p;

p = p ? false : true;

if(p) p = false; else p = true;

So if you want to toggle something ON and OFF, let's say when a user clicks a button, this method is extremely short and useful.

Dec 20, 2009

Facebook Connect: FB.Connect.streamPublish() does not show up

DUE TO FREQUENT FACEBOOK API CHANGES THIS ARTICLE IS OUTDATED.

Here are the reasons why FBJS functions sometimes fail:
  • xd_reciever.html is not installed correctly
  • application key is not set in FB.init()
  • Facebook Connect callback URL is not set in the application settings
  • Facebook JavaScript API is not set correctly in your HTML. This should be set immediately after the body tag <script src="http://static.ak.connect.facebook.com/js/api_lib/v0.4/XdCommReceiver.js" type="text/javascript"></script> This should be set before the closing body tag <script src="http://static.ak.connect.facebook.com/js/api_lib/v0.4/FeatureLoader.js.php/en_US" type="text/javascript"></script>
  • Facebook functions are called before JavaScript objects are initialized. FB.init(api_key, channel_path); FB.ensureInit(function() { // put your functions here });
  • All Facebook JavaScript functions are called BEFORE the FeatureLoader.js.php You should put your functions after this call and into FB.ensureInit() function
  • There are JavaScript errors in your code
If any of the errors mentioned above are met, the function will fail. If FBML tags are not rendered, you might also want to check, if you set the HTML header correctly. http://wiki.developers.facebook.com/index.php/XFBML Also, XFBML tags are required to render tags correctly.