CellView.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import {ANITIME, CELL_HEIGHT, CELL_STATUS, CELL_WIDTH} from "../Model/ConstValue";
  2. cc.Class({
  3. extends: cc.Component,
  4. properties: {
  5. defaultFrame:{
  6. default:null,
  7. type:cc.SpriteFrame
  8. }
  9. },
  10. // LIFE-CYCLE CALLBACKS:
  11. onLoad () {
  12. this.isSelect = false;
  13. },
  14. initWithModel(model){
  15. this.model = model;
  16. let x = model.startX;
  17. let y = model.startY;
  18. this.node.x = CELL_WIDTH * (x - 0.5);
  19. this.node.y = CELL_HEIGHT * (y - 0.5);
  20. // console.log("this.node.x:",this.node.x)
  21. // console.log("this.node.y:",this.node.y)
  22. let animation = this.node.getComponent(cc.Animation);
  23. if (model.status == CELL_STATUS.COMMON){
  24. animation.stop();
  25. }else {
  26. animation.play(model.status);
  27. }
  28. },
  29. //执行移动动作
  30. updateView(){
  31. let cmd = this.model.cmd;
  32. if (cmd.length <= 0){
  33. return;
  34. }
  35. let actionArray = [];
  36. let curTime = 0;
  37. for (let i in cmd){
  38. if (cmd[i].playTime > curTime){
  39. let delay = cc.delayTime(cmd[i].playTime - curTime);
  40. actionArray.push(delay);
  41. }
  42. if (cmd[i].action == "moveTo"){
  43. let x = (cmd[i].pos.x - 0.5) * CELL_WIDTH;
  44. let y = (cmd[i].pos.y - 0.5) * CELL_HEIGHT;
  45. let move = cc.moveTo(ANITIME.TOUCH_MOVE, cc.v2(x, y));
  46. actionArray.push(move);
  47. }else if (cmd[i].action == "toDie"){
  48. if (this.status == CELL_STATUS.BIRD){
  49. let animation = this.node.getComponent(cc.Animation);
  50. animation.play("effect");
  51. actionArray.push(cc.delayTime(ANITIME.BOMB_BIRD_DELAY));
  52. }
  53. let callFunc = cc.callFunc(function (){
  54. this.node.destroy();
  55. },this);
  56. actionArray.push(callFunc);
  57. }else if (cmd[i].action == "setVisible"){
  58. let isVisible = cmd[i].isVisible;
  59. actionArray.push(cc.callFunc(function (){
  60. if (isVisible){
  61. this.node.opacity = 255;
  62. }else {
  63. this.node.opacity = 0;
  64. }
  65. },this));
  66. }else if (cmd[i].action == "toShake"){
  67. let rotateRight = cc.rotateBy(0.06, 30);
  68. let rotateLeft = cc.rotateBy(0.12, -60);
  69. actionArray.push(cc.repeat(cc.sequence(rotateRight, rotateLeft, rotateRight),2));
  70. }
  71. curTime = cmd[i].playTime + cmd[i].keepTime;
  72. }
  73. if (actionArray.length == 1){
  74. this.node.runAction(actionArray[0]);
  75. }else {
  76. this.node.runAction(cc.sequence(...actionArray));
  77. }
  78. },
  79. setSelect(flag){
  80. let animation = this.node.getComponent(cc.Animation);
  81. let bg = this.node.getChildByName("select");
  82. if (flag == false && this.isSelect && this.model.status == CELL_STATUS.COMMON){
  83. animation.stop();
  84. this.node.getComponent(cc.Sprite).spriteFrame = this.defaultFrame;
  85. }else if (flag && this.model.status == CELL_STATUS.COMMON){
  86. animation.play(CELL_STATUS.CLICK);
  87. }else if (flag && this.model.status == CELL_STATUS.BIRD){
  88. animation.play(CELL_STATUS.CLICK);
  89. }
  90. bg.active = flag;
  91. this.isSelect = flag;
  92. },
  93. // start () { },
  94. // update (dt) {},
  95. });