Fish.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. function Fish(options, fishData) {
  2. base(this,LSprite,[]);
  3. this.init(options, fishData);
  4. }
  5. Fish.SWIM = "swim";
  6. Fish.prototype.init = function(options, fishData) {
  7. var self = this;
  8. self.stype = options.stype;
  9. var data = fishData[options.image.name];
  10. var list = LGlobal.divideCoordinate(options.image.width, options.image.height, 1, options.image.frameNumber);
  11. self.sprite = new LAnimationTimeline(data,list);
  12. self.sprite.setLabel(Fish.SWIM,0,0);
  13. self.sprite.scaleX = (options.motion.direction==0?1:-1);
  14. self.sprite.speed = Math.floor(GAME_FPS/options.motion.speed);
  15. // self.sprite.speed = 10;
  16. if(options.motion.direction==0) {
  17. self.sprite.x = -data.width/2;
  18. self.sprite.y = -data.height/2;
  19. // self.graphics.drawVertices(0,"#880088", [[-data.width/2,-data.height/2],[data.width/2,-data.height/2],[data.width/2,data.height/2],[-data.width/2,data.height/2]], true, "#880088");
  20. } else {
  21. self.sprite.x = data.width/2;
  22. self.sprite.y = -data.height/2;
  23. // self.graphics.drawVertices(0,"#880088", [[-data.width/2,-data.height/2],[data.width/2,-data.height/2],[data.width/2,data.height/2],[-data.width/2,data.height/2]], true, "#880088");
  24. }
  25. self.addShape(LShape.RECT, [-data.width/2,-data.height/2,data.width,data.height]);
  26. self.motion = options.motion;
  27. self.addChild(self.sprite);
  28. self.bodyWidth = data.width;
  29. self.hasBeenHooked = false;
  30. self.addEventListener(LEvent.ENTER_FRAME, self.onframe);
  31. self.step = 0;
  32. };
  33. Fish.prototype.onframe = function(event) {
  34. var self = event.target;
  35. self.sprite.onframe();
  36. if(!self.hasBeenHooked) {
  37. self.step++;
  38. if (self.motion.direction == 0) {
  39. self.x = LGlobal.width+self.bodyWidth/2 - self.motion.speed*self.step;
  40. } else {
  41. self.x = self.motion.speed*self.step - self.bodyWidth/2;
  42. }
  43. self.y = self.motion.amplitude * Math.sin(self.x*self.motion.frequency) + self.motion.phase;
  44. if (self.x > LGlobal.width+Math.abs(self.bodyWidth) || self.x < -Math.abs(self.bodyWidth)) {
  45. setTimeout(function() {
  46. self.remove();
  47. self.die();
  48. }, 0);
  49. }
  50. }
  51. }
  52. Fish.prototype.beHooked = function() {
  53. var self = this;
  54. self.hasBeenHooked = true;
  55. }