移动使用TOUCH监控,这样能兼容到PC和移动端:
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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
| export default class Player extends cc.Component { @property moveSpeed: number = 100;
@property moveDuration: number = 8; targetPosition = { x: 0, y: 0, }; maxY:number = 0
minX:number = 0 maxX:number = 0;
isMoving: boolean = false; start() { this.targetPosition.x = this.node.x; this.targetPosition.y = this.node.y; this.maxY = this.node.getParent().height - cc.find(CompPath.TitleMenu).height - this.node.height let xPad = 15 this.minX = xPad + this.node.width/2 this.maxX = this.node.getParent().width + xPad - this.node.width this.moveByTouch() } moveByTouch(){ const touchWindow = cc.find(CompPath.MainGameWindow) const titleMenu = cc.find(CompPath.TitleMenu) const maxY = titleMenu.y - titleMenu.height touchWindow.on(cc.Node.EventType.TOUCH_START,(event:cc.Event.EventTouch)=>{ if(globalVar.gamePause) return this.isMoving = true }) touchWindow.on(cc.Node.EventType.TOUCH_END,(event:cc.Event.EventTouch)=>{ if(globalVar.gamePause) return this.isMoving = false }) touchWindow.on(cc.Node.EventType.TOUCH_CANCEL,(event:cc.Event.EventTouch)=>{ if(globalVar.gamePause) return this.isMoving = false }) touchWindow.on(cc.Node.EventType.TOUCH_MOVE,(event:cc.Event.EventTouch)=>{ if(globalVar.gamePause) return if(this.isMoving){ let ex = event.getLocationX(), ey = event.getLocationY() if(ey - touchWindow.y <=0 || ey >= maxY) { return }else if(ex <= this.minX || ex >= (this.maxX + 15)){ return } this.targetPosition.x = ex - 15 this.targetPosition.y =ey - touchWindow.y } }) } update(dt) { this.node.x += (this.targetPosition.x - this.node.x) / this.moveDuration; this.node.y += (this.targetPosition.y - this.node.y) / this.moveDuration; } }
|
至于发射子弹,一开始是打算由玩家操控子弹射击,
但由于移动端操作的界面比较小,如果要操控射击,就需要单独设计操控面板,
考虑到只是个边学边做的小游戏,就没设计这个(其实就是懒)
后来发现让主角跟随触碰点移动,会让角色完全被手指盖住(悲)
这下理解为什么手机游戏基本都是用摇杆或者指击控制角色移动了。