Fruit.ts 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. const {ccclass, property} = cc._decorator;
  2. @ccclass
  3. export default class Fruit extends cc.Component {
  4. @property({type:cc.Prefab})
  5. bubblePrefab:cc.Prefab = null;
  6. armatureDisplay:dragonBones.ArmatureDisplay;
  7. index:number;
  8. type:number;
  9. willCry:boolean;
  10. init(index:number,type:number){
  11. this.index = index;
  12. this.type = type;
  13. return this;
  14. }
  15. toCry(){
  16. this.willCry = true;
  17. }
  18. onLoad(){
  19. this.armatureDisplay = this.node.getComponent(dragonBones.ArmatureDisplay);
  20. if(this.willCry){
  21. this.cry();
  22. }else{
  23. this.node.runAction(cc.repeatForever(cc.sequence(
  24. cc.moveBy(0.6,cc.v2(0,36)),
  25. cc.moveBy(0.6,cc.v2(0,-36))
  26. )));
  27. this.idle();
  28. }
  29. }
  30. idle(){
  31. if(this.armatureDisplay.animationName!='idle'+this.type){
  32. this.armatureDisplay.playAnimation('idle'+this.type,0);
  33. }
  34. }
  35. cry(){
  36. if(this.armatureDisplay.animationName!='cry'+this.type){
  37. this.armatureDisplay.playAnimation('cry'+this.type,0);
  38. }
  39. }
  40. bubble(){
  41. let bubble = cc.instantiate(this.bubblePrefab);
  42. bubble.setPosition(this.node.position);
  43. if(this.node.parent){
  44. this.node.parent.addChild(bubble);
  45. }
  46. }
  47. eat():number[]{
  48. window.gc.fruitEaten[this.index] = true;
  49. this.bubble();
  50. this.node.destroy();
  51. return [this.type,this.node.x,this.node.y];
  52. }
  53. }