// 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 { onLoad () { this.node.on("touchstart", this.show1, this); } show1 (e:cc.Event.EventTouch) { //抓取貓節點 var node1 = cc.find("Canvas/貓"); //抓取Canvas節點 var canvas1 = cc.find("Canvas"); //觸控點坐標 //(1)不好做法:觸控事件是世界坐標 //var pos1 = cc.v2(e.getLocationX(), e.getLocationY()); //(2)較好做法:把世界坐標,轉換成,局部坐標 //方法:局部坐標.x = 世界坐標.x - Canvas.width/2 //方法:局部坐標.y = 世界坐標.y - Canvas.height/2 var pos1 = cc.v2(e.getLocationX()-canvas1.width/2, e.getLocationY()-canvas1.height/2); //設定貓位置 = 觸控點坐標 node1.setPosition(pos1); } // update (dt) {} }