Sign in to follow this  
evilwizard

Adding sprites and adding them to groups in the same line

Recommended Posts

I looked at the documentation on how to create a sprite

https://phaser.io/docs/2.6.2/Phaser.GameObjectFactory.html#sprite

It takes in the following parameters

  1. x
  2. y
  3. key
  4. frame
  5. group

It's very easy to add a sprite with just the first three parameters

car1 = this.game.add.sprite(800, 400, 'car');

But it fails when I try to add the car to a group

car1 = this.game.add.sprite(800, 400, 'car', objects);

If I went with the first declaration and then added

objects.add(car1)

it will then work

I need to use the second declaration because my codebase is becoming way too large, and I need fewer lines.

Not sure, why this is failing. Maybe an update killed this functionality?

Share this post


Link to post
Share on other sites
46 minutes ago, samme said:

You need to omit frame (4):


car1 = this.game.add.sprite(800, 400, 'car', null, objects);

 

I would like to thank you for your quick response.

Indeed, you must pass in something for all of the parameters, even if it's null. I got confused because the documentation said they were optional. Adding null to the frame parameter solves this issue.

Once again, thank you. This was very helpful for me

Share this post


Link to post
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...
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.