77 lines
2.4 KiB
TypeScript
77 lines
2.4 KiB
TypeScript
// 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 {
|
|
|
|
duratrion : number = 15;
|
|
moveAmountY : number = 25;
|
|
|
|
moveEnemy : cc.ActionInterval;
|
|
|
|
@property(cc.Prefab)
|
|
yellowBullet: cc.Prefab = null;
|
|
|
|
enemyLife: number = 1
|
|
|
|
playAnimation : Boolean = true;
|
|
|
|
setMovements() {
|
|
var moveDown = cc.moveBy(this.duratrion, cc.v2(0, -this.node.parent.getContentSize().height * 0.95));
|
|
this.node.runAction(cc.repeatForever(moveDown));
|
|
}
|
|
|
|
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.scheduleOnce(this.setMovements, 0.1);
|
|
|
|
this.schedule(this.shootBullets, Math.random() * 1 + 1, cc.macro.REPEAT_FOREVER, 0);
|
|
}
|
|
onCollisionEnter(otherCollider, selfCollider) {
|
|
if(otherCollider.name === 'greenbullet<PolygonCollider>') {
|
|
this.enemyLife--;
|
|
if(this.enemyLife <= 0 && this.playAnimation) {
|
|
this.node.getComponent(cc.Collider).enabled = false;
|
|
this.node.stopAllActions();
|
|
this.playAnimation = false;
|
|
this.node.getComponent(cc.Animation).play();
|
|
}
|
|
}
|
|
if(otherCollider.name === 'player<PolygonCollider>') {
|
|
this.node.destroy();
|
|
cc.director.loadScene('Menu');
|
|
}
|
|
}
|
|
|
|
removeExplosion() {
|
|
this.node.destroy();
|
|
}
|
|
|
|
start () {
|
|
|
|
}
|
|
|
|
update (dt) {
|
|
if(this.node.position.y <= -(this.node.parent.getContentSize().height)) {
|
|
this.node.destroy();
|
|
cc.director.loadScene('Menu');
|
|
}
|
|
}
|
|
}
|