本文介绍了andengine精灵移动PathModifier和身体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用这个code

        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去的mplayer

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,我不明白。请如何做出正确的$ C $下mCatBody吗?

impossible to follow the body of chat_sale_1 and then nothing happens with mplayer and chat_sale_1, I do not understand. please how make a correct code for mCatBody please?

推荐答案

                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(){

in mScene.registerUpdateHandler(new 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);

享受!

这篇关于andengine精灵移动PathModifier和身体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-31 22:24