如何抛出/使用AndEngine上刷卡扔球? [英] How to throw/fling the ball by swiping on it using AndEngine?

查看:89
本文介绍了如何抛出/使用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.

我想要做一些事情一样纸折腾

I wanted to do some thing like paper toss

任何一个可以帮助我。 先谢谢了。

Can any one help me out. Thanks in advance.

推荐答案

您需要重写雪碧的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.

更新:我添加了一些code来帮助你找出方向。在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天全站免登陆