使用PathModifier或MoveYModifier来模拟精灵跳跃

发布于 2024-12-26 05:13:34 字数 1029 浏览 0 评论 0原文

我在 AndEngine 中使用这个方法来确定用户触摸的场景。

@Override
public boolean onSceneTouchEvent(Scene pScene, TouchEvent pSceneTouchEvent) {
     if(pSceneTouchEvent.isActionDown()) {
         Log.e("Arcade", "Scene Tapped");
                   //Simulate player jumping

     }
    return false;
}

我想做的是当场景被点击时,我想让玩家跳跃。

现在有两件事是考虑到它是横向模式,使用 PathModifier 或 MoveYModifier 会更好吗? 如果有的话,请提供一个这样的例子。 谢谢

编辑:

我设法使用物理来模拟跳跃使用这个..

@Override
public boolean onSceneTouchEvent(Scene pScene, TouchEvent pSceneTouchEvent) {
     if(pSceneTouchEvent.isActionDown()) {
         Log.e("Arcade", "Scene Tapped");

          final Vector2 velocity = Vector2Pool.obtain(mPhysicsWorld.getGravity().x * -0.5f,mPhysicsWorld.getGravity().y * -0.5f);
          body.setLinearVelocity(velocity);
          Vector2Pool.recycle(velocity);
         return true;
     }
    return false;
}

正如你在答案中所说的通过改变重力。唯一的问题是,当用户不断触摸屏幕时,精灵会不断向上、向上、向上。我如何将其设置为用户只能单击一次并且不能让他再次跳跃,直到精灵击中地面(这是一个矩形)?

I am using this method in AndEngine to determine the scene being touched by the user.

@Override
public boolean onSceneTouchEvent(Scene pScene, TouchEvent pSceneTouchEvent) {
     if(pSceneTouchEvent.isActionDown()) {
         Log.e("Arcade", "Scene Tapped");
                   //Simulate player jumping

     }
    return false;
}

What i want to do is when the scene is tapped, i want to allow the player to jump.

Now two things for this would it be better to use PathModifier, or MoveYModifier considering it is landscape mode?
If either please provide an example of such.
Thanks

EDIT:

ive managed to use Physics to simulate a jump using this..

@Override
public boolean onSceneTouchEvent(Scene pScene, TouchEvent pSceneTouchEvent) {
     if(pSceneTouchEvent.isActionDown()) {
         Log.e("Arcade", "Scene Tapped");

          final Vector2 velocity = Vector2Pool.obtain(mPhysicsWorld.getGravity().x * -0.5f,mPhysicsWorld.getGravity().y * -0.5f);
          body.setLinearVelocity(velocity);
          Vector2Pool.recycle(velocity);
         return true;
     }
    return false;
}

As you said in the answer by changing the gravity. The only issue is, when the user keeps touching the screen the sprites keep going up and up and up. How can i set it where the user can only click once and cant make him jump again until the sprite hits the ground, which is a rectangle?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心是晴朗的。 2025-01-02 05:13:34

使用MoveYModifier。请记住,您可以根据需要注册任意数量的修饰符。例如,如果游戏是平台游戏,并且角色始终在 X 轴上移动,并且如果他愿意,他可以跳跃(例如 Gravity Guy 或 Yoo Ninja,尽管这些游戏会改变重力,这是另一回事)。

您可以这样做:

Entity playerEntity = ..//It doesn't matter if the player is a sprite, animated sprite, or anything else. So I'll just use Entity here, but you can declare your player as you wish.

final float jumpDuration = 2;
final float startX = playerEntity.getX();
final float jumpHeight = 100;

final MoveYModifier moveUpModifier = new MoveYModifier(jumpDuration / 2, startX, startX + jumpHeight);
final MoveYModifier moveDownModifier = new MoveYModifier(jumpDuration / 2, startX + jumpHeight, startX);
final SequenceEntityModifier modifier = new SequenceEntityModifier(moveUpModifier, moveDownModifier);

playerEntity.registerEntityModifier(modifier);

编辑:

对于第二个问题:

  1. 在场景中创建一个变量boolean mIsJumping;当跳转开始时 - 将其设置为true。如果用户点击屏幕并且mIsJumping == true,则跳跃。
  2. 现在,注册一个 联系监听器到您的PhysicsWorld。每当玩家与地面接触时,将 mIsJumping 设置为 false

AndEngine 论坛中有很多使用 ContactListener 的示例,快速搜索会产生一些结果。如果您需要示例,可以索要一个:)

编辑 2: ContactListener 示例:

  1. 有 2 个变量来保存玩家和地面的 ID:private static Final String PLAYER_ID = "player", GROUND_ID = "ground";
  2. 创建地面体和玩家体时,将它们的 ID 设置为用户数据: playerBody.setUserData(PLAYER_ID);groundBody.setUserData(GROUND_ID);
  3. 创建一个 ContactListener 作为场景中的字段:< /p>

    private ContactListener mContactListener = new ContactListener() { 
    /**
     * 当两个灯具开始接触时调用。
     */
    公共无效beginContact(联系联系){
        最终主体 bodyA = contact.getFixtureA().getBody();
        最终主体 bodyB = contact.getFixtureB().getBody();
    
        if(bodyA.getUserData().equals(PLAYER_ID)) {
            if(bodyB.getUserData().equals(GROUND_ID))
                mIsJumping = false;
        }
        否则 if(bodyA.getUserData().equals(GROUND_ID)) {
            if(bodyB.getUserData().equals(PLAYER_ID))
                mIsJumping = false;
        }
    
    }
    
    /**
     * 当两个灯具停止接​​触时调用。
     */
    公共无效endContact(联系联系人){}
    
    /**
     * 更新联系人后调用此方法。
     */
    公共无效preSolve(联系pContact){}
    
    /**
     * 这使您可以在解算器完成后检查接触。 
     */
    公共无效postSolve(联系pContact){}
    };
    
  4. 最后,注册该接触侦听器:physicalsWorld.setContactListener(mContactListener);< /p>

编辑 3:

要移动您的精灵在 X 轴上,您可以使用 Body.applyForce 方法施加力,或使用 Body.applyLinearImpulse 方法施加脉冲。尝试一下这些论点并找到下一步可行的方法。

该向量应仅包含 X 部分;尝试 Vector2 force = Vector2Pool.obtain(50, 0);。然后通过以下方式施加力:body.applyForce(force, body.getWorldCenter());

Use the MoveYModifier. remember, you can register as many modifiers as you want. So if, for example, the game a platform game and the character always moves on the X axis, and he can jumpt if he wants (Like Gravity Guy or Yoo Ninja, although these games change the gravity which is something else).

You could do like:

Entity playerEntity = ..//It doesn't matter if the player is a sprite, animated sprite, or anything else. So I'll just use Entity here, but you can declare your player as you wish.

final float jumpDuration = 2;
final float startX = playerEntity.getX();
final float jumpHeight = 100;

final MoveYModifier moveUpModifier = new MoveYModifier(jumpDuration / 2, startX, startX + jumpHeight);
final MoveYModifier moveDownModifier = new MoveYModifier(jumpDuration / 2, startX + jumpHeight, startX);
final SequenceEntityModifier modifier = new SequenceEntityModifier(moveUpModifier, moveDownModifier);

playerEntity.registerEntityModifier(modifier);

EDIT:

For your second question:

  1. Create a variable boolean mIsJumping in your scene; When the jump starts - set it to true. If the user taps the screen and mIsJumping == true, don't jump.
  2. Now, register a ContactListener to your PhysicsWorld. Whenever there is contact between the player and the ground, set mIsJumping to false.

There are many samples of using ContactListeners in AndEngine forums, a quick search yields some. If you need an example, you can ask for one :)

EDIT 2: ContactListener sample:

  1. Have 2 variables to hold IDs for the player and the ground: private static final String PLAYER_ID = "player", GROUND_ID = "ground";
  2. When you create the ground body and the player body, set their IDs as the user data: playerBody.setUserData(PLAYER_ID); and groundBody.setUserData(GROUND_ID);
  3. Create a ContactListener as a field in your scene:

    private ContactListener mContactListener = new ContactListener() { 
    /**
     * Called when two fixtures begin to touch.
     */
    public void beginContact (Contact contact) {
        final Body bodyA = contact.getFixtureA().getBody();
        final Body bodyB = contact.getFixtureB().getBody();
    
        if(bodyA.getUserData().equals(PLAYER_ID)) {
            if(bodyB.getUserData().equals(GROUND_ID))
                mIsJumping = false;
        }
        else if(bodyA.getUserData().equals(GROUND_ID)) {
            if(bodyB.getUserData().equals(PLAYER_ID))
                mIsJumping = false;
        }
    
    }
    
    /**
     * Called when two fixtures cease to touch.
     */
    public void endContact (Contact contact) { }
    
    /**
     * This is called after a contact is updated.
     */
    public void preSolve(Contact pContact) { }
    
    /**
     * This lets you inspect a contact after the solver is finished. 
     */
    public void postSolve(Contact pContact) { }
    };
    
  4. Lastly, register that contact listener: physicsWorld.setContactListener(mContactListener);

EDIT 3:

To move your sprite over the X axis, you can apply a force using Body.applyForce method, or apply an impulse using Body.applyLinearImpulse method. Play around with the arguments and find what works the next.

The vector should consist a X part only; Try Vector2 force = Vector2Pool.obtain(50, 0);. Then apply the force this way: body.applyForce(force, body.getWorldCenter());.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文