cocos creator从零开发虚拟摇杆(完结)-跟随移动摇杆

关注干货悦读公众号,获取摇杆资源。

编辑scripts/JoyStick.ts,修改onTouchMove方法。

private onTouchMove(event: cc.Event.EventTouch) {
    if (!this._canMove) return

    const posDelta = event.getDelta()

    if (this.joystickType == JoystickType.Fixed || this.joystickType == JoystickType.Follow) {
        this.dotNode.setPosition(this.dotNode.getPosition().add(posDelta))

        const len = this.dotNode.position.len()
        const maxLen = this.ringNode.width / 2
        const ratio = len / maxLen

        if (ratio > 1) {
            this.dotNode.setPosition(this.dotNode.position.div(ratio))
        }
    } else {
        const dotPos = this.dotNode.getPosition()
        const dotPos2 = dotPos.add(posDelta)
        if (dotPos2.len() <= this.ringNode.width / 2) {
            this.dotNode.setPosition(dotPos2)
        } else {
            const dir = dotPos2.normalize()
            this.dotNode.setPosition(dir.mul(this.ringNode.width / 2))

            const dist = dotPos2.len() - this.ringNode.width / 2
            this.ringNode.setPosition(this.ringNode.getPosition().add(dir.mul(dist)))
        }
    }

    const dir = this.dotNode.getPosition().normalize()
    this.handlers.forEach(handler => handler.emit([dir]))
    this.node.emit('JoyStick', dir)
}

场景修改JoyStick节点joystickType值为FollowMove并运行程序,跟随移动摇杆可以移动了。

跟随移动摇杆是移动时,如果dot节点在最大范围内则只移动dot节点。如果超出最大范围则把dot节点设置在最大范围位置,ring节点则在当前方向加上dot节点超出的距离。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值