开始
第一种全局变量方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
|
const {ccclass, property} = cc._decorator;
@ccclass export default class CameraMove extends cc.Component {
private endBoundaryNum: number = 960;
public onLoad (): void { this.isLocationX(); this.openTouch(); }
private isLocationX (): void { let local_x = window['locationX']; local_x ? this.node.x = local_x : 0; }
private openTouch (): void { this.node.on(cc.Node.EventType.TOUCH_MOVE, this.touchStart, this); }
private touchStart (event: cc.Event.EventTouch): void { let x = this.node.x - event.getDeltaX(); this.node.x = x <= 0 ? 0 : (x >= this.endBoundaryNum ? this.endBoundaryNum : x); window['locationX'] = x; }
}
|
第二种存入本地缓存中
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| const {ccclass, property} = cc._decorator;
@ccclass export default class CameraMove extends cc.Component { private endBoundaryNum: number = 960;
public onLoad (): void { this.isLocationX(); this.openTouch(); }
private isLocationX (): void { let local_x = localStorage.getItem('isLocationX'); local_x ? this.node.x = parseFloat(local_x) : 0; }
private openTouch (): void { this.node.on(cc.Node.EventType.TOUCH_MOVE, this.touchStart, this); }
private touchStart (event: cc.Event.EventTouch): void { let x = this.node.x - event.getDeltaX(); this.node.x = x <= 0 ? 0 : (x >= this.endBoundaryNum ? this.endBoundaryNum : x); localStorage.setItem('isLocationX',x + ''); }
}
|
两种方案的区别主要在于下次打开应用是从零开始还是回到上次关闭前的位置,可以根据具体需求进行使用。
最终效果
