// 須要先給節點綁定cc.Graphics組件 var graphics = this.getComponent(cc.Graphics); // 布徑, moveTo graphics.moveTo(-50, 50); graphics.lineTo(-50, -50); graphics.lineTo(50, -50); graphics.lineTo(50, 50); graphics.close(); // 組成一個封閉的路徑 // end // 畫線,填充; graphics.stroke(); graphics.fill(); // graphics.clear();
//follow_target.js 攝像機跟隨 cc.Class({ extends: cc.Component, properties: { // foo: { // // ATTRIBUTES: // default: null, // The default value will be used only when the component attaching // // to a node for the first time // type: cc.SpriteFrame, // optional, default is typeof default // serializable: true, // optional, default is true // }, // bar: { // get () { // return this._bar; // }, // set (value) { // this._bar = value; // } // }, target: { type: cc.Node, default: null, }, }, // LIFE-CYCLE CALLBACKS: // onLoad () {}, start() { }, update(dt) { if (this.target === null) { return; } var w_pos = this.target.convertToWorldSpaceAR(cc.v2(0, 0)); // cc.v2 ---> cc.p var pos = this.node.parent.convertToNodeSpaceAR(w_pos); this.node.x = pos.x; this.node.y = pos.y; }, });
// game_ctrl.js 捕捉touchevent var nav_agent = require("nav_agent"); cc.Class({ extends: cc.Component, properties: { // foo: { // // ATTRIBUTES: // default: null, // The default value will be used only when the component attaching // // to a node for the first time // type: cc.SpriteFrame, // optional, default is typeof default // serializable: true, // optional, default is true // }, // bar: { // get () { // return this._bar; // }, // set (value) { // this._bar = value; // } // }, camera: { type: cc.Camera, default: null, }, player: { type: nav_agent, default: null, }, map: { type: cc.Node, default: null, }, }, // LIFE-CYCLE CALLBACKS: // onLoad () {}, start() { this.node.on(cc.Node.EventType.TOUCH_START, function(e) { var camrea_pos = e.getLocation(); // 除非camrea (0, 0) 否者你要轉換一下啊; var w_pos = this.camera.getCameraToWorldPoint(camrea_pos); var pos = this.map.convertToNodeSpaceAR(w_pos); this.player.walk_to_map(pos); }.bind(this), this); }, // update (dt) {}, });
// nav_agent.js角色位移代碼 cc.Class({ extends: cc.Component, properties: { // foo: { // // ATTRIBUTES: // default: null, // The default value will be used only when the component attaching // // to a node for the first time // type: cc.SpriteFrame, // optional, default is typeof default // serializable: true, // optional, default is true // }, // bar: { // get () { // return this._bar; // }, // set (value) { // this._bar = value; // } // }, speed: 100, }, // LIFE-CYCLE CALLBACKS: // onLoad () {}, start() { this.is_waling = false; }, walk_to_map(dst) { var src = this.node.getPosition(); var dir = dst.sub(src); // cc.pSub dst.sub; cc.pSub(dst, src); var len = dir.mag(); // cc.pLength; if (len <= 0) { return; } this.walk_time = len / this.speed; this.now_time = 0; this.vx = this.speed * (dir.x / len); this.vy = this.speed * (dir.y / len); this.is_waling = true; }, update(dt) { if (this.is_waling === false) { return; } this.now_time += dt; if (this.now_time > this.walk_time) { dt -= (this.now_time - this.walk_time); } var sx = this.vx * dt; var sy = this.vy * dt; this.node.x += sx; this.node.y += sy; if (this.now_time >= this.walk_time) { this.is_waling = false; } }, });