andengine sprite移动PathModifier和body

时间:2013-04-16 14:17:05

标签: android andengine move sprite

我使用此代码

        this.mPhysicsWorld = new FixedStepPhysicsWorld(30, new Vector2(0, 0), false, 8, 1);
    this.mScene.registerUpdateHandler(this.mPhysicsWorld);      

     final AnimatedSprite chat_sale_1 = new AnimatedSprite(2157, 638, this.mchatSaleTextureRegion);     

 //final PhysicsHandler physicsHandler2 = new PhysicsHandler(chat_sale_1);
//chat_sale_1.registerUpdateHandler(physicsHandler2);

            final FixtureDef chat_sale_1FixtureDef = PhysicsFactory.createFixtureDef(0, 0, 0f);
            mCatBody = PhysicsFactory.createBoxBody(this.mPhysicsWorld, chat_sale_1, BodyType.DynamicBody, chat_sale_1FixtureDef);
            //mCatBody.setColor(1, 0, 0, 0.25f);                


            final Path path = new Path(5).to(2157, 638).to( 2775, 638).to( 2775, 610).to( 2157, 610).to( 2157, 638);
            //207

            chat_sale_1.registerEntityModifier(
                new LoopEntityModifier(
 new PathModifier(35, path, null, new IPathModifierListener() {

                @Override
                public void onPathStarted(final PathModifier pPathModifier, final IEntity pEntity) {
                    //Debug.d("onPathStarted");
                }

                @Override
                public void onPathWaypointStarted(final PathModifier pPathModifier, final IEntity pEntity, final int pWaypointIndex) {
                    switch(pWaypointIndex) {
                    case 0:         
                        chat_sale_1.animate(new long[]{200, 200, 200}, 9, 11, true);                            
                        break;
                    case 1:
                        chat_sale_1.animate(new long[]{200, 200, 200}, 13, 15, true);
                        break;
                    case 2:
                        chat_sale_1.animate(new long[]{200, 200, 200}, 5, 7, true);
                        break;
                    case 3:
                        chat_sale_1.animate(new long[]{200, 200, 200},1, 3, true);
                        break;
                }
                    //Debug.d("onPathWaypointStarted:  " + pWaypointIndex);
                }

                @Override
                public void onPathWaypointFinished(final PathModifier pPathModifier, final IEntity pEntity, final int pWaypointIndex) {
                    //Debug.d("onPathWaypointFinished: " + pWaypointIndex);
                    //player.stopAnimation();
                }

                @Override
                public void onPathFinished(final PathModifier pPathModifier, final IEntity pEntity) {
                    //Debug.d("onPathFinished");
                    chat_sale_1.stopAnimation();                        
                }
            },EaseLinear.getInstance()))
            );

            this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(chat_sale_1, mCatBody, true, true){
                @Override
                public void onUpdate(float pSecondsElapsed){
                    super.onUpdate(pSecondsElapsed);
                    //Log.d(tag, "onUpdate");
                    //chat_sale_1.setPosition(mCamera.getMinX()+chat_sale_1.getX(), mCamera.getMinY()+chat_sale_1.getY());
            //mCatBody.setLinearVelocity(chat_sale_1.getX(),chat_sale_1.getY());
                    //mCatBody.setLinearVelocity(new Vector2(0,0));
                    //mCatBody.setTransform(new Vector2(mCamera.getMinX()+chat_sale_1.getX()/32,mCamera.getMinY()+chat_sale_1.getY()/32), 0);

                    //mCatBody.setLinearVelocity(new Vector2(mCamera.getMinX()+chat_sale_1.getX(),mCamera.getMinY()+chat_sale_1.getY()));
      //final Vector2 velocity2 = Vector2Pool.obtain(chat_sale_1.getX(), chat_sale_1.getY());
    //mCatBody.setLinearVelocity(velocity2);
     //Vector2Pool.recycle(velocity2);  
      mCatBody.setTransform(chat_sale_1.getX()/32,chat_sale_1.getY()/32, 0);
    //mCatBody.setTransform(new Vector2(chat_sale_1.getX()/mPixelToMeterRatio,chat_sale_1.getY()/mPixelToMeterRatio), 0); 
//final Vector2 velocity2 = Vector2Pool.obtain(mCamera.getMinX()+chat_sale_1.getX(), mCamera.getMinY()+chat_sale_1.getY());

     //final Vector2 velocity2 = Vector2Pool.obtain(chat_sale_1.getX(), chat_sale_1.getY());
     //mCatBody.setLinearVelocity(velocity2);
     //Vector2Pool.recycle(velocity2);  
//mCatBody.setTransform(new Vector2(chat_sale_1.getX()/32,chat_sale_1.getY()/32), 0); 
                }
            });

            mScene.attachChild(chat_sale_1);    

Code de mplayer

mplayer = new Saiyan(playerX, playerY, this.textureRegionHashMap.get("hero5")); 
    //this.mCamera.setChaseEntity(mplayer); 

    physicsHandler = new PhysicsHandler(mplayer);
    mplayer.registerUpdateHandler(physicsHandler);

    final FixtureDef playerFixtureDef = PhysicsFactory.createFixtureDef(0, 0, 0f);
    mPlayerBody = PhysicsFactory.createBoxBody(this.mPhysicsWorld, mplayer, BodyType.DynamicBody, playerFixtureDef);

    this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(mplayer, mPlayerBody, true, false){
        @Override
        public void onUpdate(float pSecondsElapsed){
            super.onUpdate(pSecondsElapsed);
            mCamera.updateChaseEntity();
        }
    });

    mCamera.setChaseEntity(mplayer);

    mScene.attachChild(mplayer);

无法跟随chat_sale_1的主体,然后mplayer和chat_sale_1没有任何反应,我不明白。请问如何为mCatBody制作正确的代码?

1 个答案:

答案 0 :(得分:1)

解决方案

                final FixtureDef chat_sale_1FixtureDef = PhysicsFactory.createFixtureDef(0, 0, 0f);
            mCatBody = PhysicsFactory.createBoxBody(this.mPhysicsWorld, 8,1, 16,1, 0, BodyType.KinematicBody, chat_sale_1FixtureDef);

在mScene.registerUpdateHandler中(新的IUpdateHandler(){

                        Vector2 localPoint = Vector2Pool.obtain(((chat_sale_1.getX()+(chat_sale_1.getWidth()/2))/PhysicsConstants.PIXEL_TO_METER_RATIO_DEFAULT), ((chat_sale_1.getY()+chat_sale_1.getHeight()-8)/PhysicsConstants.PIXEL_TO_METER_RATIO_DEFAULT));
                    mCatBody.setTransform(localPoint, 0);
                    Vector2Pool.recycle(localPoint);

享受!!!