Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

267
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!