简体   繁体   English

AndEngine Physics Box2D-即使没有任何传感器更改,AnimatedSprite图像也会移动

[英]AndEngine Physics Box2D - the AnimatedSprite images moves even without any sensor change

I have an AnimatedSprite as a player, and I'm using IAccelerometerListener to move to the player to and fro. 我有一个AnimatedSprite作为播放器,并且正在使用IAccelerometerListener来回移动到播放器。 Problem is that even when the device is rested on table without any movement, my player moves upward or toward right. 问题是,即使将设备放在桌子上而没有任何移动,我的播放器也会向上或向右移动。

Can anyone point what I have missed. 谁能指出我错过的事情。 Below is my code. 下面是我的代码。

public Scene onLoadScene() {
.....
physicsWorld = new PhysicsWorld(new Vector2(0, SensorManager.GRAVITY_EARTH), false);


    /** for POTRAIT */
    final Display display = getWindowManager().getDefaultDisplay();
    int cameraWidth = display.getWidth();
    int cameraHeight = display.getHeight();
    final Shape ground = new Rectangle(0,cameraHeight, CAMERA_WIDTH,0);
    final Shape roof = new Rectangle(0,cameraHeight - (player.getHeight() * 2)  , CAMERA_WIDTH, player.getHeight() / 2);
    final Shape left = new Rectangle(0,0, 2,cameraHeight);
    final Shape right = new Rectangle(cameraWidth , 0, 2, cameraHeight);

    final FixtureDef wallFixtureDef = PhysicsFactory.createFixtureDef(0, 0, 1);
    PhysicsFactory.createBoxBody(this.physicsWorld, ground, BodyType.StaticBody, wallFixtureDef);
    PhysicsFactory.createBoxBody(this.physicsWorld, roof, BodyType.StaticBody, wallFixtureDef);
    PhysicsFactory.createBoxBody(this.physicsWorld, left, BodyType.StaticBody, wallFixtureDef);
    PhysicsFactory.createBoxBody(this.physicsWorld, right, BodyType.StaticBody, wallFixtureDef);

    this.mMainScene.attachChild(ground);
    this.mMainScene.attachChild(roof);
    this.mMainScene.attachChild(left);
    this.mMainScene.attachChild(right);

    mMainScene.registerUpdateHandler(physicsWorld);
    final Body grd = PhysicsFactory.createBoxBody(physicsWorld, player, BodyType.DynamicBody, FIXTURE_DEF);

    this.mMainScene.attachChild(player);
    physicsWorld.registerPhysicsConnector(new PhysicsConnector(player,grd, true, true));


}

You can set a threshold value and check in onAccelerationChanged method if the x and y values are greater than that threshold, then update your sprite otherwise ignore that update step. 您可以设置一个阈值,并检查x和y值是否大于该阈值的onAccelerationChanged方法,然后更新您的精灵,否则忽略该更新步骤。 Like: 喜欢:

@Override
    public void onAccelerationChanged(AccelerationData pAccelerationData) {
        // TODO Auto-generated method stub
        if(pAccelerationData.getX() > THRESHOLD_X || pAccelerationData.getY() > THRESHOLD_Y) {
          // update your sprite now...

        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM