当用户点击屏幕时使精灵跳跃?

发布于 2024-12-26 14:42:35 字数 136 浏览 0 评论 0 原文

我正在使用 andengine 来实现一个精灵,可以使用它在屏幕上拖动。

所以我想做的是当用户点击屏幕上的任何位置时使精灵跳跃。

或向上移动然后向下移动。

使用 andengine 执行此操作的最佳方法是什么?

I am using andengine to implement a sprite being able to be dragged across the screen using this.

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?

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

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

发布评论

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

评论(2

青瓷清茶倾城歌 2025-01-02 14:42:35

这应该有效:

@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;
}

请记住,如果您要使用此功能,每当有 ACTION_DOWN 事件时,您可能会错过一些其他事件处理程序(例如,如果您有屏幕按钮,他们永远不会处理该事件)。

处理它的方法是添加您想要接收触摸事件的任何其他实体作为场景的触摸区域,Scene.registerTouchArea(ITouchArea) 就是这样做的。 SpriteAnimatedSprite 实现了 ITouchArea,因此您可以使用它们。

Scene 接收到要处理的 TouchEvent 时,它会首先让您注册的所有 ITouchArea 处理它,然后如果没有一个 ITouchArea 消耗它,它将尝试 onSceneTouchEvenet 方法。

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

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. Sprites and AnimatedSprites implement ITouchArea, so you can use these.

When a Scene receives a TouchEvent to handle, it will first let all the ITouchAreas you registered handle it, then if non of them consumed it, it will try the onSceneTouchEvenet method.

呆萌少年 2025-01-02 14:42:35

看起来您必须设置一个侦听器,在本教程中显示如何操作: http://www.andengine.org/forums/tutorials/updating-sprites-objects-listeners-t386.html 然后也许使用移动修改器,如本文所示:
如何使用 AndEngine (Android) 移动精灵对象
我希望你能解决你的问题。 ;)

祝你好运- Lijap

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

Good Luck- Lijap

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