Jump to content

groupName.create where groupName is a variable doesn't work


TijmenH
 Share

Recommended Posts

Hello,

 

I've got a function where I pass groupName as parameter. Now I want to create a sprite like this:

object = groupName.create( x, y,objectImage);

but it doesn't read the groupName as parameter/variable. It just looks for a function called groupName.create.

 

How can I make sure javascript replaces the groupName with the string that's passed as parameter?

 

Thanks in advance!

Link to comment
Share on other sites

You can't. I'm assuming "groupName" is a string, like the actual variable you passed to "game.add.group"? That won't work.

 

What you *could* do is make a new variable, "name2Group = {};" and then do something like "name2Group[groupName] = game.add.group(game.world, groupName);". Then you can say "name2Group[groupName]" later and get the group back.

Link to comment
Share on other sites

Ah, I see.

 

Too bad, I fixed it another way using a switch statement. Maybe not the best option but it would be too much reworking to have your solution working.

 

I already did a switch statement for all the groups (so if groupName is 'hunters' it already had some group specific code to scale, set animations etc) I just create the objects for the groups in there now.

 

Thanks for helping me!

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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