2011-08-24 17 views
-5

我正在使用Android的Andengine,並且我創建了一個代碼,當我觸摸屏幕時,我的播放器將會拍攝一顆子彈。AndEngine如何去除子彈並一次發射一顆子彈?

現在我想要做的兩件事情

第一:我想,當它與敵人 二碰撞被取出子彈:我希望能夠在同一時間只拍攝子彈。所以只要子彈沒有擊中敵人,我不希望發射子彈的方法被調用。

這裏是我創建 的代碼我最小化所有不屬於真正重要

public class ShooterActivity extends BaseGameActivity implements IOnSceneTouchListener,IAccelerometerListener{ 

//Variables 
private static final int CAMERA_WIDTH = 720; 
private static final int CAMERA_HEIGHT = 480; 
private Camera mCamera; 
private BitmapTextureAtlas mBitmapTextureAtlas; 
private TiledTextureRegion mTiledTextureRegion; 
private TextureRegion mBulletTextureRegion; 
private AnimatedSprite facebox; 
private AnimatedSprite enemy; 
private Sprite bulletsprite; 
private Scene mScene; 
private PhysicsWorld mPhysicsWorld; 
private Shape ground, roof, right, left; 
private Body body, bulletbody, enemybody; 
private FixtureDef mFixtureDef = PhysicsFactory.createFixtureDef(1, 0.5f, 0.5f); 
private TiledTextureRegion enemyTiled; 
private boolean pFlippedHorizontal = true; 



@Override 
public Engine onLoadEngine() { 
    // TODO Auto-generated method stub 
    this.mCamera = new Camera(0,0,CAMERA_WIDTH,CAMERA_HEIGHT); 
    return new Engine(new EngineOptions(true, ScreenOrientation.LANDSCAPE, new RatioResolutionPolicy(CAMERA_WIDTH,CAMERA_HEIGHT),this.mCamera)); 
} 

@Override 
public void onLoadResources() { 
    // TODO Auto-generated method stub 
    this.mBitmapTextureAtlas = new BitmapTextureAtlas(1024,1024, TextureOptions.BILINEAR_PREMULTIPLYALPHA); 
    this.mTiledTextureRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(mBitmapTextureAtlas, this, "gfx/player.png", 0, 0, 8, 1); 
    this.enemyTiled = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(mBitmapTextureAtlas, this, "gfx/enemy.png", 200, 500, 8, 1); 
    this.mBulletTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(mBitmapTextureAtlas, this, "gfx/badge.png", 200, 200); 
    this.mEngine.getTextureManager().loadTexture(mBitmapTextureAtlas); 
} 

@Override 
public Scene onLoadScene() { 
    // TODO Auto-generated method stub 
    this.mEngine.registerUpdateHandler(new FPSLogger()); 
    mScene = new Scene(); 
    mScene.setBackground(new ColorBackground(0,0,0)); 
    this.mPhysicsWorld = new PhysicsWorld(new Vector2(0,SensorManager.GRAVITY_EARTH), false); 
    //Walls 
    final FixtureDef wallFixture = PhysicsFactory.createFixtureDef(1, 0.5f, 0.5f); 
    roof = new Rectangle(0, 0, CAMERA_WIDTH, 2); 
    ground = new Rectangle(0,CAMERA_HEIGHT ,CAMERA_WIDTH,2); 
    left = new Rectangle(0,0,2,CAMERA_HEIGHT); 
    right = new Rectangle(CAMERA_WIDTH -2, 0,2,CAMERA_HEIGHT); 
    PhysicsFactory.createBoxBody(this.mPhysicsWorld, ground, BodyType.StaticBody, wallFixture); 
    PhysicsFactory.createBoxBody(this.mPhysicsWorld, roof, BodyType.StaticBody, wallFixture); 
    PhysicsFactory.createBoxBody(this.mPhysicsWorld, left, BodyType.StaticBody, wallFixture); 
    PhysicsFactory.createBoxBody(this.mPhysicsWorld, right, BodyType.StaticBody, wallFixture); 
    this.mScene.attachChild(roof); 
    this.mScene.attachChild(ground); 
    this.mScene.attachChild(left); 
    this.mScene.attachChild(right); 
    //facebox 
    facebox = new AnimatedSprite(150,150, this.mTiledTextureRegion); 
    facebox.setScale(.75f); 
    facebox.animate(200); 
    body = PhysicsFactory.createBoxBody(this.mPhysicsWorld, facebox, BodyType.DynamicBody, mFixtureDef); 
    this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(facebox,body,true,false)); 
    this.mScene.attachChild(facebox); 
    //enemy 
    enemy = new AnimatedSprite(500,150,this.enemyTiled); 
    enemy.animate(200); 
    enemy.setScale(.75f); 
    enemy.setFlippedHorizontal(pFlippedHorizontal); 
    this.mScene.attachChild(enemy); 
    enemybody = PhysicsFactory.createBoxBody(this.mPhysicsWorld, enemy, BodyType.DynamicBody, mFixtureDef); 
    this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(enemy,enemybody,true,false)); 
    //scene 
    this.mScene.setOnSceneTouchListener(this); 
    this.mScene.registerUpdateHandler(mPhysicsWorld); 
    return mScene; 

} 

@Override 
public void onLoadComplete() { 
    // TODO Auto-generated method stub 

} 

//touch the screen to create bullets 
@Override 
public boolean onSceneTouchEvent(Scene pScene, TouchEvent pSceneTouchEvent) { 
    // TODO Auto-generated method stub 
    if(pSceneTouchEvent.getAction() == TouchEvent.ACTION_DOWN){ 
     runOnUpdateThread(new Runnable(){ 

      @Override 
      public void run() { 
       // TODO Auto-generated method stub 
      fire(); 
      } 

     }); 

     //here I want to be able to remove the bullets when it hits the enemy but not sure what method to use 
     this.mScene.registerUpdateHandler(new IUpdateHandler(){ 

      @Override 
      public void onUpdate(float pSecondsElapsed) { 
       // TODO Auto-generated method stub 
       if(bulletsprite.collidesWith(enemy)){ 
       } 
      } 
      @Override 
      public void reset() { 
       // TODO Auto-generated method stub 
      } 
     }); 
    } 
    return false; 
} 
//method to create bullets 
public void fire(){ 
    bulletsprite = new Sprite(this.facebox.getX() + 15, this.facebox.getY() -5, this.mBulletTextureRegion); 
    bulletsprite.setScale(.5f); 
    bulletbody = PhysicsFactory.createCircleBody(this.mPhysicsWorld, bulletsprite, BodyType.DynamicBody, mFixtureDef); 
    this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(bulletsprite, bulletbody, true, true)); 
    final Vector2 speed = Vector2Pool.obtain(50, 0); 
    bulletbody.setLinearVelocity(speed); 
    Vector2Pool.recycle(speed); 
    this.mScene.attachChild(bulletsprite); 
} 

//nothing here just accelerometer 
@Override 
public void onAccelerometerChanged(AccelerometerData pAccelerometerData) { 
    // TODO Auto-generated method stub 
    final Vector2 gravity = Vector2Pool.obtain(pAccelerometerData.getX() *3, 10); 
    this.mPhysicsWorld.setGravity(gravity); 
    Vector2Pool.recycle(gravity); 
} 
@Override 
protected void onResume() { 
    // TODO Auto-generated method stub 
    super.onResume(); 
    this.enableAccelerometerSensor(this); 
} 
@Override 
protected void onPause() { 
    // TODO Auto-generated method stub 
    super.onPause(); 
    this.disableAccelerometerSensor(); 
} 

}

+0

我已經提供了一個答案,可以讓你在某個地方,但丹有一個問題 - 你的問題的答案是「實施碰撞檢測」,這不是可以簡潔地解釋 - 因此你不太可能得到一個代碼樣本作爲答案,除非你的問題更加狹隘和集中。 – jefflunt

+0

我知道提供更多信息,或者我是如何提問的? –

回答

1

首先,谷歌的碰撞檢測的代碼 - 這將幫助你解決第一個問題。其次,只保留1個子彈對象的實例,當它或者(a)與敵人或其他物體相撞,或者(b)離開屏幕時,則讓子彈能夠再次被射擊。

+0

我知道碰撞檢測。如果你看了我的代碼,我已經在那裏,並且還有關於它的評論。我不知道的是如何在碰撞發生時去除子彈。我知道只有兩種方法可以刪除精靈。 detachChild()和detachSelf(); –

+0

你可以在它上面設置一個布爾類型,叫做'inFlight',當'true'表示子彈正在飛行,並且還沒有和任何東西發生衝突時。當它與某物碰撞時,計算傷害,並將'inFlight'設置爲false。這表明子彈(a)不應該呈現在屏幕上,並且(b)它已準備好再次被觸發。您還需要重置其位置,並且在「inFlight」爲false時不會移動它。點擊屏幕會將'inFlight'更改爲true,只有當它已經是false時纔會觸發點火。 – jefflunt

+0

非常感謝。我解決了一次只能拍一顆子彈的問題,你知道我該如何徹底清除子彈?將其設置爲false並將其分離僅使其不可見。這就像我的遊戲中的玩家與一個看不見的物體相撞。 –