moveScript.ts 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. // 以左下角开始为原点开始计算
  2. const { ccclass, property } = cc._decorator;
  3. @ccclass
  4. export default class MoveScript extends cc.Component {
  5. canMove: boolean = false
  6. start() {
  7. this.node.on(cc.Node.EventType.TOUCH_START, (event: cc.Event.EventTouch) => {
  8. this.canMove = true
  9. })
  10. this.node.on(cc.Node.EventType.TOUCH_MOVE, (event: cc.Event.EventTouch) => {
  11. if (this.canMove == false) { return }
  12. if (this.node.x + event.getDelta().x > 0) {
  13. this.node.x = 0
  14. return
  15. }
  16. if (this.node.x + event.getDelta().x < cc.winSize.width - this.node.width) {
  17. this.node.x = cc.winSize.width - this.node.width
  18. return
  19. }
  20. this.node.x += event.getDelta().x
  21. // if (this.node.x > -cc.winSize.width / 2 + this.node.width / 2 * this.node.scale) {
  22. // this.node.x = this.node.width / 2 * this.node.scale - cc.winSize.width / 2
  23. // }
  24. // if (this.node.x < -this.node.width / 2 * this.node.scale + cc.winSize.width / 2) {
  25. // this.node.x = -this.node.width / 2 * this.node.scale + cc.winSize.width / 2
  26. // }
  27. // if (this.node.x > this.node.width / 2 * this.node.scale) {
  28. // this.node.x = this.node.width / 2 * this.node.scale
  29. // }
  30. // if (this.node.x < -this.node.width / 2 * this.node.scale) {
  31. // this.node.x = -this.node.width / 2 * this.node.scale
  32. // }
  33. })
  34. this.node.on(cc.Node.EventType.TOUCH_CANCEL, (event: cc.Event.EventTouch) => {
  35. this.canMove = false
  36. })
  37. this.node.on(cc.Node.EventType.TOUCH_END, (event: cc.Event.EventTouch) => {
  38. this.canMove = false
  39. })
  40. }
  41. }