CellView.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import {CELL_STATUS, CELL_WIDTH, CELL_HEIGHT, ANITIME} from '../Model/ConstValue';
  2. cc.Class({
  3. extends: cc.Component,
  4. properties: {
  5. // foo: {
  6. // default: null, // The default value will be used only when the component attaching
  7. // to a node for the first time
  8. // url: cc.Texture2D, // optional, default is typeof default
  9. // serializable: true, // optional, default is true
  10. // visible: true, // optional, default is true
  11. // displayName: 'Foo', // optional
  12. // readonly: false, // optional, default is false
  13. // },
  14. // ...
  15. defaultFrame:{
  16. default: null,
  17. type: cc.SpriteFrame
  18. }
  19. },
  20. // use this for initialization
  21. onLoad: function () {
  22. //this.model = null;
  23. this.isSelect = false;
  24. },
  25. initWithModel: function(model){
  26. this.model = model;
  27. var x = model.startX;
  28. var y = model.startY;
  29. this.node.x = CELL_WIDTH * (x - 0.5);
  30. this.node.y = CELL_HEIGHT * (y - 0.5);
  31. var animation = this.node.getComponent(cc.Animation);
  32. if (model.status == CELL_STATUS.COMMON){
  33. animation.stop();
  34. }
  35. else{
  36. animation.play(model.status);
  37. }
  38. },
  39. // 执行移动动作
  40. updateView: function(){
  41. var cmd = this.model.cmd;
  42. if(cmd.length <= 0){
  43. return ;
  44. }
  45. var actionArray = [];
  46. var curTime = 0;
  47. for(var i in cmd){
  48. if( cmd[i].playTime > curTime){
  49. var delay = cc.delayTime(cmd[i].playTime - curTime);
  50. actionArray.push(delay);
  51. }
  52. if(cmd[i].action == "moveTo"){
  53. var x = (cmd[i].pos.x - 0.5) * CELL_WIDTH;
  54. var y = (cmd[i].pos.y - 0.5) * CELL_HEIGHT;
  55. var move = cc.moveTo(ANITIME.TOUCH_MOVE, cc.v2(x,y));
  56. actionArray.push(move);
  57. }
  58. else if(cmd[i].action == "toDie"){
  59. if(this.status == CELL_STATUS.BIRD){
  60. let animation = this.node.getComponent(cc.Animation);
  61. animation.play("effect");
  62. actionArray.push(cc.delayTime(ANITIME.BOMB_BIRD_DELAY));
  63. }
  64. var callFunc = cc.callFunc(function(){
  65. this.node.destroy();
  66. },this);
  67. actionArray.push(callFunc);
  68. }
  69. else if(cmd[i].action == "setVisible"){
  70. let isVisible = cmd[i].isVisible;
  71. actionArray.push(cc.callFunc(function(){
  72. if(isVisible){
  73. this.node.opacity = 255;
  74. }
  75. else{
  76. this.node.opacity = 0;
  77. }
  78. },this));
  79. }
  80. else if(cmd[i].action == "toShake"){
  81. let rotateRight = cc.rotateBy(0.06,30);
  82. let rotateLeft = cc.rotateBy(0.12, -60);
  83. actionArray.push(cc.repeat(cc.sequence(rotateRight, rotateLeft, rotateRight), 2));
  84. }
  85. curTime = cmd[i].playTime + cmd[i].keepTime;
  86. }
  87. /**
  88. * 智障的引擎设计,一群SB
  89. */
  90. if(actionArray.length == 1){
  91. this.node.runAction(actionArray[0]);
  92. }
  93. else{
  94. this.node.runAction(cc.sequence(...actionArray));
  95. }
  96. },
  97. // called every frame, uncomment this function to activate update callback
  98. // update: function (dt) {
  99. // },
  100. setSelect: function(flag){
  101. var animation = this.node.getComponent(cc.Animation);
  102. var bg = this.node.getChildByName("select");
  103. if(flag == false && this.isSelect && this.model.status == CELL_STATUS.COMMON){
  104. animation.stop();
  105. this.node.getComponent(cc.Sprite).spriteFrame = this.defaultFrame;
  106. }
  107. else if(flag && this.model.status == CELL_STATUS.COMMON){
  108. animation.play(CELL_STATUS.CLICK);
  109. }
  110. else if(flag && this.model.status == CELL_STATUS.BIRD){
  111. animation.play(CELL_STATUS.CLICK);
  112. }
  113. bg.active = flag;
  114. this.isSelect = flag;
  115. }
  116. });