简体   繁体   English

当用户点击屏幕时,用精灵跳转?

[英]Making a sprite jump when user taps on the screen?

I am using andengine to implement a sprite being able to be dragged across the screen using this. 我正在使用andengine来实现一个能够使用它在屏幕上拖动的精灵。

So what i want to do is when the user taps any where on the screen make the sprite jump. 所以我想要做的是当用户点击屏幕上的任何地方使精灵跳跃。

or move up and then move down. 或向上移动然后向下移动。

What is the best way to go bout doing this with andengine? 用发动机做这件事的最好方法是什么?

This should work: 这应该工作:

@Override
public boolean onSceneTouchEvent(Scene pScene, TouchEvent pSceneTouchEvent) {
    if(pSceneTouchEvent.isActionDown()) { //Jump only if the user tapped, not moved his finger or something
        final Entity playerEntity = ...;//Get player entity here.

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

        final MoveYModifier moveUpModifier = new MoveYModifier(jumpDuration / 2, startX, startX - jumpHeight); // - since we want the sprite to go up.
        final MoveYModifier moveDownModifier = new MoveYModifier(jumpDuration / 2, startX + jumpHeight, startX);
        final SequenceEntityModifier modifier = new SequenceEntityModifier(moveUpModifier, moveDownModifier);

        playerEntity.registerEntityModifier(modifier);
        return true;
    }
    return false;
}

Keep in mind that, if you will use this, whenever there is an ACTION_DOWN event, you might miss some other event handlers for this (For example, if you have on-screen buttons, they will never handle the event). 请记住,如果您将使用此功能,则只要有ACTION_DOWN事件,您可能会错过其他一些事件处理程序(例如,如果您有屏幕上的按钮,它们将永远不会处理该事件)。

The way to handle it is to use add any other entities you want to receive touch events as touch areas for your scene, Scene.registerTouchArea(ITouchArea) does so. 处理它的方法是使用添加任何其他想要接收触摸事件的实体作为场景的触摸区域, Scene.registerTouchArea(ITouchArea)这样做。 Sprite s and AnimatedSprite s implement ITouchArea , so you can use these. SpriteAnimatedSprite实现了ITouchArea ,因此您可以使用它们。

When a Scene receives a TouchEvent to handle, it will first let all the ITouchArea s you registered handle it, then if non of them consumed it, it will try the onSceneTouchEvenet method. 当一个Scene接收到要处理的TouchEvent时,它将首先让你注册的所有ITouchArea处理它,然后如果没有它们消耗它,它将尝试onSceneTouchEvenet方法。

It looks like you would have to set up a listener shown how in this tutorial: http://www.andengine.org/forums/tutorials/updating-sprites-objects-listeners-t386.html then perhaps use a move modifier as shown in this post: how to move sprite object using AndEngine (Android) I hope that you will figure out your problem. 看起来您必须设置一个监听器,在本教程中如何显示: http//www.andengine.org/forums/tutorials/updating-sprites-objects-listeners-t386.html然后可能使用移动修改器,如图所示在这篇文章中: 如何使用AndEngine(Android)移动精灵对象我希望你能找出你的问题。 ;) ;)

Good Luck- Lijap 祝你好运 - Lijap

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM