2013-08-17 165 views
0

我有一個問題,玩家釋放強力攻擊,這個攻擊必須移動到某個位置,然後才能分離,我該如何實現?我嘗試使用setvelocity,但它並沒有很好的工作...請幫助!按下按鈕,將身體從一個位置移動到另一個位置

確定這裏是我的攻擊方法是按下按鈕時,被稱爲:)

公共無效powerattack({

float startBulletX = player.getX() + 30; //Get X position of character body 
    float startBulletY = player.getY();  //Get Y position of character body 

    final Sprite bullet = new Sprite(startBulletX, startBulletY, 
      resourcesManager.special_attack, vbom); //The special attack sprite 

    final FixtureDef bulletFixtureDef1 = PhysicsFactory.createFixtureDef(0, 
      0, 0, false, CATEGORYBIT_KNIFE, MASKBIT_KNIFE, (short) 0); 
    this.mBulletBody = PhysicsFactory.createBoxBody(physicsWorld, bullet, 
      BodyType.DynamicBody, bulletFixtureDef1); 

    mBulletBody.setLinearVelocity(20f,0); 

    this.physicsWorld.registerPhysicsConnector(new PhysicsConnector(bullet, 
      this.mBulletBody, true, false)); 


    this.attachChild(bullet); 

} 

當我運行這段代碼的特殊攻擊體移出屏幕..我想限制強力攻擊到一定的位置,即距離角色很少的距離。

回答

0

我得到了解決,我用的onUpdate方法和方法的onupdate內我設定的代碼不可見的,這裏是我的代碼:)

public void powerattack() { 

    float startBulletX = player.getX() + 30; 
    float startBulletY = player.getY(); 

    final Sprite ray = new Sprite(startBulletX, startBulletY, 
      resourcesManager.special_attack, vbom); 
    final Vector2 velocity = Vector2Pool.obtain(20f, 0); 
    final FixtureDef rayFixtureDef1 = PhysicsFactory.createFixtureDef(0, 0, 
      0, false, CATEGORYBIT_RAY, MASKBIT_RAY, (short) 0); 
    this.mRayBody = PhysicsFactory.createBoxBody(physicsWorld, ray, 
      BodyType.DynamicBody, rayFixtureDef1); 

    this.mRayBody.setLinearVelocity(velocity); 
    Vector2Pool.recycle(velocity); 

    this.physicsWorld.registerPhysicsConnector(new PhysicsConnector(ray, 
      this.mRayBody, true, false) { 
     /*Update method keeps checking whether the power attack reached the specific distance*/ 
     @Override 
     public void onUpdate(float pSecondsElapsed) { 

      if (ray.getX() >= (player.getX() + 300)) { 

       ray.setVisible(false); 
       ray.setIgnoreUpdate(false); 

      } 
      super.onUpdate(pSecondsElapsed); 
      camera.onUpdate(0.1f); 
     } 
    }); 

    mRayBody.setUserData("power"); 
    this.attachChild(ray); 

} 
相關問題