Delete the text between the keys of a username

0

I have a game like agar.io and PIELES are used.

The skin is put in the player's name with { ... } . That is, {nombreDaLaPiel} Nickname .

The function that hides the name of the skin so that it can not be seen in the name

setName: function(name) {
  var reg = /\{([\w]+)\}/.exec(name);
  if (reg) if (reg.length === 2) {
    this.nameSkin = reg[1].toLowerCase();
    this.name = name.replace(reg[0], "").trim();
    return;
  }
  this.name = name;
}

The cell or game ball is a circle, and I want that, having a certain skin , the cell has a certain border color . For that I used:

if (this.name.indexOf("{chicken}") !== -1) {
  mainCtx.strokeStyle = "#D00606";
}

But the code does not work for me because indexOf() does not work when the name of the skin is hidden: as if it does not recognize it.

What can I do?


If I change the next line of the function:

this.name = name.replace(reg[0], "").trim();

Because of this:

 this.name = name.trim();

The edge does work, but the skin looks in the name.

Again, what can I do?

If more detail is needed, the complete code: link

    
asked by Eduardo Campos 29.01.2017 в 17:42
source

1 answer

2

At first glance the problem is that:

  • In this.name you initially have the "skin" of the player.
  • Then you "hide" the "skin" (you actually remove it, this is the problem).

When you "hide" the skin, in this.name and the "skin" is no longer, it is now in this.nameSkin .

Solution:

  • Method setName :

    setName: function(name) {
      var reg = /\{([\w]+)\}/.exec(name);
      if (reg && reg.length === 2) {
        this.nameSkin = reg[1].toLowerCase();
        this.name = name.replace(reg[0], "").trim();
        return;
     }
     // Si no tiene "piel"
     this.nameSkin = '';
     this.name = name;
    }
    
  • Validation of "skin":

    // Usar "nameSkin"
    if (this.nameSkin.indexOf("chicken") !== -1) {
          mainCtx.strokeStyle = "#D00606";
    }
    
answered by 30.01.2017 в 12:42