// Learn TypeScript: // - https://docs.cocos.com/creator/manual/en/scripting/typescript.html // Learn Attribute: // - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html // Learn life-cycle callbacks: // - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html const {ccclass, property} = cc._decorator; @ccclass export default class NewClass extends cc.Component { //設定爆炸spriteFrame(注意,動態腳本的圖片無法讀入檔案,所以必須由上一層父節點的ts01先讀入,再傳入) @property(cc.SpriteFrame) sprite_explode:cc.SpriteFrame = null; onLoad () { //啟動計時器 this.schedule(this.timer, 0.01); } timer () { this.node.y += 10; if(this.node.y >= 1900) { //關閉計時器 this.unschedule(this.timer); //顯示爆炸動畫(緩動:由縮小到放大,透明度漸漸透明) this.AniExplode(); //銷毀子彈節點 //this.node.destroy(); //結束 return; } } AniExplode() { //建立動態爆炸sprite組件(爆炸) var comp_explode = this.node.addComponent(cc.Sprite); comp_explode.spriteFrame = this.sprite_explode; //縮小圖片size this.node.scale = 0.1; //下面,即將使用閉包用法 let self = this; //慢慢放大成爆炸動畫(由縮小到放大,透明度漸漸透明) cc.tween(this.node) //1秒,放大2倍,透明度=>0 .to(1,{scale:2, opacity:0}) //呼叫函數,銷毀子彈節點 //(1)錯誤寫法:會把全部的子彈節點銷毀,就無法新增第二顆子彈 //.call(function(){this.node.destroy;}) //(2)正確寫法:使用閉包語法(函數內的this,用self取代) .call(function(){self.myDestory();}) //開始啟動 .start(); } myDestory() { this.node.destroy(); } // update (dt) {} }