cocos creator 摇杆
手机虚拟摇杆是指用于全触摸手机的虚拟于屏幕的按键虚拟摇杆,可直接操控触摸屏上的虚拟摇杆进行游戏。
这个摇杆的效果需要的了解的是数学的三角函数知识。这里有分成两个脚本,第一个脚本控制摇杆的一些效果。第二个脚本是控制主角的移动
1.这是joystick.js脚本
cc.Class({
extends: cc.Component,
properties: {
stick: cc.Node,
//stick_bg的宽度
r: 200,
},
onLoad() {
//触摸事件
this.stick.on("touchstart", this.onTouchStart, this)
this.stick.on("touchmove", this.onTouchMove, this)
this.stick.on("touchend", this.onTouchEnd, this)
this.stick.on("touchcancel", this.onTouchCancel, this)
//dir表示stick的方向
this.dir = cc.v2(0, 0)
},
onDestroy() {
this.stick.off("touchstart", this.onTouchStart, this)
this.stick.off("touchmove", this.onTouchMove, this)
t