PhysicsHandler中Scene,Velocity属性的位置是什么?

时间:2014-04-22 04:59:50

标签: android andengine game-physics scene

我是使用Andengine框架进行Android编程的初学者。我在互联网上学习教程。我不知道我误解了PhysicsHandler中Scence和Velocity属性的根位置。

下面: enter image description here

如果我的意见是真的。那么,如何设置PhysicsHandler的Velocity属性来跟随Scene的位置?

这是我的例子: enter image description here

我的代码:

scene.setOnSceneTouchListener(new IOnSceneTouchListener() {
            @Override
            public boolean onSceneTouchEvent(Scene pScene,
                    TouchEvent pSceneTouchEvent) {
                if (pSceneTouchEvent.getAction() == TouchEvent.ACTION_UP) {
                    positionUpX = pSceneTouchEvent.getX();
                    positionUpY = pSceneTouchEvent.getY();

                    ball = new Ball(positionDownX, positionDownY, positionUpX,
                            positionUpY, mFaceTextureRegion,
                            getVertexBufferObjectManager());
                    scene.attachChild(ball);
                    return true;
                }
                if (pSceneTouchEvent.getAction() == TouchEvent.ACTION_DOWN) {
                    positionDownX = pSceneTouchEvent.getX();
                    positionDownY = pSceneTouchEvent.getY();

                    return true;
                }
                return false;
            }
        });




private static class Ball extends AnimatedSprite {
        private final PhysicsHandler mPhysicsHandler;

        public Ball(final float pDownX, final float pDownY, //Start 
                final float pUpX,final float pUpY, //End
                final TiledTextureRegion pTextureRegion,
                final VertexBufferObjectManager pVertexBufferObjectManager) {
            super(pDownX, pDownY, pTextureRegion, pVertexBufferObjectManager);
            this.mPhysicsHandler = new PhysicsHandler(this);
            this.registerUpdateHandler(this.mPhysicsHandler);
            this.mPhysicsHandler.setVelocity(pUpX, pUpY);
        }

        @Override
        protected void onManagedUpdate(final float pSecondsElapsed) {
            if (this.mX < 0) {
                this.mPhysicsHandler.setVelocityX(BALL_VELOCITY);
            } else if (this.mX + this.getWidth() > CAMERA_WIDTH) {
                this.mPhysicsHandler.setVelocityX(-BALL_VELOCITY);
            }
            if (this.mY < 0) {
                this.mPhysicsHandler.setVelocityY(BALL_VELOCITY);
            } else if (this.mY + this.getHeight() > CAMERA_HEIGHT) {
                this.mPhysicsHandler.setVelocityY(-BALL_VELOCITY);
            }
            super.onManagedUpdate(pSecondsElapsed);
        }
    }

1 个答案:

答案 0 :(得分:0)

我找到了Physics Box2DMouse Joint

的答案
相关问题