cocos creator从零开发虚拟摇杆(05)-摇杆事件测试

Canvas节点下创建ParticleSystem (粒子)并重命名为player

创建player

新建scripts/Game.ts,内容如下,把它挂载到Canvas节点上。

const { ccclass } = cc._decorator


@ccclass
export default class Game extends cc.Component {
    private player: cc.Node
    private speed: number = 300

    private _dir: cc.Vec2 = cc.Vec2.ZERO


    protected onLoad(): void {
        this.player = this.node.getChildByName('player')

        this.node.getChildByName('JoyStick').on('JoyStick', this.onJoyStick, this)
    }

    protected update(dt: number): void {
        if (this._dir.lengthSqr() <= 0) return

        const dis = this._dir.mul(this.speed * dt)
        const pos = this.player.getPosition().add(dis)

        this.player.setPosition(pos)
    }


    public onJoyStick(dir: cc.Vec2) {
        this._dir = dir
    }
}

运行程序,player节点可以被虚拟摇杆控制了。

接收摇杆事件一共有两种方式,我这里监听摇杆节点的JoyStick事件了,还有一种方式就是像使用Button组件的点击事件一样手动绑定回调方法。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值