// 以左下角开始为原点开始计算 const { ccclass, property } = cc._decorator; @ccclass export default class MoveScript extends cc.Component { canMove: boolean = false start() { this.node.on(cc.Node.EventType.TOUCH_START, (event: cc.Event.EventTouch) => { this.canMove = true }) this.node.on(cc.Node.EventType.TOUCH_MOVE, (event: cc.Event.EventTouch) => { if (this.canMove == false) { return } if (this.node.x + event.getDelta().x > 0) { this.node.x = 0 return } if (this.node.x + event.getDelta().x < cc.winSize.width - this.node.width) { this.node.x = cc.winSize.width - this.node.width return } this.node.x += event.getDelta().x // if (this.node.x > -cc.winSize.width / 2 + this.node.width / 2 * this.node.scale) { // this.node.x = this.node.width / 2 * this.node.scale - cc.winSize.width / 2 // } // if (this.node.x < -this.node.width / 2 * this.node.scale + cc.winSize.width / 2) { // this.node.x = -this.node.width / 2 * this.node.scale + cc.winSize.width / 2 // } // if (this.node.x > this.node.width / 2 * this.node.scale) { // this.node.x = this.node.width / 2 * this.node.scale // } // if (this.node.x < -this.node.width / 2 * this.node.scale) { // this.node.x = -this.node.width / 2 * this.node.scale // } }) this.node.on(cc.Node.EventType.TOUCH_CANCEL, (event: cc.Event.EventTouch) => { this.canMove = false }) this.node.on(cc.Node.EventType.TOUCH_END, (event: cc.Event.EventTouch) => { this.canMove = false }) } }