two images is loaded instead of one when first socket connects - Node.js

你。 提交于 2019-12-24 02:08:38

问题


I'm trying to implement a multiplayer feature to my game. I'm using node.js for this. When one person connects, an image is supposed to be loaded to the browser. Instead it loads two images.

Heres is part of my class

var count = 0;          //Keep track of which player we're oparating on
function Car(id){
    var that = this;    //Reference to 'this'

    this.loadHero = function(){
        that.id = document.getElementById(id); //Store the id of our charakter
        $(that.id).css({"display" : "block"});
    }
}

node.js on client side:

var socket = io.connect('http://localhost:3000');

    socket.on('entrance', function(data){
        console.log(data.message);
        var num = (count > 0) ? count : '';
        console.log("hero" + num);
        var hero = new Car("hero" + num);
        hero.init();
        count++;
    });

When i start my server and connect. My console shows me the following

hero is the id of my first image (first player the connects). hero1 is the second player and so on. So I don't understand why both my images is loaded when one player connects.

Any help is greatly appreciated!

来源:https://stackoverflow.com/questions/19202615/two-images-is-loaded-instead-of-one-when-first-socket-connects-node-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!