Jump to content

Phaser Facebook integration


Doyban
 Share

Recommended Posts

After many hours of looking on this forum and not only it's time to ask directly for help.

So guys my problem is how to integrate game request after click on exactly button.

Here is integration with Facebook via .html file and it works correctly, the problem is how to do request acction after click button. When I applied code for request in .html file it was good, the problem is, directly after login there was window to send request - I don't want like that, I want after click.
 

<script>
        window.fbAsyncInit = function() {
            FB.init({
              appId      : '611658819003576',
              xfbml      : true,
              version    : 'v2.6'
            });
            
            // ADD ADDITIONAL FACEBOOK CODE HERE
            
            function onLogin(response) {
              if (response.status == 'connected') {
                FB.api('/me?fields=first_name', function(data) {
                  var welcomeBlock = document.getElementById('fb-welcome');
                  welcomeBlock.innerHTML = 'Hello, ' + data.first_name + '!';
                });
              }
            }

            FB.getLoginStatus(function(response) {
              // Check login status on load, and if the user is
              // already logged in, go directly to the welcome message.
              if (response.status == 'connected') {
                onLogin(response);
              } else {
                // Otherwise, show Login dialog first.
                FB.login(function(response) {
                  onLogin(response);
                }, {scope: 'user_friends, email'});
              }
            }); 
        };
        
        (function(d, s, id){
            var js, fjs = d.getElementsByTagName(s)[0];
            if (d.getElementById(id)) {return;}
            js = d.createElement(s); js.id = id;
            js.src = "//connect.facebook.net/en_US/sdk.js";
            fjs.parentNode.insertBefore(js, fjs);
        }(document, 'script', 'facebook-jssdk'));
</script>


Now it's time for phaser code, I tryed many many ways, it seems to me this is the closest

create : function () {
//        this.onClick();
        this.invite();
    },
    
    onClick : function () {
        window.open("http://phaser.io");
    },
    
    invite : function () {
        window.fbAsyncInit = function() {
            FB.init({
              appId      : '611658819003576',
              xfbml      : true,
              version    : 'v2.6'
            }); 
        
            FB.ui({method: 'apprequests', message: 'Play with me in InfiniteHell!'
            }, function(response){
                console.log(response);
            });  
        };
        
        (function(d, s, id){
            var js, fjs = d.getElementsByTagName(s)[0];
            if (d.getElementById(id)) {return;}
            js = d.createElement(s); js.id = id;
            js.src = "//connect.facebook.net/en_US/sdk.js";
            fjs.parentNode.insertBefore(js, fjs);
        }(document, 'script', 'facebook-jssdk'));
    }

I guess it should be something with "window", the test "onClick" works perfectly. With Twitter share here should be just link, but with Facebook API I can't get it...

- Intel XDK Debugger after click (with active "invite" function) doesn't show any errors, just blank page,
- Firefox doesn't do anything, just stays in the same state - Menu, also without any errors in console
- Chrome just shows this kind of error, nothing more, but I'm not sure it's about that...

sdk.js:88 Uncaught TypeError: Cannot set property 'innerHTML' of null

sdk.js:88 Uncaught TypeError: Cannot set property 'innerHTML' of null

 

Any tips for beginner? It's my second game and first in Phaser, everything is working well expect integration with FB, maybe wanna help? ;)

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...