Sign in to follow this  
Ali Malik

Sprite attach with sprite (and move with parent sprite) HELP!

Recommended Posts

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 code

gun = game.add.sprite(player.x + 45, player.y + 55, 'DE'); 

somehow, player.addChild(gun); is not working... for that i did that

gun = 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

Share this post


Link to post
Share on other sites

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 this

everything else should of course work before you try to add the gun..

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.