我在andengine中创建了一个赛车游戏。有一个尺寸为100x30px的玩家,就像一个矩形。还有fire()函数来射击子弹,代码在下面。它工作正常。但问题是当玩家旋转.player是一个汽车image.so他们旋转directionButton()。当我射击子弹时,它开始0x0px的汽车。但我想它必须开始在汽车前面像100x15px的车一样。我怎么办?
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);
}