想写一个主角突突突打怪的游戏,主角移动时可向四周发射子弹。
滑动左边半个屏幕控制主角移动,右边模拟摇杆控制子弹发射方向。
主角站在x:0,y:0的位置子弹方向一点问题没有, 主角移动后子弹就歪了。
求大神帮忙看看代码,万分感谢!
HeroProject.zip (220.5 KB)
想写一个主角突突突打怪的游戏,主角移动时可向四周发射子弹。
滑动左边半个屏幕控制主角移动,右边模拟摇杆控制子弹发射方向。
主角站在x:0,y:0的位置子弹方向一点问题没有, 主角移动后子弹就歪了。
求大神帮忙看看代码,万分感谢!
HeroProject.zip (220.5 KB)
bullet.getComponent("HeroBullet").init(this.bulletDirectionX, this.bulletDirectionY, this.bulletPos);
dragMove(event) {
let locationv = event.getLocation();
let location = this.node.convertToNodeSpaceAR(locationv);
this.directionNode.rotation = -Math.atan2(location.y, location.x) * 180 / Math.PI - 90;
let angle = -this.directionNode.rotation - 270;
let offsetX = -Math.cos(angle * 0.017453293) * 56;
let offsetY = -Math.sin(angle * 0.017453293) * 56;
console.log("offset: " + offsetX + " , " + offsetY);
console.log("hero: " + this.heroNode.position);
let realPos = this.heroNode.position.add(cc.v2(offsetX, offsetY));
console.log("sub: " + realPos);
this.bulletPos = realPos
this.bulletDirectionX = offsetX;
this.bulletDirectionY = offsetY;
},
init(directionX, directionY, bulletPos) {
this.node.position = bulletPos;
let rigidBody = this.getComponent(cc.RigidBody);
rigidBody.linearVelocity = cc.v2(directionX, directionY);
},
照着这个改就好了
只是BulletNode没有初始化正确的位置嘛。 我太疏忽了! 谢谢大佬~