2013-02-06 21 views
1

我有一個AnimatedSprite作爲播放器,我使用IAccelerometerListener來回移動播放器。 問題是,即使設備擱置在桌面上而沒有任何移動,我的播放器也會向上或向右移動。AndEngine Physics Box2D - 即使沒有任何傳感器變化,AnimatedSprite圖像也會移動

任何人都可以指出我錯過了什麼。以下是我的代碼。

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)); 


} 

回答

0

你可以設置一個閾值,併爲您在onAccelerationChanged方法,如果x和y的值都小於閾值,然後更新您的精靈,否則忽略更新步驟。 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... 

     } 
    } 
相關問題