TestCollision.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // Learn cc.Class:
  2. // - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
  3. // - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
  4. // Learn Attribute:
  5. // - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
  6. // - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
  7. // Learn life-cycle callbacks:
  8. // - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
  9. // - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html
  10. var GameStates = require('GameStates');
  11. cc.Class({
  12. extends: cc.Component,
  13. properties: {
  14. // foo: {
  15. // // ATTRIBUTES:
  16. // default: null, // The default value will be used only when the component attaching
  17. // // to a node for the first time
  18. // type: cc.SpriteFrame, // optional, default is typeof default
  19. // serializable: true, // optional, default is true
  20. // },
  21. // bar: {
  22. // get () {
  23. // return this._bar;
  24. // },
  25. // set (value) {
  26. // this._bar = value;
  27. // }
  28. // },
  29. speed: 0,
  30. Array: [],
  31. GameStates: null,
  32. isStop: false,
  33. isDestory: false,
  34. TestSateEnum: {
  35. default: GameStates.SpawnCollisionState.Normal,
  36. type: cc.Enum(GameStates.SpawnCollisionState)
  37. }
  38. },
  39. // LIFE-CYCLE CALLBACKS:
  40. // onLoad () {},
  41. start() {
  42. this.GameStates = cc.find('Game').getComponent('GameStates');
  43. console.log("TestCollision");
  44. // cc.eventManager.addListener({
  45. // event: cc.EventListener.TOUCH_ONE_BY_ONE,
  46. // onTouchBegan: (touch, event) => {
  47. // var touchLoc = touch.getLocation();
  48. // // 获取多边形碰撞组件的世界坐标系下的点来进行点击测试
  49. // // 如果是其他类型的碰撞组件,也可以在 cc.Intersection 中找到相应的测试函数
  50. // if (cc.Intersection.pointInPolygon(touchLoc, this.polygonCollider.world.points)) {
  51. // this.title.string = 'Hit';
  52. // }
  53. // else {
  54. // this.title.string = 'Not hit';
  55. // }
  56. // return true;
  57. // },
  58. // }, this.node);
  59. },
  60. update(dt) {
  61. if (this.isStop) return;
  62. var x = this.node.x;
  63. x += this.speed * dt;
  64. if (x > 500) {
  65. x = 169;
  66. }
  67. this.node.x = x;
  68. },
  69. onCollisionEnter: function (other) {
  70. if (this.TestSateEnum == GameStates.SpawnCollisionState.Normal) {
  71. // return;
  72. // console.log("onCollisionEnter:", other.node.name);
  73. this.GameStates.index = 10;
  74. // console.log("GameStates.index:", this.GameStates.index);
  75. //向数组的末尾添加一个或更多元素,并返回新的长度。
  76. // this.GameStates.pushPlayerCollisions(other.node);
  77. this.GameStates.PlayerCollisionName.push(other.node.name);
  78. // console.log("this.GameStates.PlayerCollisionName:", this.GameStates.PlayerCollisionName);
  79. } else {
  80. this.isStop = true;
  81. setTimeout(function () {
  82. this.node.destroy();
  83. }.bind(this), 800);
  84. }
  85. },
  86. onCollisionStay: function (other, self) {
  87. // console.log('on collision stay',other.node.name);
  88. },
  89. onCollisionExit: function (other, self) {
  90. if (this.TestSateEnum == GameStates.SpawnCollisionState.Normal) {
  91. // console.log('on collision exit', other.node.name);
  92. // 删除并返回数组的最后一个元素
  93. this.GameStates.PlayerCollisionName.pop(other.node.name);
  94. // console.log("this.GameStates.PlayerCollisionName:", this.GameStates.PlayerCollisionName);
  95. }
  96. }
  97. });