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

问题描述

我想移动精灵的身体随着线线的某一部分,只是我能只移动精灵,但身体不动。

 公共场景onLoadScene(){
    this.mEngine.registerUpdateHandler(新FPSLogger());
    最后一幕一幕=新场景(2);
    scene.setBackground(新ColorBackground(0.09804f,0.00274f,0.0784f));
    这.enableAccelerometerSensor(本);
    this.sPhysicsWorld =新PhysicsWorld(新Vector2(0,SensorManager.GRAVITY_EARTH),FALSE);
    最终形状地面=新的Rectangle(0,CAMERA_HEIGHT  -  2,CAMERA_WIDTH,2);
    最终形状的屋顶=新的Rectangle(0,0,CAMERA_WIDTH,2);
    最终形状左=新的Rectangle(0,0,2,CAMERA_HEIGHT);
    最终形状右=新的​​Rectangle(CAMERA_WIDTH-2,0,2,CAMERA_HEIGHT);


    PhysicsFactory.createBoxBody(this.sPhysicsWorld,地面,
            BodyType.StaticBody,wallFixtureDef);
    PhysicsFactory.createBoxBody(this.sPhysicsWorld,屋顶,
            BodyType.StaticBody,wallFixtureDef);
    PhysicsFactory.createBoxBody(this.sPhysicsWorld,左,
            BodyType.StaticBody,wallFixtureDef);
    PhysicsFactory.createBoxBody(this.sPhysicsWorld吧,
            BodyType.StaticBody,wallFixtureDef);
    scene.getFirstChild()attachChild(地)。
    scene.getFirstChild()attachChild(屋顶)。
    scene.getFirstChild()attachChild(左)。
    scene.getFirstChild()attachChild(右)。

    最终诠释的centerX =(CAMERA_WIDTH  -  this.mFaceTextureRegion.getWidth())/ 2;
    最终诠释centerY =(CAMERA_HEIGHT  -  this.mFaceTextureRegion.getHeight())/ 2;
    最后AnimatedSprite面=新AnimatedSprite(的centerX  -  100,centerY,this.mFaceTextureRegion);
    最后身体bodyRedBall = PhysicsFactory.createCircleBody(this.sPhysicsWorld,脸,
            BodyType.DynamicBody,wallFixtureDef);
    this.sPhysicsWorld.registerPhysicsConnector(新PhysicsConnector(面部,bodyRedBall,真,真));
     scene.attachChild(面);

     最后AnimatedSprite face2 =新AnimatedSprite(100,100,this.mFaceTextureRegion);
     最后身体bodyRedBall2 = PhysicsFactory.createCircleBody(this.sPhysicsWorld,face2,
            BodyType.KinematicBody,wallFixtureDef);
     最终路径path4 =新路径(3)。为(682,223)。为了(482,223)。为了(682,223);

     face2.registerEntityModifier(新LoopEntityModifier(新PathModifier(30,path4,空,新IPathModifierListener(){
            @覆盖
            公共无效onWaypointPassed(最终PathModifier pPathModifier,最终IEntity pEntity,最终诠释pWaypointIndex){
            }
        })));
     this.sPhysicsWorld.registerPhysicsConnector(新PhysicsConnector(face2,bodyRedBall2,真,真){
         @覆盖
         公共无效的OnUpdate(最终浮动pSecondsElapsed){
                 super.onUpdate(pSecondsElapsed);
                 face2.setPosition(face2.getX(),face2.getY());
         }
 });


      scene.attachChild(face2);
     scene.registerUpdateHandler(this.sPhysicsWorld);
  返回现场;
}

@覆盖
公共无效的onLoadComplete(){
}

@覆盖
公共无效onAccelerometerChanged(AccelerometerData pAccelerometerData){
    // TODO自动生成方法存根
     最终Vector2比重= Vector2Pool.obtain(pAccelerometerData.getY(),pAccelerometerData.getX());
     this.sPhysicsWorld.setGravity(重力);
     Vector2Pool.recycle(重力);
}
 

解决方案

您可以使用 body.setLinearVelocity(); 移动身体,精灵会自动跟随身体,因为你已经指定一个物理连接。

I want to move body of the sprite along with line in some portion of line, just i able to move sprite only but body is not moving.

public Scene onLoadScene() {
    this.mEngine.registerUpdateHandler(new FPSLogger());
    final Scene scene = new Scene(2);
    scene.setBackground(new ColorBackground(0.09804f, 0.00274f, 0.0784f));
    this .enableAccelerometerSensor(this );
    this.sPhysicsWorld = new PhysicsWorld(new Vector2(0, SensorManager.GRAVITY_EARTH), false);
    final Shape ground = new Rectangle(0, CAMERA_HEIGHT - 2, CAMERA_WIDTH,2);
    final Shape roof = new Rectangle(0, 0, CAMERA_WIDTH, 2);
    final Shape left = new Rectangle(0, 0, 2, CAMERA_HEIGHT);
    final Shape right = new Rectangle(CAMERA_WIDTH-2, 0,2, CAMERA_HEIGHT);


    PhysicsFactory.createBoxBody(this.sPhysicsWorld, ground,
            BodyType.StaticBody, wallFixtureDef);
    PhysicsFactory.createBoxBody(this.sPhysicsWorld, roof,
            BodyType.StaticBody, wallFixtureDef);
    PhysicsFactory.createBoxBody(this.sPhysicsWorld, left,
            BodyType.StaticBody, wallFixtureDef);
    PhysicsFactory.createBoxBody(this.sPhysicsWorld, right,
            BodyType.StaticBody, wallFixtureDef);
    scene.getFirstChild().attachChild(ground);
    scene.getFirstChild().attachChild(roof);
    scene.getFirstChild().attachChild(left);
    scene.getFirstChild().attachChild(right);

    final int centerX = (CAMERA_WIDTH - this.mFaceTextureRegion.getWidth()) / 2;
    final int centerY = (CAMERA_HEIGHT - this.mFaceTextureRegion.getHeight()) / 2;
    final AnimatedSprite face = new AnimatedSprite(centerX - 100, centerY, this.mFaceTextureRegion);
    final Body bodyRedBall = PhysicsFactory.createCircleBody(this.sPhysicsWorld, face,
            BodyType.DynamicBody, wallFixtureDef);
    this.sPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(face, bodyRedBall, true, true));
     scene.attachChild(face);

     final AnimatedSprite face2 = new AnimatedSprite(100, 100, this.mFaceTextureRegion);
     final Body bodyRedBall2 = PhysicsFactory.createCircleBody(this.sPhysicsWorld, face2,
            BodyType.KinematicBody, wallFixtureDef);
     final Path path4 = new Path(3).to(682, 223).to(482, 223).to(682, 223);

     face2.registerEntityModifier(new LoopEntityModifier(new PathModifier(30, path4, null, new IPathModifierListener() {
            @Override
            public void onWaypointPassed(final PathModifier pPathModifier, final IEntity pEntity, final int pWaypointIndex) {
            }
        })));
     this.sPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(face2, bodyRedBall2, true, true){
         @Override
         public void onUpdate(final float pSecondsElapsed) {
                 super.onUpdate(pSecondsElapsed);
                 face2.setPosition(face2.getX(), face2.getY());
         }
 });


      scene.attachChild(face2);
     scene.registerUpdateHandler(this.sPhysicsWorld);
  return scene;
}

@Override
public void onLoadComplete() {
}

@Override
public void onAccelerometerChanged(AccelerometerData pAccelerometerData) {
    // TODO Auto-generated method stub
     final Vector2 gravity = Vector2Pool.obtain(pAccelerometerData.getY(), pAccelerometerData.getX());
     this.sPhysicsWorld.setGravity(gravity);
     Vector2Pool.recycle(gravity);
}
解决方案

You can use body.setLinearVelocity(); to move the body, the sprite will automatically follow the body because you have specified a physics connector.

这篇关于如何使用andEngine移动精灵的身体吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 15:17