bartk Posted October 22, 2015 Share Posted October 22, 2015 Ok, so I've got this little thing going:people = game.add.group();...npc46 = people.create(1500, 880, 'npc2');...people.forEach(function(character) { character.scale.setTo(0.5, 0.5); character.inputEnabled = true; character.events.onInputDown.add(function () { Dialogue(character); }, this);}, this);...function Dialogue(character){ speechbubble = game.add.sprite(character.x, character.y - 170, 'speechbubble'); speech = game.add.text(character.x + 20, character.y - 150, "Hi, I'm " + character, { boundsAlignH: "left", fontSize: '12px', fill: 'black' });}I'd like the character to say it's own object name (for future purposes).However, the character's name (npc46) won't show up, only "[object Object]". Ok, fair enough, so I tried character.name. No luck. pls halp? Link to comment Share on other sites More sharing options...
ragnarok Posted October 23, 2015 Share Posted October 23, 2015 The easiest way is to store the name seperately in a new attribute of the object:people = game.add.group();...npc46 = people.create(1500, 880, 'npc2');npc46.myName="npc46";...people.forEach(function(character) { character.scale.setTo(0.5, 0.5); character.inputEnabled = true; character.events.onInputDown.add(function () { Dialogue(character); }, this);}, this);...function Dialogue(character){ speechbubble = game.add.sprite(character.x, character.y - 170, 'speechbubble'); speech = game.add.text(character.x + 20, character.y - 150, "Hi, I'm " + character.myName, { boundsAlignH: "left", fontSize: '12px', fill: 'black' });}Other things might get quite ugly:http://stackoverflow.com/questions/417645/how-to-convert-variable-name-to-string-in-javascript Link to comment Share on other sites More sharing options...
bartk Posted October 26, 2015 Author Share Posted October 26, 2015 Thanks ragnarok, that works for me. Link to comment Share on other sites More sharing options...
Recommended Posts