Jump to content

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


Ali Malik
 Share

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

Link to comment
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..

Link to comment
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..

Link to comment
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...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...