使用PathModifier或MoveYModifier来模拟精灵跳跃
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
MoveYModifier
。请记住,您可以根据需要注册任意数量的修饰符。例如,如果游戏是平台游戏,并且角色始终在 X 轴上移动,并且如果他愿意,他可以跳跃(例如 Gravity Guy 或 Yoo Ninja,尽管这些游戏会改变重力,这是另一回事)。您可以这样做:
编辑:
对于第二个问题:
boolean mIsJumping
;当跳转开始时 - 将其设置为true
。如果用户点击屏幕并且mIsJumping == true
,则不跳跃。PhysicsWorld
。每当玩家与地面接触时,将mIsJumping
设置为false
。AndEngine 论坛中有很多使用
ContactListener
的示例,快速搜索会产生一些结果。如果您需要示例,可以索要一个:)编辑 2:
ContactListener
示例:private static Final String PLAYER_ID = "player", GROUND_ID = "ground";
playerBody.setUserData(PLAYER_ID);
和groundBody.setUserData(GROUND_ID);
创建一个
ContactListener
作为场景中的字段:< /p>最后,注册该接触侦听器:
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:
EDIT:
For your second question:
boolean mIsJumping
in your scene; When the jump starts - set it totrue
. If the user taps the screen andmIsJumping == true
, don't jump.PhysicsWorld
. Whenever there is contact between the player and the ground, setmIsJumping
tofalse
.There are many samples of using
ContactListener
s in AndEngine forums, a quick search yields some. If you need an example, you can ask for one :)EDIT 2:
ContactListener
sample:private static final String PLAYER_ID = "player", GROUND_ID = "ground";
playerBody.setUserData(PLAYER_ID);
andgroundBody.setUserData(GROUND_ID);
Create a
ContactListener
as a field in your scene: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 usingBody.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());
.