// Learn TypeScript: // - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/typescript.html // - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/typescript.html // Learn Attribute: // - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html // - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/reference/attributes.html // Learn life-cycle callbacks: // - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html // - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html const {ccclass, property} = cc._decorator; @ccclass export default class NewClass extends cc.Component { @property duratrion : number = 0.5; @property moveAmountX : number = 100; @property moveAmountY : number = 50; moveEnemy : cc.ActionInterval; @property(cc.Prefab) yellowBullet: cc.Prefab = null; @property enemyLife: number = 3 playAnimation : Boolean = true; setMovements() { var moveLeft = cc.moveBy(this.duratrion, cc.v2(-this.moveAmountX, -this.moveAmountY)).easing(cc.easeCircleActionInOut()); var moveRight = cc.moveBy(this.duratrion, cc.v2(this.moveAmountX, -this.moveAmountY)).easing(cc.easeCircleActionInOut()); return cc.repeatForever(cc.sequence(moveLeft, moveRight)); } shootBullets() { var bullet = cc.instantiate(this.yellowBullet); bullet.setPosition(this.node.position.x, this.node.position.y - 60); this.node.parent.addChild(bullet); } // LIFE-CYCLE CALLBACKS: onLoad () { this.moveEnemy = this.setMovements(); this.node.runAction(this.moveEnemy); this.schedule(this.shootBullets, Math.random() * 0.5 + 0.5, cc.macro.REPEAT_FOREVER, 0); } onCollisionEnter(otherCollider, selfCollider) { if(otherCollider.name === 'greenbullet') { this.enemyLife--; if(this.enemyLife <= 0 && this.playAnimation) { this.node.stopAllActions(); this.playAnimation = false; this.node.getComponent(cc.Animation).play(); } } if(otherCollider.name === 'player') { this.node.destroy(); cc.director.loadScene('Menu'); } } removeExplosion() { this.node.destroy(); this.node.parent.getComponent('Game').spawnShips(); } start () { } update (dt) { if(this.node.position.y <= -(this.node.parent.getContentSize().height)) { this.node.destroy(); cc.director.loadScene('Menu'); } } }