Jump to content

Containers with sprites and bodies


ambidex
 Share

Recommended Posts

I'm currently writing some horizontal scrolling game in which I have 3 layers (game.Container) and one of them contains the player and all the other objects which it encounters. I'm using game.Camera to follow the player around. This works wonderfully, though, the bodies of the player and collidable objects stay in place, since they aren't added to the container which the camera is moving. I can't seem to find out of I can add the bodies to the container so they get moved with the rest... Any suggestions?

 

Thanks again all. 

Link to comment
Share on other sites

Oh so the bodies are actually being moved? I'd have to test this, I think I'm not making any kind of connection between the body and the actual sprite. I can't really be sure at this point since I'm not able to view my code at this time. Can I ask you how this relation would normally be made between a Sprite and a Body which represents it's collidable body? Thanks again!

Link to comment
Share on other sites

Enpu, I've been testing this, the bodies do not seem to be moved when their sprites are being moved and therefor debugdraw shows the right position of the bodies. This is somewhat the result that I expected, since there is no way (as far as I know) to connect the two together? I would expect some functionality like: 

this.sprite.addTo(this.container);this.body.addTo(this.container); 

Or:

this.sprite.addto(this.container);this.sprite.addChild(this.body);

Though both do not work. Any idea how to keep the body's position up-to-date with that of the sprite?

Link to comment
Share on other sites

You're addressing the following?

this.sprite.position.x = this.body.position.x;this.sprite.position.y = this.body.position.y;

But consider the following scenario.

  Screen                                                                       +-----------------+-----------------------------------------------------------+| Sprite A body   |                                                           || X: 0  Y:0       |                                                           || Parent: World   |                                                           |+-----------------+                                                           ||                                                                             ||                                                                             ||                                                                             ||                                                                             ||                                                                             ||                                                                             ||                                                                             ||                                                                             ||                                                                             ||                                             Container A                     ||                                             X: 600 Y: 400                   ||                                            +------------------+-------------+|                                            | Sprite A         |             ||                                            | X: 0 Y: 0        |             ||                                            | Parent: Cont. A  |             ||                                            +------------------+             ||                                            |                                ||                                            |                                ||                                            |                                ||                                            |                                ||                                            |                                ||                                            |                                ||                                            |                                ||                                            |                                ||                                            |                                |+--------------------------------------------+--------------------------------+

Updating the body with the sprite's coords will result in a offset body (visa-versa will result the reversed result of course). I think a nice feature might be that you can also add the body to the container, which would update the body's coords too instead of creating a additional update function to update the bodies when moving the container around. What's your perspective on this? 

Link to comment
Share on other sites

  • 9 months later...

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