Bird.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. Bird = function (game) {
  2. Phaser.Sprite.call(this, game, 0, 0, 'sprites', 'Bird' );
  3. game.add.existing(this);
  4. this.anchor.set( 0.5, 1 );
  5. this.inFly = false;
  6. this.speed = 3;
  7. this.tween = this.game.add.tween( this );
  8. this.tween.onComplete.add( this.flyEnd, this );
  9. this.path = [];
  10. };
  11. Bird.prototype = Object.create(Phaser.Sprite.prototype);
  12. Bird.prototype.constructor = Bird;
  13. var p = Bird.prototype;
  14. p.flyTo = function( x, y ){
  15. this.path.push( {x:x, y:y} );
  16. if( this.inFly )
  17. return;
  18. this.inFly = true;
  19. if( x > this.x )
  20. this.scale.set( 0.7, 0.7 );
  21. else
  22. this.scale.set( -0.7, 0.7 );
  23. this.tween._parent = null;
  24. this.tween._lastChild = null;
  25. this.tween.to( {x: x, y:y}, this.getTime( x, y ), Phaser.Easing.Sinusoidal.Out, true, 500 );
  26. };
  27. p.getLastPointY = function() {
  28. return this.path[this.path.length-1].y;
  29. };
  30. p.flyBack = function() {
  31. this.path.pop();
  32. var point = this.path.pop();
  33. var x = point.x;
  34. var y = point.y;
  35. this.path.push( {x:x, y:y} );
  36. this.inFly = true;
  37. if( x > this.x )
  38. this.scale.set( 0.3, 0.3 );
  39. else
  40. this.scale.set( -0.3, 0.3 );
  41. this.tween._parent = null;
  42. this.tween._lastChild = null;
  43. this.tween.to( {x: x, y:y}, this.getTime( x, y ), Phaser.Easing.Sinusoidal.Out, true, 500 );
  44. };
  45. p.flyToPontUnder = function( y ){
  46. for( var i = this.path.length-1; i >=0; i-- ){
  47. if( this.path[i].y < y ){
  48. this.path.pop()
  49. }else{
  50. var x = this.path[i].x;
  51. var y = this.path[i].y;
  52. this.inFly = true;
  53. if( x > this.x )
  54. this.scale.set( 0.3, 0.3 );
  55. else
  56. this.scale.set( -0.3, 0.3 );
  57. this.tween._parent = null;
  58. this.tween._lastChild = null;
  59. this.tween.to( {x: x, y:y}, this.getTime( x, y ), Phaser.Easing.Sinusoidal.Out, true, 500 );
  60. break;
  61. }
  62. }
  63. };
  64. p.flyEnd = function() {
  65. this.inFly = false;
  66. };
  67. p.getTime = function( x, y ){
  68. return Phaser.Point.distance( new Phaser.Point(this.x, this.y), new Phaser.Point(x, y) ) * this.speed;
  69. };