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

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...