MX Player - Docking of Battle Games (双人对战游戏接入)

1. Game Init (游戏初始化)

After the game is loaded, execute it in the entry class: (游戏加载完成后在入口类中执行)
onGameInit();
onGameStart();

/*** !#en Game initialization* !#zh 游戏初始化*/
export let onGameInit = function () {if (!DeviceUtil.IsMXGame) {return;}//获取游戏配置let info = gameManager.onGameInit();let gameConfig: GameConfig = JSON.parse(info);gameId_ = gameConfig.gameId;//!#zh 如需接入商城,userId_===""时为未登录用户,需要提示用户登录之后才能打开商城//!#en If you need to access the mall, userId_==="" is a non-login user, you need to prompt the user to log in before opening the malluserId_ = gameConfig.userId;//!#zh isFirstOpen===true 用户首次打开游戏,如需新手引导可用此字段判断 //!#en isFirstOpen===true The user opens the game for the first time, if you need novice guidance, you can use this field to judgeif (gameConfig.isFirstOpen !== null && gameConfig.isFirstOpen !== undefined) {is_Guide = gameConfig.isFirstOpen;}gameInitComplete = true;
};/*** !#en Game Start  If the loading image is used in the game zip package, you need to call this method to clear the loading image* !#zh 游戏开始  如果游戏zip包中用了loading图需要调用此方法清除loading图片*/
export let onGameStart = function () {if (!DeviceUtil.IsMXGame) {return;}gameManager.onGameStart();
};

2. Start matching(开始匹配)

Call the onGameLoaded() method to start matching, and the matching data will be returned by cc.game.emit("commonEventInterface").
After obtaining the matching data, you need to call onGameCleanPosters() to close the App layer matching interface, and then perform the in-game matching animation and connect to the in-game server.

调用 *onGameLoaded()方法开始匹配,匹配数据会通过cc.game.emit("commonEventInterface")*返回 。
获取到匹配数据后需调用
onGameCleanPosters()**关闭App层匹配界面,然后进行游戏内匹配动画和连接游戏内server。

/*** !#en The game is loaded, the App layer matching page is displayed and the matching starts* !#zh 游戏加载完成,App层匹配页面显示并开始匹配*/
export let onGameLoaded = function () {console.log("---------------:" + game_version);if (!DeviceUtil.IsMXGame) {return;}let info = { GameVersion: game_version };gameManager.onGameLoaded(JSON.stringify(info));//!#en Record the start time of the game//!#zh 记录游戏开始时间game_start_time = Date.now();//!#en App will emit a commonEventInterface event to return events and data//!#zh App会发射commonEventInterface事件返回事件和数据cc.game.on("commonEventInterface", commonEventInterface_battle, this);
};/*** !#en General event interface* !#zh 通用事件接口* @param event_name 事件名* @param info 信息*/
export let commonEventInterface_battle = function (event_name: string, info: any) {switch (event_name) {case "backPressed": {cc.game.emit("userGamePausedCall");cc.game.emit("userPausedViewShowCall");break;}case "screenOn": {break;}case "screenOff": {cc.game.emit("userGamePausedCall");cc.game.emit("userPausedViewShowCall");break;}case "pagePause": {cc.game.emit("eventGamePausedCall");cc.game.emit("eventMusicPausedCall");break;}case "pageResume": {cc.game.emit("eventGameResumeCall");cc.game.emit("eventMusicResumeCall");break;}case "userPresent": {//解锁屏幕break;}//!#en Battle data//!#zh 对战数据case "userMatched": {battleInfo(info);break;}}
};/*** !#en Close App layer matching interface* !#zh 关闭App层匹配界面*/
export let onGameCleanPosters = function () {if (!DeviceUtil.IsMXGame) {return;}gameManager.onGameCleanPosters();
};

3. Battle Game Over(游戏结束)

Call onBattleGameOver() at the end of the game, you can also call this method if you want to force the end of the game.

游戏结束调用 onBattleGameOver() ,想强行结束游戏也可调用此方法。

/*** !#en GameOver* !#zh 游戏结束*/
export let onBattleGameOver = function () {if (!DeviceUtil.IsMXGame) {return;}if (is_over) {return;}is_over = true;let myScore = 0;let otherScore = 0;for (let i = 0; i < 3; i++) {for (let j = 0; j < 3; j++) {if (gameStore.player1Data.completeBlockArr[i][j] === 1) {myScore++;}if (gameStore.player2Data.completeBlockArr[i][j] === 1) {otherScore++;}}}let info = {gameId: gameId_,battleId: battleId_,roomId: roomId_,battle_info: {currentscore: myScore,oppoScore: otherScore,winStatus: gameStore.winType,numA: gameStore.selfMoveCount,numB: gameStore.otherMoveCount,numC: gameStore.createPropsNum,numD: gameStore.usePauseSkillCount,numE: gameStore.useSwapSkillCount,numF: gameStore.useRandomSkillCount,numG: gameStore.beUsedPauseSkillCount,numH: gameStore.beUsedRandomSkillCount,numI: gameStore.selfGetPropNum,numJ: gameStore.otherGetPropNum,oppoType: gameStore.isAI,oppoUserID: gameStore.player2Data.id,},};//!#en Quit the game report//!#zh 退出游戏打点/**********************************************************************************************/let rInfo = {roomID: battleId_,playtime: Math.round((Date.now() - game_start_time) / 1000)};gameManager.onTrack("gameExit", JSON.stringify(rInfo));/**********************************************************************************************///!#en This is a custom report and can be ignored//!#zh 此为自定义打点,可忽略const emojiClickedInfo = JSON.stringify(gameStore.expressionStatistic);gameManager.onTrack("emojiClicked", emojiClickedInfo);//!#en info is the reported data at the end of the game//!#zh info是游戏结束的上报数据gameManager.onBattleGameOver(JSON.stringify(info));
};

Attach the complete code at the end:
最后附上完整代码:

import { conditionId } from "../Manager/DataManager";
import SocketManager from "../Manager/SocketManager";
import UIManager from "../Manager/UIManager";
import { gameStore, WinType } from "../store/GameStore";
import MatchUI from "../UI/Match/MatchUI";
import DeviceUtil from "../utils/DeviceUtil";//!#en The game version is synchronized with the server
//!#zh 游戏版本与服务器同步
export let game_version: string = "1.0.0";
//!#en Game ID
//!#zh 游戏ID
export let gameId_: string = "";
//!#en Battle ID
//!#zh 对战ID
export let battleId_: string = "";
//!#en Room ID
//!#zh 房间ID
export let roomId_: string = "4f4b7518761dc9533f9f2cc4c77b561d_c4d2cgjqgkce9tkehg5g";
//!#en User ID
//!#zh 用户ID
export let userId_: string = "";
//!#en Game start time,Used to end the reported game duration
//!#zh 游戏开始时间,用于结束上报游戏时长
export let game_start_time: number = 0;
//!#en Novice guidance Or Not
//!#zh 是否新手引导
export let is_Guide: boolean = false;
//!#en Is game init complete?
//!#zh 初始化游戏配置是否获取完成
export let gameInitComplete: boolean = false;/*** !#en GameInit return Object*/
export interface GameConfig {highestScore: number;lastLevel: number;roomType: string;gameId: string;gameName: string;userId: string;isFirstOpen: boolean;source: string;startType: string;rewardType: string;appVersion: number;appId: string;trackInfo: string;roomId?: string;tournamentId?: string;
}/*** Battle data Object*/
interface info {status: string,roomId: string,connectUrl: string,mapId: number,gameVersion: string,users: user[],battleId: string,gameId: string,selfUserId: string
}/*** User data Object*/
interface user {type: string,userId: string,playerId: string,name: string,avatar: string,score: number,playerData?: any
}export let setGameId = function (id: string) {gameId_ = id;
};export let setBattleId = function (id: string) {battleId_ = id;
};export let setRoomId = function (id: string) {roomId_ = id;
};export let setGuide = function (b: boolean) {is_Guide = b;
};/*** !#en Game initialization* !#zh 游戏初始化*/
export let onGameInit = function () {if (!DeviceUtil.IsMXGame) {return;}//获取游戏配置let info = gameManager.onGameInit();let gameConfig: GameConfig = JSON.parse(info);gameId_ = gameConfig.gameId;//!#zh 如需接入商城,userId_===""时为未登录用户,需要提示用户登录之后才能打开商城//!#en If you need to access the mall, userId_==="" is a non-login user, you need to prompt the user to log in before opening the malluserId_ = gameConfig.userId;//!#zh isFirstOpen===true 用户首次打开游戏,如需新手引导可用此字段判断 //!#en isFirstOpen===true The user opens the game for the first time, if you need novice guidance, you can use this field to judgeif (gameConfig.isFirstOpen !== null && gameConfig.isFirstOpen !== undefined) {is_Guide = gameConfig.isFirstOpen;}gameInitComplete = true;
};/*** !#en Game Start  If the loading image is used in the game zip package, you need to call this method to clear the loading image* !#zh 游戏开始  如果游戏zip包中用了loading图需要调用此方法清除loading图片*/
export let onGameStart = function () {if (!DeviceUtil.IsMXGame) {return;}gameManager.onGameStart();
};/*** !#en The game is loaded, the App layer matching page is displayed and the matching starts* !#zh 游戏加载完成,App层匹配页面显示并开始匹配*/
export let onGameLoaded = function () {console.log("---------------:" + game_version);if (!DeviceUtil.IsMXGame) {return;}let info = { GameVersion: game_version };gameManager.onGameLoaded(JSON.stringify(info));//记录游戏开始时间game_start_time = Date.now();cc.game.on("commonEventInterface", commonEventInterface_battle, this);
};export let is_over: boolean = false;
/*** !#en Close App layer matching interface* !#zh 关闭App层匹配界面*/
export let onGameCleanPosters = function () {if (!DeviceUtil.IsMXGame) {return;}gameManager.onGameCleanPosters();
};/*** !#en GameOver* !#zh 游戏结束*/
export let onBattleGameOver = function () {if (!DeviceUtil.IsMXGame) {return;}if (is_over) {return;}is_over = true;let myScore = 0;let otherScore = 0;for (let i = 0; i < 3; i++) {for (let j = 0; j < 3; j++) {if (gameStore.player1Data.completeBlockArr[i][j] === 1) {myScore++;}if (gameStore.player2Data.completeBlockArr[i][j] === 1) {otherScore++;}}}let info = {gameId: gameId_,battleId: battleId_,roomId: roomId_,battle_info: {currentscore: myScore,oppoScore: otherScore,winStatus: gameStore.winType,numA: gameStore.selfMoveCount,numB: gameStore.otherMoveCount,numC: gameStore.createPropsNum,numD: gameStore.usePauseSkillCount,numE: gameStore.useSwapSkillCount,numF: gameStore.useRandomSkillCount,numG: gameStore.beUsedPauseSkillCount,numH: gameStore.beUsedRandomSkillCount,numI: gameStore.selfGetPropNum,numJ: gameStore.otherGetPropNum,oppoType: gameStore.isAI,oppoUserID: gameStore.player2Data.id,},};//!#en Quit the game report//!#zh 退出游戏打点/**********************************************************************************************/let rInfo = {roomID: battleId_,playtime: Math.round((Date.now() - game_start_time) / 1000)};gameManager.onTrack("gameExit", JSON.stringify(rInfo));/**********************************************************************************************///!#en This is a custom report and can be ignored//!#zh 此为自定义打点,可忽略const emojiClickedInfo = JSON.stringify(gameStore.expressionStatistic);gameManager.onTrack("emojiClicked", emojiClickedInfo);//!#en info is the reported data at the end of the game//!#zh info是游戏结束的上报数据gameManager.onBattleGameOver(JSON.stringify(info));
};/*** !#en General event interface* !#zh 通用事件接口* @param event_name 事件名* @param info 信息*/
export let commonEventInterface_battle = function (event_name: string, info: any) {switch (event_name) {case "backPressed": {cc.game.emit("userGamePausedCall");cc.game.emit("userPausedViewShowCall");break;}case "screenOn": {break;}case "screenOff": {cc.game.emit("userGamePausedCall");cc.game.emit("userPausedViewShowCall");break;}case "pagePause": {cc.game.emit("eventGamePausedCall");cc.game.emit("eventMusicPausedCall");break;}case "pageResume": {cc.game.emit("eventGameResumeCall");cc.game.emit("eventMusicResumeCall");break;}case "userPresent": {//解锁屏幕break;}//!#en Battle data//!#zh 对战数据case "userMatched": {battleInfo(info);break;}}
};/*** !#en Battle data analysis* !#zh 对战数据解析* @param info*/
export let battleInfo = (info: any) => {console.log("battleinfo" + info)let obj: info = JSON.parse(info);setGameId(obj.gameId);setBattleId(obj.battleId);setRoomId(obj.roomId);let selfUserId: string = obj.selfUserId; // 自己的 用户IDlet my_index = 0;let other_index = 1;if (obj.users[1].userId == selfUserId) {my_index = 1;other_index = 0;}gameStore.player1Data.setId(obj.users[my_index].playerId);gameStore.player1Data.setNickName(obj.users[my_index].name);gameStore.player1Data.setAvatar(obj.users[my_index].avatar);gameStore.player2Data.setId(obj.users[other_index].playerId);gameStore.player2Data.setNickName(obj.users[other_index].name);gameStore.player2Data.setAvatar(obj.users[other_index].avatar);//!#en Can get the other party's data, such as the skin used by the other party//!#zh 可获取对方数据,如对方使用的皮肤let playerData = obj.users[other_index].playerData;if (playerData && playerData[conditionId.UseSkin]) {let player2BlockSkin = playerData[conditionId.UseSkin].split("|")[0];gameStore.setPlayer2BlockSkinId(Number(player2BlockSkin) % 100000);} else {gameStore.setPlayer2BlockSkinId(1);}//!#en websocket address//!#zh websocket地址SocketManager.instance.connecturl = obj.connectUrl;//!#en game start report//!#zh 游戏开始打点let rInfo = {roomID: battleId_,};gameManager.onTrack("gameStart", JSON.stringify(rInfo));//!#en Close App layer matching interface,If there is a matching animation in the game, it can be called after this//!#zh 关闭平台匹配页面,如游戏内有匹配动画,可在此之后调用onGameCleanPosters();UIManager.instance.OnMsg(MatchUI, "", null);
};

mx.d.ts

declare namespace gameManager {declare function onGameLoaded(paramStr: string): void;declare function onGameCleanPosters(): void;declare function onTrack(eventName: string, paramStr: string): void;declare function onBattleGameOver(paramStr: string): void;declare function onGameStart(): void;declare function onGameInit(): string;
}

mxplayer battle游戏接入相关推荐

  1. Unity游戏接入TypeSDK集成笔记

    前言: 这是一个unity小程序猿使用typesdk给公司项目接入各渠道游戏联运sdk的使用笔记,说是笔记是因为从接触这个聚合工具到接入项目中并且将项目上线到渠道(目前已上线oppo,huawei,a ...

  2. 微信小游戏接入遇到的坑

    微信小游戏接入遇到的坑 1.微信web开发工具必须安装到C盘,才能被egret wing自动调取. 2.exml文件不能放在src文件夹,必现放在resource文件夹 3.egret Launche ...

  3. untiy游戏接入之uc_sdk(九游)

    概述 接入心德 游戏接入是比较费时间的,也行少些一个字母或多了一个字母,有时候都有可能需要重做,所以一定不要怕麻烦,多余渠道方的技术交流. 本文描述 本文是按照uc6.1.0sdk的文档进行接入的,具 ...

  4. 帮你抢小游戏流量红利——360小游戏接入指南

    360小游戏是指360 PC浏览器打开的桌面小程序运行平台 接入平台请戳这里 接入文档请戳这里 (本文根据2019年10月 360平台api和sdk版本总结,后续360平台做了很多优化) 接入不方便的 ...

  5. CocosCreator微信小游戏接入微信登录获取微信名、头像、经纬度等信息

    前言 微信小游戏接入微信登录还是很简单的,不像原生平台开发,还需要提供appid,appsecret等信息,并有一系列的和微信平台的交互,才能最终授权成功. 下面TS代码演示了,老的接入流程. exp ...

  6. 最近想给自己的Unity游戏接入广告

    最近想给自己的游戏接入广告,就记录下了一篇很优秀的博客 需要的朋友可以看看 https://blog.csdn.net/qq_36622009/article/details/104818876

  7. Egret微信游戏接入

    自学开发笔记,有兴趣的同学请关注微信WiGameFun,不定时分享游戏开发相关技术.有不对的地方烦请指点修正.​ Egret微信游戏接入 ​前面几篇都是整理如何使用Egret引擎或者与它相关的一些开发 ...

  8. Pygame学习笔记11:三角函数及Tank Battle游戏

    这一次将运用三角函数的相关知识以及前面学过的相关知识,如声音.精灵图像等来设计Tank Battle游戏,即坦克大战. Tank Battle游戏 为了实现该游戏,需要再向MyLibrary.py文件 ...

  9. (转)申请企业级IDP、真机调试、游戏接入GameCenter 指南、游戏接入OpenFeint指南;...

    Himi 原创, 转载请注明出处,谢谢! 原文地址:http://blog.csdn.net/xiaominghimi/article/details/6913967 这里Himi给出对于开发iOS的 ...

  10. 帮你抢小游戏流量红利——vivo小游戏接入指南

    vivo小游戏接入指南 一.平台介绍 VIVO小游戏运行在VIVO手机自带的游戏中心,活跃用户接近上亿,目前小游戏产品接入需要商务审核,审核通过后方可上线. 开放平台:https://dev.vivo ...

最新文章

  1. java tomcat源码_详解Tomcat系列(一)-从源码分析Tomcat的启动
  2. Go-json解码到接口及根据键获取值
  3. php中在使用js_提交的表单不为空_为什么显示等于,php编程,这段代码为什么不能阻止表单的提交!不管为不为空 都跳转到1.php页面啦 这是怎么回事?...
  4. 思考、学习新技术的原则和方式
  5. HDU 1083 Courses 匹配
  6. HP刀片服务器C7000-Cisco网络模块配置指南
  7. 电子工业出版社博文视点图书在微软VS2010全球发布会上受追捧
  8. html session修改,html session
  9. 西门子/AB/ModbusTCP/FX3U 安卓手机app软件,二代Teslascada2电脑组态版本app Runtime
  10. 如何搭建前端开发环境
  11. java web后台生成随机数字字母验证码
  12. ORACLE介质管理库MML
  13. 微软C++团队将出席ACCU 2021
  14. 大数据营销模型思路架构
  15. 2022 前端常用的开发工具、组件库等等~持续整理,待你分享~
  16. 2006年十二生肖运势(收藏)
  17. V4L2视频采集与H264编码1—V4L2采集JPEG数据
  18. 像360悬浮窗那样,用WindowManager做一个炫酷的悬浮迷你音乐盒(上)
  19. mysql分组排序后加序号
  20. Linux系统下返回上一级目录的命令

热门文章

  1. 交换排序算法之快速排序-C语言版(带图详细)
  2. python nlp 中文伪原创_人工智能伪原创工具(AI伪原创)
  3. 计算机网络编程基础知识总结思维导图
  4. 苹果iPad mini 5蜂窝数据版上架:3896元起
  5. 华为P50/P50Pro怎么解锁huawei P50pro屏幕锁开机锁激活设备锁了应该如何强制解除鸿蒙系统刷机解锁方法流程步骤不开机跳过锁屏移除锁定进系统方法经验
  6. 【转】MapGIS K9基础系列(二)
  7. 冒泡排序java实现
  8. 最新的省市区三级地区MySQL数据库,附带获取方法
  9. sht20中写用户寄存器_哪位帮忙看看下,程序读取SHT20 的温度时就不行,无ACK反馈了...
  10. 广东百望税控盘初始化设置