Jump to content

Get object name as string


bartk
 Share

Recommended Posts

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

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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...