1

我在andengine中创建了一个赛车游戏。有一个大小为100x30px的玩家,像一个矩形。并且有fire()函数来射击子弹,代码如下。它工作正常。但问题是当玩家旋转时。玩家是汽车图像。所以它们通过方向按钮()旋转。当我射击子弹时,它从汽车的 0x0 像素开始。但我希望它必须像 100x15 像素的汽车一样开始在汽车前面移动。我该怎么办?

public void fire() {


        float startBulletX=player.getX();
        float startBulletY=player.getY();
        Sprite bullet=new Sprite(startBulletX,startBulletY,bulletRegion);

           final float xComp = (float) Math.cos(MathUtils.degToRad(this.player.getRotation() + 90));
           final float yComp = (float) Math.sin(MathUtils.degToRad(this.player.getRotation() + 90));
           final Vector2 velocity = Vector2Pool.obtain(xComp * 10, yComp * 10);


           bullet.setRotation(this.player.getRotation());

           final FixtureDef bulletFixtureDef1 = PhysicsFactory.createFixtureDef(0, 0, 0);
           this.mBulletBody = PhysicsFactory.createBoxBody(this.mPhysicsWorld, bullet, BodyType.KinematicBody, bulletFixtureDef1);
           this.mBulletBody.setLinearVelocity(velocity);
           Vector2Pool.recycle(velocity);
           this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(bullet, this.mBulletBody, true, false));




        this.scene.attachChild(bullet);
    }

//屏幕控制按钮

 public void addDirectionButton() {
       player = new AnimatedSprite(centerX, centerY, this.playerTextureRegion);
        final AnalogOnScreenControl analogOnScreenControl = new AnalogOnScreenControl(0, CAMERA_HEIGHT - this.mOnScreenControlBaseTextureRegion.getHeight(), this.mBoundChaseCamera, this.mOnScreenControlBaseTextureRegion, this.mOnScreenControlKnobTextureRegion, 0.1f, 200, new IAnalogOnScreenControlListener() {

                 public void onControlChange(final BaseOnScreenControl pBaseOnScreenControl, final float pValueX, final float pValueY) {

                         final Body carBody = DeffenseActivity.this.mCarBody;

                         final Vector2 velocity = Vector2Pool.obtain(pValueX * 5, pValueY * 5);
                         carBody.setLinearVelocity(velocity);
                         Vector2Pool.recycle(velocity);

                         final float rotationInRad = (float)Math.atan2(-pValueX, pValueY);

                         player.setRotation(MathUtils.radToDeg(rotationInRad));

                 }

                 public void onControlClick(final AnalogOnScreenControl pAnalogOnScreenControl) {

                 }
         });
         analogOnScreenControl.getControlBase().setBlendFunction(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
         analogOnScreenControl.getControlBase().setAlpha(0.5f);
         analogOnScreenControl.getControlBase().setScaleCenter(0, 128);
         analogOnScreenControl.getControlBase().setScale(1.25f);
         analogOnScreenControl.getControlKnob().setScale(1.25f);
         analogOnScreenControl.refreshControlKnobPosition();

         this.scene.setChildScene(analogOnScreenControl);

         this.mBoundChaseCamera.setChaseEntity(player);
       a=new Sprite(0,0,bulletRegion);
         player.attachChild(a);

         final FixtureDef carFixtureDef = PhysicsFactory.createFixtureDef(1, 0.5f, 0.5f);
         this.mCarBody = PhysicsFactory.createBoxBody(this.mPhysicsWorld, player, BodyType.DynamicBody, carFixtureDef);
         this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(player, this.mCarBody, true, false));
         this.scene.attachChild(player);

       }
4

0 回答 0