Ali Malik Posted October 3, 2014 Share Posted October 3, 2014 Hello everyone, it's me again.. i think my last post wasn't very clear .. i need help see i'm using this code (PLAYER)player = this.game.add.sprite(32, game.world.height - 150, 'dude'); game.physics.arcade.enable(player); player.body.bounce.y = 0.2; player.body.gravity.y = 1200; player.body.collideWorldBounds = true; player.animations.add('left', [0, 1, 2, 3], 10, true); player.animations.add('right', [5, 6, 7, 8], 10, true); Now i want to attach a gun with him, infront of him so i just used this codegun = game.add.sprite(player.x + 45, player.y + 55, 'DE'); somehow, player.addChild(gun); is not working... for that i did thatgun = game.add.sprite(45, 55, 'DE'); player.addChild(gun); (If you think your code is working with .addChild() function please let me know) it's still not working, Please have a look at JsFiddle http://jsfiddle.net/AliMalik/snctgm5q/5/(I DIDN'T ADD, ADDCHILD FUNCTION IN THERE, PLEASE JUST TAKE A LOOK, GIVE ME YOUR OPINIONS ABOUT HOW TO MOVE WITH PLAYER) NOTE : i also used "group" function to move gun, it worked fine. The only problem was in that function was whenever my player launches vertical velocity (player.body.velocity.y = -xx;) the gun in the group never stops, it gets out of bound in Y direction.. thank you Link to comment Share on other sites More sharing options...
valueerror Posted October 3, 2014 Share Posted October 3, 2014 http://test.xapient.net/phaser/help-addchild.zip // download the zip file with a working example and all used assets and here is the testcase live:http://test.xapient.net/phaser/help-addchild/ Link to comment Share on other sites More sharing options...
valueerror Posted October 3, 2014 Share Posted October 3, 2014 the important lines are these:gun = game.add.sprite(0,0, 'gun'); // we add the gun at 0,0 because this will be the relative position to the player once we added the gun as childplayer.addChild(gun); // here we use a pixi method to add a child to a sprite.. both sprites behave as one sprite after thiseverything else should of course work before you try to add the gun.. Link to comment Share on other sites More sharing options...
Ali Malik Posted October 3, 2014 Author Share Posted October 3, 2014 That Helped Thank you now i want to know what if i fix the position (XY) of gun.. will it work? Link to comment Share on other sites More sharing options...
Ali Malik Posted October 3, 2014 Author Share Posted October 3, 2014 Hey i just set player.anchor.setTo(1,0.5); everything is working fine on it's place!! thank you THAT HELPED VERY WELL! Link to comment Share on other sites More sharing options...
valueerror Posted October 3, 2014 Share Posted October 3, 2014 if you set the anchor to 1 and 0.5 it will not rotate anymore around the center.. is that really what you want? if you just want to place the gun on a different position of the player (relative to the player) just change it's x and y coordinates on creation time.. instead of 0,0 10, 0 will move it 10 px to the right relative to the player.. SuperMarco 1 Link to comment Share on other sites More sharing options...
Recommended Posts