// 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.Label) lb1: cc.Label = null; @property t1: string = 'hello'; //大雄移動的方向(向量),一開始向右 direction = cc.v2(1,0); // LIFE-CYCLE CALLBACKS: // onLoad () {} start () { //this.node.on("mousedown",this.move, this); cc.systemEvent.on("keydown",this.move, this); } move(e1:cc.Event.EventKeyboard) { if(e1.keyCode == cc.macro.KEY.right) { this.direction = cc.v2(1,0); } else if(e1.keyCode==cc.macro.KEY.left) { this.direction = cc.v2(-1,0); } else if(e1.keyCode == cc.macro.KEY.up) { this.direction = cc.v2(0,1); } else if(e1.keyCode == cc.macro.KEY.down) { this.direction = cc.v2(0,-1); } } //幀動畫 update (dt) { this.node.x += 5*this.direction.x; this.node.y += 5*this.direction.y; } }