Using function parameter to access different objects inside properties

properties: {
    balloon0btl: {
        default: null,
        type: cc.RichText
    },
    balloon1btl: {
        default: null,
        type: cc.RichText
    },
    balloon2btl: {
        default: null,
        type: cc.RichText
    },
},

i have this objects in my script they are different richtexts i wanna choose one depending on the paramater of function and change its strings value

here is what i tried and didnt work

var selectb = function (be) {
var n = String("balloon"+be+"btl");
this.n.string = selectedtxt;
};
selectb(cbal);

i know this isnt exactly a problem related to cocos but i hope you guys can help thanks in advance

I would create an array property instead. Since your “be” parameter is the index, this should map pretty easily to the array.

1 Like

can you give an example

You should be able to access the property with this[n]

So in your case
this[n].string = selectedtxt;

1 Like

Sure. I am using Typescript, so you would declare a property member like this:

@property([cc.RichText])
public balloons: cc.RichText[] = [];

And you can access them by using:

const balloon = this.balloons[be]; // in this case, "be" is a number, not a string.
// do whatever you want to the balloon object (which is a cc.RichText).

this is how i do it with js

balloons: [cc.RichText],

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.