如何使用 AndEngine 通过在球上滑动来投掷/投掷球? [英] How to throw/fling the ball by swiping on it using AndEngine?

查看:22
本文介绍了如何使用 AndEngine 通过在球上滑动来投掷/投掷球?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在屏幕上有一个球精灵,当我在该精灵上触摸和滑动时,它必须向特定的滑动方向移动.

I'm having a ball sprite on screen,when i touch and swipe on that sprite,It must move in particular direction of swipe.

我为那个球添加了物理.

I have added a physics to that ball.

我想做一些事情,比如paper折腾

谁能帮帮我.提前致谢.

Can any one help me out. Thanks in advance.

推荐答案

需要重写Sprite的onAreaTouched方法,如下图.您可以从 pSceneTouchEvent、pTouchAreasLocalX 和 pTouchAreaLocalY 变量中获取有关触摸事件的信息,并使用它们来确定移动球的方向.

You need to override the onAreaTouched method of Sprite as shown below. You can get the information on the touch event from your pSceneTouchEvent, pTouchAreasLocalX, and pTouchAreaLocalY variables and use them to determine which way to move the ball.

您不想在 OnAreaTouched 方法内部对物理体施加任何力,因为应该使用更新处理程序对物理体进行更改.我建议让 onAreaTouched 方法设置一个标志和一些其他变量,以便下次更新处理程序运行时可以使用这些值.

You don't want to apply any forces to the physics body inside of the OnAreaTouched method though because changes to physics bodies should be made using an update handler. I would suggest having the onAreaTouched method set a flag and some other variables so that the next time the update handler runs it can use those values.

更新:我添加了一些代码来帮助您确定方向.if 语句中的注释应该解释它们何时被调用.基本上,您会获得初始触摸位置(动作向下),计算您移动到的位置(动作移动)并使用方向在更新处理程序中施加力(动作向上).

Update: I added some code to help you figure out the direction. The comments inside the if statements should explain when they are called. Basically, you get the initial touch position (action down), calculate where you moved to (action move) and use the direction to apply the force in the update handler (action up).

mSprite = new Sprite(x, y ,mRegion, mEngine.getVertexBufferObjectManager()){
        @Override
        public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY) 
        {

            //set flag member variable that sprite has been touched
            if (pSceneTouchEvent.isActionDown())
            {
                //here is where the touch was initiated so you 
                //can store the x,y location. You obtain it by using pSceneTouchEvent.getX()
                // and pSceneTouchEvent.getY()
            }

            if (pSceneTouchEvent.isActionMove())
            {
               //This will be called when you slide your finger, so you
               //can get the new coordinates by again using pSceneTouchEvent.getX()
               // and pSceneTouchEvent.getY()

            }

            if (sSceneTouchEvent.isActionUp())
            {
               //this will be called when you release the sprite
               // and tell the update handler to apply the force
            }
        }
    };


this.registerUpdateHandler(new IUpdateHandler(){
        @Override  
        public void onUpdate(float pSecondsElapsed) {
            //if flag is set apply a force to the physics body
            //set flag to false to wait for next touch event
        }

        @Override
        public void reset() {

      }

这篇关于如何使用 AndEngine 通过在球上滑动来投掷/投掷球?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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