Jump to content

Make arm and gun point at mouse as children of torso


michaelcalkins
 Share

Recommended Posts

How do you update a child sprite's angle so it points at the current position of your mouse?

create function

// this.player created up here

this.rightArmSprite = this.game.add.sprite(0, 0, 'right-arm')
this.rightArmSprite.anchor.set(.24, .2)

this.ak47Sprite = this.game.add.sprite(0, 0, 'ak47')

this.rightArmSprite.addChild(this.ak47Sprite)
this.player.addChild(this.rightArmSprite)

update function

let rightArmRotation = this.game.physics.arcade.angleToPointer(this.rightArmSprite)

What do I do with the angle from my right arm sprite to my mouse?

Screen Shot 2016-04-03 at 1.52.35 PM.png

crouching1_png.png

Link to comment
Share on other sites

You should always anchor your sprite using sprite.anchor.setTo(0.5); then, when you add the child sprite, anchor it using anchor.setTo(0, 0); this means the sprite will be created at the centre of the man, but rotate around the top left shoulder part. Also, if you've not cropped the arm down to the pivot point, then this may also be the issue and you'll have to trial and error find the correct anchor point.. i.e: anchor.setTo(0.13, 0.22);

This should fix your issue :) 

Link to comment
Share on other sites

    var angleInDegrees = this.game.physics.arcade.angleToPointer(this.rightArmSprite) * 180 / Math.PI;
    this.rightArmSprite.angle = angleInDegrees + 90

to

    var angleInDegrees = this.game.physics.arcade.angleToPointer(this.player) * 180 / Math.PI;
    this.rightArmSprite.angle = angleInDegrees + 90

fixed the issue I was having.  Once the right arm is a child of the player I was calculating the angle from the mouse incorrectly to the right arm still.  It should have been calculating from the parent. 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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