Advertisement
Carcigenicate

setSpriteDirection

Apr 14th, 2016
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.   * Sets the direction the sprite faces based on it's velocity.
  3.   * Faces the sprite in it's opposite direction when velocity is < 0
  4.   * Assumes the sprite is facing right when its horizontal scale is < 0
  5.   *
  6.   * @param sprite The sprite to set the direction of. Must have arcade physics enabled on it.
  7.   */
  8. function setSpriteDirection(sprite: Phaser.Sprite) {
  9.     let horVelocity = sprite.body.velocity.x;
  10.     let horScale = sprite.scale.x;
  11.  
  12.     let velSign = GeneralHelpers.signum(horVelocity);
  13.     let scaleSign = GeneralHelpers.signum(horScale);
  14.  
  15.     if (horVelocity !== 0 && scaleSign !== velSign) {
  16.         let absScale = Math.abs(horScale);
  17.  
  18.         sprite.scale.x = velSign === 1 ? absScale : absScale * -1;
  19.     }
  20.  
  21. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement