Android 安发动机-在旋转枪前发射子弹

Android 安发动机-在旋转枪前发射子弹,android,andengine,Android,Andengine,你好,我在论坛上搜索过,但没有找到有用的答案。 我正在和安德林做一个游戏,我被旋转精灵的射击卡住了3天 这是我的代码,也是我旋转枪的方式。我试着在这里射出一颗子弹,但是它从一个错误的起点射出,我想从枪的末端射出一颗子弹 @覆盖 公共布尔onSceneTouchEvent(场景pScene、触摸事件pSceneTouchEvent){ if(pSceneTouchEvent.isActionMove()){ 最终浮点dX=pSceneTouchEvent.getX()-machine.getX()

你好,我在论坛上搜索过,但没有找到有用的答案。 我正在和安德林做一个游戏,我被旋转精灵的射击卡住了3天

这是我的代码,也是我旋转枪的方式。我试着在这里射出一颗子弹,但是它从一个错误的起点射出,我想从枪的末端射出一颗子弹

@覆盖
公共布尔onSceneTouchEvent(场景pScene、触摸事件pSceneTouchEvent){
if(pSceneTouchEvent.isActionMove()){
最终浮点dX=pSceneTouchEvent.getX()-machine.getX();
最终浮点dY=pSceneTouchEvent.getY()-machine.getY();
浮动角度=(浮动)数学atan2(dX,dY);
浮动旋转=MathUtils.radToDeg(角度)+1;
机器设置旋转(旋转-90);
Log.d(“BUG”,machine.getRotation()+);
if(machine.getRotation()>=84){
机器设置旋转(84);
}
if(machine.getRotation()0&&sElapsed>cooldownBetweenShoot*cdModd){
e=新实体(0,machine.getY());
e、 setRotation(getRotation());
SceneManager.getInstance().getCurrentScene().attachChild(e);
float x2=(float)(machine.getscenecentercoardinates()[0]+machine.getWidth()/2*Math.cos(machine.getRotation());
float y2=(float)(machine.getscenecentercoardinates()[1]+machine.getWidth()/2*Math.sin(machine.getRotation());
float realX=(float)(Math.toRadians(x2)+machine.getWidth());
realY=(float)Math.toRadians(y2);
项目符号=新精灵(realX、realY、resourcesManager.bulletRegion.deepCopy()、vbom){
ManageDupDate上的受保护无效(浮点psecondsAppeased){
float currentX=this.getX();
这个.setX(currentX+incrementXValue);
super.onManagedUpdate(psecondseased);
}
};
子弹。设定刻度(0.06f);
e、 攻击儿童(子弹);
ProjectleTobeAdded.add(项目符号);
我要出去--;
lastFire=System.currentTimeMillis();
setBulletsText(公告);
resourcesManager.PistonsSound.play();
}
返回true;
}
返回false;
}

假设您使用的是GLES2 AnchorCenter:

  • 您可以通过将子弹设置为枪管末端的位置来定位子弹,该位置可以通过调用
    gun.convertLocalToSceneCoordinates(gunMuzzleX,gunMuzzleY)
    获得

  • 然后将子弹旋转设置为枪的旋转

  • 对子弹施加速度。按照以下方式计算速度矢量
    FloatMath.sin(旋转fBulletinradians)*速度
    FloatMath.cos(旋转fBulletinradians)*速度
    请注意,必须将旋转以弧度为单位传递给正反函数,而不是以度为单位


  • 所以我找到了解决办法

    问题出在这一行代码中:

    e=新实体(0,machine.getY());
    
    应该是:

    e=新实体(machine.getX()-(machine.getHeight()/2),machine.getY())
    
    您好,谢谢您的回复,但我有一些问题,如何获取gunMuzzleX和gunMuzzleY?2.我认为这应该可以工作。setRotation(gun.getRotation());3.如何获得子弹的弧度旋转,为什么要使用速度,还有一个问题,在我有了gunMuzzleX和gunMuzzleY之后,如何在我的代码中使用它?我也尝试了这个,但没有成功:ps.setVelocity(FloatMath.sin((float)Math.toRadians(bullets.getRotation())*100,FloatMath.cos((float)Math.toRadians(bullets.getRotation())*100); 项目符号。注册表更新处理程序(ps);我转动枪,我不知道如何将子弹的起点设置在枪的末端我不明白你为什么接受你自己的答案而不是我的答案。我已经列出了你问题的所有可能根源。但最终还是由你决定。。。