douejuan9162 2014-11-29 14:16
浏览 28

帆布多人游戏的插座

i have problem with my js canvas game. project seems mad god game. (rotmg in google) i use php socket to communication. when player change possition i send updated x and y:

function keysPressed(e) {
    keys[e.keyCode] = true;

    if (socket_open) {
        var msgg = {
            type : 'char_pos',
            px : pixX,
            py : pixY
        };

        websocket.send(JSON.stringify(msgg));
    }
}

server part:

//check for any incomming data
while(socket_recv($changed_socket, $buf, 1024, 0) >= 1)
{
    $received_text = unmask($buf); //unmask data
    $msg = json_decode($received_text); //json decode 
    $id=array_search($changed_socket,$clients);


        $pos[$id]['x']=$msg->px;
        $pos[$id]['y']=$msg->py;

    //prepare data to be sent to client
    $response_text = mask(json_encode(array('type'=>'rys_inni', 'id'=>$id, 'pos'=>$pos)));
    send_message($response_text); //send data
    break 2; //exist this loop
}

next, when data come back i do this:

websocket.onmessage = function (ev) {
    var msg = JSON.parse(ev.data);
    var type = msg.type; 

    if (type == 'rys_inni') {
        tab_innych_charow = [];
        for (key in msg.pos)
            if (typeof(msg.pos[key]) !== 'undefined')
                tab_innych_charow.push(msg.pos[key]);

    }
};

and this is in interval:

function rysuj() {

// clear canvas and bckground

    for (i = 0; i < tab_innych_charow.length; i++) {

        var x = tab_innych_charow[i].x; // +...;
        var y = tab_innych_charow[i].y; // +...;

        draw(ctx, something, x, y);
    }
}

the problem is that the different players drawing with lags and my game look poor. where i do mistake ? is the problem lies in php? i read about socket.io but i would rather php cause it is easier for me, i think. in my model (realm of the mad god) move is very fast and smooth. i don't know what to do. you are my last chance. thanks.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
    • ¥15 CSAPPattacklab
    • ¥15 一直显示正在等待HID—ISP
    • ¥15 Python turtle 画图
    • ¥15 关于大棚监测的pcb板设计
    • ¥15 stm32开发clion时遇到的编译问题
    • ¥15 lna设计 源简并电感型共源放大器
    • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
    • ¥15 Vue3地图和异步函数使用
    • ¥15 C++ yoloV5改写遇到的问题