keysabyl Posted June 1, 2017 Share Posted June 1, 2017 Is it possible to fill an array to add text, like in this example: for(var i:number = 0; i < 15; i++){ text = this.add.text(1710,70 + (i*67),this.array, { font: "65px Arial", fill: "#8b0000", align: "center" });} I get this error: Cannot set property '0' of undefined Link to comment Share on other sites More sharing options...
Nesh108 Posted June 1, 2017 Share Posted June 1, 2017 @keysabyl, I don't have the chance to test it but shouldn't it be something like this? var size = 3; var labels = []; var strings = ["hello", "keysabyl", "!"]; for(var i:number = 0; i < size; i++){ labels.push(this.add.text(1710,70 + (i*67), strings[i], { font: "65px Arial", fill: "#8b0000", align: "center" })); } First of all you might want to store those labels inside an array for later use. Then you are just passing in an array while it needs a string. After this, you have your labels and you can do anything with them Link to comment Share on other sites More sharing options...
mattstyles Posted June 1, 2017 Share Posted June 1, 2017 Why muck around with a for...loop when Array.join has your back? this.add.text(1710, 70 + (i * 67), array.join(' '), styles) Or, for your 15 text elements (with a slight functional twist he he): function foo () { const arr = ['JS', 'is', 'awesome', '!'] const styles = {font: '65px Arial'} new Array(15) .fill() .map((v, i) => i) .forEach(i => { Game.add.text(1710, 70 + i * 67, arr.join(' '), styles) }) } Join is non-destructive and will return a string whilst leaving the underlying array (or array-like) structure unmodified (I probably should not have used const to declare the text array here as its potentially misleading, its the pointer to the array which becomes constant, not the array itself so you still need to be mindful which array functions perform mutations—such as push and pop—and which dont—such as join or concat). Link to comment Share on other sites More sharing options...
keysabyl Posted June 2, 2017 Author Share Posted June 2, 2017 Thanks for the answers, my problem is solved! mattstyles 1 Link to comment Share on other sites More sharing options...
Recommended Posts