// 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 import tsBullet from "./tsBullet"; //載入『動態腳本tsBullet』 const {ccclass, property} = cc._decorator; @ccclass export default class NewClass extends cc.Component { @property(cc.SpriteFrame) sprit_bullet:cc.SpriteFrame = null; //設定爆炸spriteFrame(注意,動態腳本的圖片無法讀入檔案,所以必須由上一層父節點的ts01先讀入,再傳入) @property(cc.SpriteFrame) sprite_explode_input:cc.SpriteFrame = null; onLoad () { this.node.on("touchstart", this.shot, this); } shot () { //動態建立節點 var node_bullet = new cc.Node; //動態建立sprite組件 var comp_sprite = node_bullet.addComponent(cc.Sprite); //設定sprite的spriteFrame comp_sprite.spriteFrame = this.sprit_bullet; //縮小子彈 node_bullet.scale = 0.3; //將子彈節點,掛在坦克下 node_bullet.parent = this.node; //顯示子彈(相對位置) node_bullet.setPosition(cc.v2(0, 400)); //把動態子彈,加上『動態腳本tsBullet』,傳回mytsBullet腳本組件 var mytsBullet = node_bullet.addComponent(tsBullet); //設定mytsBullet腳本組件的爆炸spriteFrame(注意,動態腳本的圖片無法讀入檔案,所以必須由上一層父節點的ts01先讀入,再傳入) mytsBullet.sprite_explode = this.sprite_explode_input; } // update (dt) {} }