- 布局如下:
- 編寫移動腳本
cc.Class({
extends: cc.Component,
properties: {
isAdd: {}//坐标是加還是減
},
// LIFE-CYCLE CALLBACKS:
onLoad() {
//滑鼠拖動移動
this.node.on(cc.Node.EventType.TOUCH_MOVE, function (event) {
this.opacity = 100;
var delta = event.touch.getDelta();
this.x += delta.x;
this.y += delta.y;
}, this.node);
//選中變透明
this.node.on(cc.Node.EventType.TOUCH_END, function () {
this.opacity = 255;
}, this.node);
},
update(dt) {
// 根據目前速度更新主角的位置
if (this.node.x >= 0) {
this.isAdd = false;
} else if (this.node.y <= -150) {
this.isAdd = true;
}
if (this.isAdd) {
this.node.x += 20 * dt;
this.node.y += 20 * dt;
} else {
this.node.x -= 20 * dt;
this.node.y -= 20 * dt;
}
},
});
- 選擇主角元件,如圖的方式添加腳本
- 運作項目如圖: