按下按钮将身体从一个位置移动到另一个位置

时间:2013-08-17 16:17:15

标签: andengine

我有一个问题,即玩家释放动力攻击,这个动作攻击必须移动到某个位置,然后分离,我怎么能实现这个目标?我尝试使用setvelocity,但它没有那么好用...请帮助!!

好的,这是按下按钮时调用的攻击方法:

public void 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);  

}

当我运行此代码时,特殊攻击体移出屏幕......我想将攻击力限制在某个位置,即距离角色几个距离。

1 个答案:

答案 0 :(得分: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);

}