cocos creator 摇杆制作
程序员文章站
2022-03-31 20:38:31
...
首先触摸事件注册
initTouchEvent() {
this.node.on(cc.Node.EventType.TOUCH_START, this.touchStartEvent, this);
this.node.on(cc.Node.EventType.TOUCH_MOVE, this.touchMoveEvent, this);
this.node.on(cc.Node.EventType.TOUCH_END, this.touchEndEvent, this);
this.node.on(cc.Node.EventType.TOUCH_CANCEL, this.touchEndEvent, this);
}
固定的点击出现,跟随的点击出现在点击位置再判断
touchStartEvent(event) {
let startTouchPos = this.node.convertToNodeSpaceAR(event.getLocation());
if (this.joyStickType == JoyStickType.FIXED) {
this.stickPos = this.rockerNode.getPosition();
let distance = startTouchPos.sub(this.rockerNode.getPosition()).mag();
if (this.radius > distance) {
this.smallrockerNode.active = true;
this.smallrockerNode.setPosition(startTouchPos);
}
}
else if (this.joyStickType == JoyStickType.FOLLOW) {
this.stickPos = startTouchPos;
this.rockerNode.opacity = 255;
this.smallrockerNode.opacity = 255;
this.touchPos = event.getLocation();
this.rockerNode.setPosition(startTouchPos);
this.smallrockerNode.setPosition(startTouchPos);
}
}
移动摇杆的时候获得移动距离 dis 选择快速移动或者慢速移动
获得p 向量方向
touchMoveEvent(event) {
if (this.joyStickType == JoyStickType.FOLLOW) {
if (this.touchPos == event.getLocation()) {
return;
}
}
//将触摸坐标 转化到 bg 下, 此时 的dis 就是 触摸点相对于bg 的距离
let touchP = this.rockerNode.convertToNodeSpaceAR(event.getLocation());
let dis = touchP.mag();
console.log(dis, "disdisdisdisdisdisdis")
let posX = this.stickPos.x + touchP.x;
let posY = this.stickPos.y + touchP.y;
//向量归一(获取方向)
let p = cc.v2(posX, posY).sub(this.rockerNode.getPosition()).normalize();
if (this.radius > dis) {
this.smallrockerNode.setPosition(cc.v2(posX, posY));
this.playerScr.speedType = SpeedType.NORMAL;
}
else {
//摇杆在圈边
let nowPosX = this.stickPos.x + p.x * this.radius;
let nowPosy = this.stickPos.y + p.y * this.radius;
this.smallrockerNode.setPosition(cc.v2(nowPosX, nowPosy));
this.playerScr.speedType = SpeedType.FAST;
}
this.playerScr.moveDir = p;
}
结束时隐藏
touchEndEvent(event) {
this.smallrockerNode.setPosition(this.rockerNode.getPosition());
if (this.joyStickType == JoyStickType.FOLLOW) {
this.rockerNode.opacity = 0;
this.smallrockerNode.opacity = 0;
}
this.playerScr.speedType = SpeedType.STOP;
}
update里做移动
move() {
//radiansToDegrees弧度转角度。atan2返回从 x 轴到点 (x,y) 之间的角度 Math.atan2(y,x),结果为弧度
if (this.playerScr.moveDir) {
this.playerScr.rotation = 90 - cc.misc.radiansToDegrees(Math.atan2(this.playerScr.moveDir.y, this.playerScr.moveDir.x));
//缩放向量
let newPos = this.playerScr.position.add(this.playerScr.moveDir.mul(this.moveSpeed / 60));
this.playerScr.setPosition(newPos);
}
//子弹移动
for (let i = 0; i < this.node.childrenCount; i++) {
if (this.node.children[i].name == "bullet" && this.node.children[i].newPos) {
let newbulletPos = this.node.children[i].position.add(this.node.children[i].newPos.mul(this.bulletSpeed / 60))
this.node.children[i].setPosition(newbulletPos)
}
}
}
发射子弹时获得坦克的方向
Firingbullets() {
let bulletNode = cc.instantiate(this.bulletNode);
bulletNode.parent = this.node;
bulletNode.setPosition(this.playerScr.x, this.playerScr.y);
bulletNode.rotation = 90 - cc.misc.radiansToDegrees(Math.atan2(this.playerScr.moveDir.y, this.playerScr.moveDir.x))
//缩放向量
let p = this.playerScr.moveDir;
bulletNode.newPos = p
}
效果
推荐阅读
-
抖音道具制作工具Effect Creator安装新建保存上传教程
-
Unity使用ScrollRect制作摇杆
-
【Cocos谁学谁会】制作会跑动的地板
-
cocos creator入门
-
cocos creator Touch事件应用(触控选择多个子节点的实例)
-
全球首家!Cocos Creator正式支持HarmonyOS多设备协同
-
Cocos Creator 热更新工具BUG:重启游戏后热更新无效!(已解决)
-
荐 麒麟子Cocos Creator 3D研究笔记三:角色换装(无动画)
-
用 shader effect 实现雨滴落水效果!Cocos Creator 3D !
-
cocos creator 3D | 拇指投篮 | 3D项目入门实战