Html 5 canvas, Controlling an object instance from a variable

New Here ,
May 06, 2021 May 06, 2021

Copy link to clipboard

Copied

Hi, this is my first time asking here and I think I got a super dumb error, I have a movieclip which contains several dynamic text fields that must be filled according to a player ranking, so I tought doing something like this:

window._this = this;

//ARRAY RANKING, this is the array that gets the time(tiempo) and the player(jugador) //
window._arrayRanking = [
{jugador:_jugadorActivo,tiempo:_tiempoFinal},
];

//// then I use this function, I use the console a lot to check my errors///

_this.Ranking = function() {
console.log(_arrayRanking.sort((a, b) => a.tiempo - b.tiempo ));
console.log(_arrayRanking.length);

for (let i = 0; i < _arrayRanking.length; i++) {
_this.mc_ranking.jugador_[i].text="Jugador "+_arrayRanking[i].jugador;
_this.mc_ranking.tiempo_[i].text=_arrayRanking[i].tiempo;

console.log("el for funciona");


}};

 

My brain tells me that this must work, even when i turn this line (_this.mc_ranking.jugador_[i]) into a variable (var jugadorR="_this.mc_ranking.jugador_"+i, and then try jugadorR.text= "xxx")  and put it in the console it shows _this.mc_ranking.jugador_0 or _1 but for some reason it doesnt control the text field which i need to control, it also happens when i try to control a movieclip instance (say _this.mc_ranking.jugador_[i].visible= false;) so as a Machete type solution i used a switch case for every one of the 15 players which gave me like 100 more lines of code. I want to know why that happens and how can I solve it in the future, thanks. 

Views

94

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
May 06, 2021 May 06, 2021

Copy link to clipboard

Copied

LATEST

First, you should be assigning "_this" to a local variable, not the global window object.

 

Second, the only reason to stash a copy of "this" is for code that will be called from an event handler, which you are not doing here.

 

Third, using "let" instead of "var" will cause your code to crash on IE11, which is still used by millions of people.

 

Fourth, this: "jugador_[i]" is trying to access elements of an array named "jugador_". You don't have an array named that, so of course it's failing. Same for "tiempo_[i]". I'm guessing what you're wanting to do is actually this:

_this.mc_ranking["jugador_" + i].text

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Property_accessors

 

 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines