FB

633 阅读1分钟

开始游戏(假如用HTML开发)

引入JS

<script type="text/javascript" src="https://connect.facebook.net/en_US/fbinstant.6.2.js"></script>

开始游戏

FBInstant.initializeAsync()
	.then(() => {
	FBInstant.startGameAsync()
		.then(() => {
			业务逻辑.............
		})
})

常用SDK

邀请好友(弹出好友框)

  • 发送邀请到messenger
FBInstant.context
	.chooseAsync()
	.then(function () {
		FBInstant.updateAsync({
		action: 'CUSTOM',
	// 按钮文字
	cta: {
		default: "Join Now",
		localizations: {
			zh_CN: "Join Now"
		}
	},
	image: 需要base64格式,
	text: {
	default: `${userName} invited you to play`,
	localizations: {
		zh_CN: `${userName} invited you to play`
		}
	},
	template: 'VILLAGE_INVASION',
	// 携带的参数
	data: {},
	strategy: 'IMMEDIATE',
	notification: 'NO_PUSH'
	})
})

获取广告

  • 地区当前没有广告或者请求广告过快都会拿不到广告

插屏广告

// 设置一个变量存储广告对象
let preloadedInterstitial = null
let ADid = 插屏广告ID
// 预加载广告
 FBInstant.getInterstitialAdAsync(
        ADid // Your Ad Placement Id
      ).then((interstitial) => {
        // Load the Ad asynchronously
        preloadedInterstitial = interstitial
        return preloadedInterstitial.loadAsync()
      }).then(function () {
        console.log('Interstitial preloaded')
      }).catch(function (err) {
        console.error('Interstitial failed to preload: ' + err.message)
      })
// 显示广告
preloadedInterstitial.showAsync()
        .then(() => {
          console.log('Interstitial ad finished successfully')
          // this.getAd()
        })
        .catch((e) => {
          // this.getAd()
          console.error(e.message)
        })

视频广告

// 设置一个变量存储广告对象
let preloadedRewardedVideo = null
let ADid = 视频广告ID
// 预加载广告
FBInstant.getRewardedVideoAsync(
        ADid
      ).then((rewarded) => {
        preloadedRewardedVideo = rewarded;
        return preloadedRewardedVideo.loadAsync();
      }).then(function() {
        console.log('Rewarded video preloaded')
      }).catch(function(err){
        console.error('Rewarded video failed to preload: ' + err.message);
      });
// 播放广告
preloadedRewardedVideo.showAsync()
	.then(function() {
	// Perform post-ad success operation
		console.log('Rewarded video watched successfully');
	})
	.catch(function(e) {
		console.error(e.message);
	});

分享游戏

FBInstant.shareAsync({
        intent: 'SHARE',
		// 图片要为base64
        image: img,
        text: 'Are you kidding me???',
		// 携带的数据
        data: {
            id: ...
        }
    })
	.then(function () {
    })

获取进入游戏时携带的参数

data = FBInstant.getEntryPointData()

获取排行榜数据(看下游戏里面的UTILS就知道了)

  • 必须要游戏开发者页面排行榜(网页托管下面)设置排行榜, 不然会提示没有找到排行榜
  • 设置排行榜要把"社交排行榜"选项点为否, 不然设置排行榜的时候会要求加上"context_id"

messenger