Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

269
Visualizações
Passing a hex number that includes letters from an array of hex numbers to .setTint() not working properly

I have a following array of hex numbers, that are different characters in the game.

gameState.charactersArray = [0xff5c33, 0x324563, 0x000000, 0xffffff, 0x003300];

And if I try to access a number that includes letters, so indeces 0 and 3, then the character simply isn't displayed (btw, I'm not getting any errors logged to the console). The hex numbers from that array composed only by numbers, so indeces 1, 2 and 4 work fine.

Also, if in the line:

gameState.character.setTint(gameState.charactersArray[gameState.characterIndex]);

I put any of the hex numbers with letters, for example 0xff5c33 instead of gameState.charactersArray[gameState.characterIndex] then it works fine too.

I just want to be able to set the character's color based on user's choice. Also gameState.characterIndex works fine, no issues there.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Since hex numbers are numbers and both are treated the same (in javascript), it should not matter, if you use the hex literal or the decimal form. So the problem is probably somewhere else, maybe the gameState.characterIndex is not correct, or the tint is added serveral times?

But if you want to be on the save side, you could use the phaser Color object, with this you can convert into string, numbers, or access the single components.

Here a demo, illustrating the previous mentioned points:
(if you use the color object, I would use it at the initialisation of the array, like let colors = [Phaser.Display.Color.ValueToColor(0xff5c33),..] or so.

document.body.style = 'margin:0;';

var config = {
    type: Phaser.AUTO,
    width: 536,
    height: 183,
    backgroundColor: 0xcdcdcd,
    scene: {
        preload,
        create
    },
    banner: false
}; 

function preload(){
    this.load.image('img', 'http://labs.phaser.io/assets/sprites/copy-that-floppy.png');
}

function create () {
   
   let colors = [0xff5c33, 0x324563, 0x000000, 0xffffff, 0x003300];
   
   for(let idx = 0; idx < colors.length; idx++){
       let currentColor = colors[idx];
       let colorObj = Phaser.Display.Color.ValueToColor(currentColor); 
       let colorString = Phaser.Display.Color.RGBToString(colorObj.r, colorObj.g, colorObj.b);
       
       let img = this.add.image(10 + (idx * 90), 10 , 'img').setScale(.5).setOrigin(0);
       let text = this.add.text(10 + (idx * 90), 90, `${currentColor}\n${colorString}`, {color: colorString});
       img.setTint(currentColor);
   }
}

new Phaser.Game(config);
<script src="https://cdn.jsdelivr.net/npm/phaser@3.55.2/dist/phaser.js"></script>

Update (from the comments):
the array values for index 0 and 3 must have been overwriten somewhere.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda