// 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 { @property(cc.SpriteFrame) pic_bullet:cc.SpriteFrame = null; onLoad () { this.node.on("touchstart", this.shot, this); } shot() { //☎步驟1:動態產生子彈節點: var node1 = new cc.Node; //☎步驟2:動態產生子彈節點裡面的組件(sprite) var comp1 = node1.addComponent(cc.Sprite); //☎步驟3:設定子彈節點裡面的sprite組件的屬性(sprite的spriteFrame) comp1.spriteFrame = this.pic_bullet; //☎步驟4:顯示子彈節點,兩個方法都可以 //(1)方法1:子彈node1節點,掛在坦克裡面 node1.parent = this.node; //(2)方法2:子彈node1節點,掛在Canvas裡面 //node1.parent = cc.find("Canvas"); //☎步驟5:設定子彈圖片的大小(縮小) //注意:cc.v2(),除了可以設定坐標,也可以設定縮放大小 //cc.v2(), cc.v3()都可以 node1.setScale(cc.v2(0.3, 0.3)); //☎步驟6:設定子彈節點的位置 //注意:若是子彈掛在坦克節點裡面,那麼坐標是『坦克的相對坐標,坐標以坦克中心點為(0, 0) node1.setPosition(cc.v2(0, 400)); } // update (dt) {} }