two images is loaded instead of one when first soc

2019-07-20 14:39发布

问题:

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!