// 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) // label: cc.Label = null; // @property // text: string = 'hello'; //圖片編號號碼 num = 0; //sprite陣列 @property(cc.SpriteFrame) sprfrms = []; //sprfrms = new Array(); //速度 speed = 10; onLoad () { //啟動計時器 this.schedule(this.timer, 0.1); //偵測鍵盤事件 cc.systemEvent.on("keydown",this.move, this); } timer() { //圖片編號+1 this.num += 1; if(this.num >= 18)this.num =0; //抓取sprite節點的sprite組件 var sprcomp1 = this.getComponent(cc.Sprite); //顯示圖片 sprcomp1.spriteFrame = this.sprfrms[this.num]; } move(e:cc.Event.EventKeyboard) { if(e.keyCode == cc.macro.KEY.left) this.node.x -= this.speed; else if(e.keyCode == cc.macro.KEY.right) this.node.x += this.speed; else if(e.keyCode == cc.macro.KEY.up) this.node.y += this.speed; else if(e.keyCode == cc.macro.KEY.down) this.node.y -= this.speed; } start () { } // update (dt) {} }