使用PathModifier或MoveYModifier模拟精灵跳跃 [英] Using PathModifier or MoveYModifier to simulate sprite jumping

查看:134
本文介绍了使用PathModifier或MoveYModifier模拟精灵跳跃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用AndEngine此方法来确定现场用户被感动。

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.

现在两件事情这会是更好地使用PathModifier,或者MoveYModifier认为它是横向模式?
如果任一请提供的这样的例子。
谢谢

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

编辑:

香港专业教育学院设法利用物理以此来模拟跳..

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?

推荐答案

使用 MoveYModifier 。记住,你可以为你想要的修饰注册。因此,如果,例如,该游戏平台的游戏和字符始终上移动的X轴,并且如果他想要他可以jumpt(象重力盖伊或柳忍,虽然这些游戏更改重力这是其他内容)。

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).

您可以做这样的:

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. 创建一个变量布尔mIsJumping 在场景;当跳开始 - 设置为真正。如果用户点击屏幕和 mIsJumping ==真的跳跃。

  2. 现在,登记一个<一个href=\"http://$c$c.google.com/p/andenginephysicsbox2dextension/source/browse/src/com/badlogic/gdx/physics/box2d/ContactListener.java?r=1605f6e82f710ef9ebbe07632d6b055239d3b520\"相对=nofollow> ContactListener 您的 PhysicsWorld 。每当有玩家和地面,设置之间的接触 mIsJumping

  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.

有使用 ContactListener 的许多样品中AndEngine论坛,<一个href=\"http://www.andengine.org/forums/search.php?st=0&sk=t&sd=d&sr=posts&keywords=contactlistener&fid%5B%5D=14\"相对=nofollow>快速搜索给出了一些。如果你需要一个例子,你可以问一个:)

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 :)

编辑2: ContactListener 示例:


  1. 有2个变量来保存玩家和地面标识:私有静态最后弦乐PLAYER_ID =球员,GROUND_ID =地面;

  2. 当您创建地面体和球员的身体,设置其ID作为用户的数据: playerBody.setUserData(PLAYER_ID); groundBody .setUserData(GROUND_ID);

  3. 创建一个 ContactListener 作为场景的字段:

  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) { }
};


  • 最后,注册该联系人监听器: physicsWorld.setContactListener(mContactListener);

    修改3:

    要移动你的精灵在X轴,您可以使用 Body.applyForce 方法施加力,或使用适用的脉冲 Body.applyLinearImpulse 方法。玩的参数,寻找适合自己的下一个。

    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.

    矢量应该只由一个X部;尝试 Vector2力= Vector2Pool.obtain(50,0); 。然后应用力这样: body.applyForce(力,body.getWorldCenter());

    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());.

    这篇关于使用PathModifier或MoveYModifier模拟精灵跳跃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆