最近搜索

第11节 game_world场景 完成玩家进来,排位置。等代码

浏览:415
管理员 2021-10-10 06:06

玩家进来。

玩家出去。都要正常。



玩家离开 如何处理。


image.png

    start() {
    },
    doSocketMessage(data) {
        console.log("dosocketmessage");
        console.log(data);
        data = JSON.parse(data);
        switch (data.type) {
            case "play_join":
                //发送玩家进入的消息。由gama_world处理
                global.eventlistener.fire("play_join", data);
                break;
            case "leave":
                //发送玩家掉线的消息。由gama_world处理
                global.eventlistener.fire("leave", data);
                break;
            case "change_house_manager":
                //换房主的消息
                console.log("新房主clientid:" + data.new_house_manage.client.id);
                global.house_manage_id = data.new_house_manage.client.id;
                global.gameEventListener.fire("change_house_manager", data.new_house_manage.client.id);
                break;
            case "push_card":
                //发牌的消息 
                console.log("canvas---push_card消息!!!!");
                global.gameEventListener.fire("push_card", data);
                break;
            case "show_card":
                //接受牌的消息 。 看牌
                global.gameEventListener.fire("show_card", data);
                break;
        }
    }
    // update (dt) {},
});




image.png


        //接受人员掉线的信息
        global.gameEventListener.on("leave", function (data) {
            //data就obj对象 ("type", "leave");("msg","玩家掉线");("clientId", clientId);("roomId", roomId);
            console.log("game_world---leave--玩家掉线--" + JSON.stringify(data));
            let leaveClientId = data.clientId;
            console.log("leaveClientId:" + leaveClientId);
            for (let i = 0; i < self.playerNodeList.length; i++) {
                var playerNode = self.playerNodeList[i];
                if (playerNode.getComponent("player_node").getClientId() == leaveClientId) {
                    console.log(playerNode.getComponent("player_node").getClientId() + "此id离开了");
                    playerNode.removeFromParent(true);
                    playerNode.destroy();
                    self.playerNodeList.splice(i, 1);
                }
            }

            //玩家掉线。 位置 重新安排 
        })
        //接受人员掉线的信息

        //房主变换 的消息 
        global.gameEventListener.on("change_house_manager", function (clientId) {
            //data就obj对象
            console.log("game_world---change_house_manager--房主变换 的消息 --clientId:" + clientId);
            if(clientId==global.client.id){
                self.game_ready_ui.active = true;
            }
        })
        //房主变换 的消息




修改创建人的代码

image.png

    },
    createPlayer(playerData, index) {
        //客户是1  我是2    1 -2=-1 5
        let currIndex = index - this.index_;

        if(currIndex<0){
            currIndex = currIndex + 6 ;
        }

        let player = cc.instantiate(this.player_node_prefab);
        player.parent = this.node;
        player.getComponent("player_node").init(playerData,index);
        player.position = this.player_pos_list[currIndex];
        this.playerNodeList.push(player);
    },


联系站长

站长微信:xiaomao0055

站长QQ:14496453