// Learn cc.Class: // - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html // - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html // Learn Attribute: // - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html // - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html // Learn life-cycle callbacks: // - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html // - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html var GameStates = require('GameStates'); cc.Class({ extends: cc.Component, properties: { // foo: { // // ATTRIBUTES: // default: null, // The default value will be used only when the component attaching // // to a node for the first time // type: cc.SpriteFrame, // optional, default is typeof default // serializable: true, // optional, default is true // }, // bar: { // get () { // return this._bar; // }, // set (value) { // this._bar = value; // } // }, speed: 0, Array: [], GameStates: null, isStop: false, isDestory: false, TestSateEnum: { default: GameStates.SpawnCollisionState.Normal, type: cc.Enum(GameStates.SpawnCollisionState) } }, // LIFE-CYCLE CALLBACKS: // onLoad () {}, start() { this.GameStates = cc.find('Game').getComponent('GameStates'); console.log("TestCollision"); // cc.eventManager.addListener({ // event: cc.EventListener.TOUCH_ONE_BY_ONE, // onTouchBegan: (touch, event) => { // var touchLoc = touch.getLocation(); // // 获取多边形碰撞组件的世界坐标系下的点来进行点击测试 // // 如果是其他类型的碰撞组件,也可以在 cc.Intersection 中找到相应的测试函数 // if (cc.Intersection.pointInPolygon(touchLoc, this.polygonCollider.world.points)) { // this.title.string = 'Hit'; // } // else { // this.title.string = 'Not hit'; // } // return true; // }, // }, this.node); }, update(dt) { if (this.isStop) return; var x = this.node.x; x += this.speed * dt; if (x > 500) { x = 169; } this.node.x = x; }, onCollisionEnter: function (other) { if (this.TestSateEnum == GameStates.SpawnCollisionState.Normal) { // return; // console.log("onCollisionEnter:", other.node.name); this.GameStates.index = 10; // console.log("GameStates.index:", this.GameStates.index); //向数组的末尾添加一个或更多元素,并返回新的长度。 // this.GameStates.pushPlayerCollisions(other.node); this.GameStates.PlayerCollisionName.push(other.node.name); // console.log("this.GameStates.PlayerCollisionName:", this.GameStates.PlayerCollisionName); } else { this.isStop = true; setTimeout(function () { this.node.destroy(); }.bind(this), 800); } }, onCollisionStay: function (other, self) { // console.log('on collision stay',other.node.name); }, onCollisionExit: function (other, self) { if (this.TestSateEnum == GameStates.SpawnCollisionState.Normal) { // console.log('on collision exit', other.node.name); // 删除并返回数组的最后一个元素 this.GameStates.PlayerCollisionName.pop(other.node.name); // console.log("this.GameStates.PlayerCollisionName:", this.GameStates.PlayerCollisionName); } } });