Ryan Posted February 2, 2014 Share Posted February 2, 2014 EDIT: I have fixed this by making all of the parts the same size and setting the anchor to (0.5, 0). It's not very elegant, but it seems to be the only way to get sprites of different sizes to stay in position when you flip them./** * ADD THE PLAYER TO THE GAME */player_group = this.game.add.group();player_group.x = 200;player_group.y = this.game.world.height - 140;player_foot_back = player_group.create(0, 0, 'player_foot_back');player_foot_back.anchor.setTo(0.5, 0);player_body = player_group.create(0, 0, 'player_body');player_body.anchor.setTo(0.5, 0);player_head = player_group.create(0, 0, 'player_head');player_head.anchor.setTo(0.5, 0);player_foot_front = player_group.create(0, 0, 'player_foot_front');player_foot_front.anchor.setTo(0.5, 0);------------------------------------------------- Hey guys, I've been playing around with this for a few hours now and I can't seem to get anywhere. Basically, it's a platformer game with a player made up of 4 parts (head, body, feet, hands). There a things that I need the whole group to do, like gravity. There are also things I need just parts to do, like the hands will rotate./** * ADD THE PLAYER TO THE GAME */ player_group = this.game.add.group(); player_group.x = 20; player_group.y = this.game.world.height - 150; player_body = player_group.create(18, 35, 'player_body'); player_body.anchor.setTo(0, 0); player_head = player_group.create(0, 0, 'player_head'); player_head.anchor.setTo(0, 0); player_feet = player_group.create(25, 122, 'player_foot'); player_feet.anchor.setTo(0, 0);This positions the parts on the stage just fine.if (leftKey.isDown) { player_group.setAll("body.velocity.x", -450); player_group.setAll("scale.x", -1); } else if (rightKey.isDown) { player_group.setAll("body.velocity.x", 450); player_group.setAll("scale.x", 1); }This moves the player fine until it changes direction, then all of the parts are not positioned correctly. I'm assuming this has something to do with the anchor. Does anyone have any suggestions on the best way to go about controlling a sprite made up of multiple parts? Thanks! FyreTale. jpdev 1 Link to comment Share on other sites More sharing options...
jpdev Posted February 2, 2014 Share Posted February 2, 2014 I am wondering about this too. (So I am adding my question here, I think it boils down to the same one..) If for example I want to give a accessory to my player sprite, that should follow the sprite around (without it's own physics) I thought I could use a group for that.But since a group does not have a body I can't just have the player be a group.. Of course I could set the position of the accessory sprites in every update() function to specific x,y coordinates depending on the player sprite position, but I don't think that that's the correct way. Has anyone any insides into how to move sprites as a unit with the physics engine (ie. velocity)? Link to comment Share on other sites More sharing options...
Ryan Posted February 2, 2014 Author Share Posted February 2, 2014 I have done just that. I have four body parts + the hand/gun. I've made all the body parts the same width/height and they are part of a group. I then used group.setAll() to set the gravity etc. Since the arm/gun will rotate, I couldn't have it be the same size as the other parts, so I had to position it in the update() function. Seems to be the only way around it. Link to comment Share on other sites More sharing options...
XekeDeath Posted February 2, 2014 Share Posted February 2, 2014 Sprite.addChild()Child sprites are positioned relative to the parent, will move with the parent, and can be moved/rotated separately to the parent. jpdev, Titus and yougotashovel 3 Link to comment Share on other sites More sharing options...
Ryan Posted February 3, 2014 Author Share Posted February 3, 2014 Sprite.addChild()Child sprites are positioned relative to the parent, will move with the parent, and can be moved/rotated separately to the parent. That sounds like it will do it! +1 vote on adding this to the docs. I know Rich is a busy man, maybe we could make the docs more like a wiki so the community could build it? Link to comment Share on other sites More sharing options...
XekeDeath Posted February 3, 2014 Share Posted February 3, 2014 It is in *some* docs, but not the Phaser.Sprite ones.You can find the addChild function in the PIXI docs, because it is inherited all the way up from the PIXI DisplayObjectContainer object. Link to comment Share on other sites More sharing options...
Ryan Posted February 3, 2014 Author Share Posted February 3, 2014 Ahh, awesome. I guess I'll look through both of the docs now before posting. Thanks again! Link to comment Share on other sites More sharing options...
jpdev Posted February 3, 2014 Share Posted February 3, 2014 Just to confirm, it works perfectly. Thanks Link to comment Share on other sites More sharing options...
eugenioclrc Posted April 11, 2014 Share Posted April 11, 2014 I have perfectly use de addChild, but i am having some problems animating them. I have create a body, then i add some armor and weapons, then i trigger the animation, but it doesnt trigger con the sprite childs. Is there any method available in Phaser.Sprite? or i just have to write my own? Link to comment Share on other sites More sharing options...
vann Posted April 30, 2014 Share Posted April 30, 2014 This sounds almost exactly like what i'm trying to do. Can someone point me to a working example? (in P2) Link to comment Share on other sites More sharing options...
Recommended Posts