怎样在CocosCreator中使用游戏手柄
网络编程 2021-07-04 14:07www.168986.cn编程入门
这篇文章主要介绍了怎样使用CocosCreator的游戏手柄,每一个步骤都挺详细,资源可以随便找一些,希望同学们看完,一定要试一下
一.场景布置
二. 添加手柄监听器
1.监听事件的变化
由原先的mouse系列的转换为touch系列的
- touchstart 触摸按下,相当于 mousedown
- touchmove 触摸移动,相当于 mousemove
- touchend 触摸抬起,相当于 mouseup
- touchcancel 触摸取消,被其他事件终止,相当于按下了ESC键
2.坐标设定
当触摸按下随推动位置变化(要用世界坐标转换),触摸抬起后回归原位(直接设定0,0坐标默认相对坐标)。
setPosition()设定的为相对父节点的坐标
onTouchMove(e:.Event.EventTouch){ // e.getLocation() 为点击的位置,是世界坐标 // 需要把世界坐标转换为本地坐标 let parent=this.node.parent;// 父节点 (圆形底盘) let pos:.Vec2=parent.convertToNodeSpaceAR(e.getLocation()); this.node.setPosition(pos); } onTouchCancel(){ this.node.setPosition(.v3(0,0,0)); }
3. 将手柄限制在托盘内
使用方位角来定位边缘位置。pos.normalize()方法返回该点相对于(0,0)的(cos, sin),返回Vec2对象。
let parent=this.node.parent;// 父节点 (圆形底盘) let pos:.Vec2=parent.convertToNodeSpaceAR(e.getLocation()); // 该点所在的方位 (cos, sin) let direction:.Vec2=pos.normalize(); // 限制在边界之内 let maxR = 100-20; //点击的点到托盘中央的距离 let r : number = .Vec2.distance(pos, .v2(0,0)); if( r > maxR) { pos.x = maxR direction.x; pos.y = maxR direction.y; } // .log("相对位置: " + pos.x + ", " + pos.y); this.node.setPosition( pos);
三. 添加小车的控制
1. 小车的旋转
.Node.angle
表示旋转的角度,逆时针为正
官方建议不要使用 .Node.rotationa.signAngle( b)
a和b为两个向量,返回值是一a,b的夹角 (弧度值)
radian = a.signAngle(b)
(1) a位于b的顺时针方向角度为正
(2) a位于b的逆时针方向角度为负
旋转实现
添加属性 car :.Node=null;获取小车节点。
.find()注意参数用"/"除号的斜杠,否则识别不到
onLoad () { this.car=.find("Canvas/小车"); }
let radian=pos.signAngle(.v2(1,0));//计算点击位置与水平的夹角 let ang=radian/Math.PI180;//弧度制转换为角度值 this.car.angle=-ang;//逆时针为正,所以这里要调整至顺时针
2. 小车的移动
- 在小车的脚本中添加前进的动画,update(dt)方法中让x和y每帧加对应的速度在x和y轴的分量。
- 在手柄控制脚本中获取小车节点下的脚本。通过上面获取的direction的方向角,传入小车脚本中。通过控制direction来控制小车的移动。
小车运动脚本
direction: .Vec2 = null; speed: number = 3; onLoad() { } start() { } update(dt) { if (this.direction == null) return; //静止 let dx = this.speed this.direction.x; let dy = this.speed this.direction.y; let pos = this.node.getPosition(); pos.x += dx; pos.y += dy; this.node.setPosition(pos); }
手柄控制脚本
car: .Node = null; carscript: .Component = null; // LIFE-CYCLE CALLBACKS: onLoad() { this.car = .find("Canvas/小车"); this.carscript = this.car.getComponent("CarMove"); } start() { this.node.on('touchstart', this.onTouchStart, this); this.node.on('touchmove', this.onTouchMove, this); this.node.on('touchend', this.onTouchCancel, this); this.node.on('touchcancel', this.onTouchCancel, this); } onTouchStart() { } onTouchMove(e: .Event.EventTouch) { // e.getLocation() 为点击的位置,是世界坐标 // 需要把世界坐标转换为本地坐标 // let parent=this.node.parent;// 父节点 (圆形底盘) // let pos:.Vec2=parent.convertToNodeSpaceAR(e.getLocation()); // this.node.setPosition(pos); let parent = this.node.parent; // 父节点 (圆形底盘) let pos: .Vec2 = parent.convertToNodeSpaceAR(e.getLocation()); // 该点所在的方位 (cos, sin) let direction: .Vec2 = pos.normalize(); // 限制在边界之内 let maxR = 100 - 20; let r: number = .Vec2.distance(pos, .v2(0, 0)); if (r > maxR) { pos.x = maxR direction.x; pos.y = maxR direction.y; } // .log("相对位置: " + pos.x + ", " + pos.y); this.node.setPosition(pos); let radian = pos.signAngle(.v2(1, 0)); //计算点击位置与水平的夹角 let ang = radian / Math.PI 180; //弧度制转换为角度值 this.car.angle = -ang; //逆时针为正,所以这里要调整至顺时针 this.carscript.direction = direction; } onTouchCancel() { this.node.setPosition(.v3(0, 0, 0)); //将方向置空,使汽车停止 this.carscript.direction = null; } // update (dt) {}
最终效果
以上就是怎样在CocosCreator中使用游戏手柄的详细内容,更多关于CocosCreator手柄实例的资料请关注狼蚁SEO其它相关文章!
上一篇:Vue项目中实现带参跳转功能
下一篇:Vue.js源码分析之自定义指令详解
编程语言
- 甘肃哪有关键词排名优化购买方式有哪些
- 甘肃SEO如何做网站优化
- 河南seo关键词优化怎么做电话营销
- 北京SEO优化如何做QQ群营销
- 来宾百度关键词排名:提升您网站曝光率的关键
- 卢龙关键词优化:提升您网站排名的策略与技巧
- 山东网站优化的注意事项有哪些
- 四川整站优化怎样提升在搜索引擎中的排名
- 疏附整站优化:提升网站性能与用户体验的全新
- 海南seo主要做什么工作售后服务要做到哪些
- 荣昌百度网站优化:提升您网站的搜索引擎排名
- 河北seo网站排名关键词优化如何做SEO
- 江西优化关键词排名推广售后保障一般有哪些
- 古浪SEO优化:提升你的网站可见性
- 西藏网站排名优化怎么把网站排名在百度首页
- 如何提升阳东百度快照排名:详尽指南