2012-03-04 26 views
0

所以我創建了一個android的遊戲,它使用設備的滾動來設置主要角色的位置。每次運行.onSensorChanged(event)方法時都會更新位置。問題是,即使我運行我的應用程序並將手機放在桌面上時,值也會發生顯着變化(大約兩度)。收集的數據的靈敏度似乎也不是很精確,因爲角度的差異似乎每增加約0.4度就會發生一次變化。使用訪問定向滾動數據是不穩定的Android

Log.e(TAG, "roll: " + event.values[2]); 

順序度輸出看起來是這樣的:

roll: 2.265 
roll: 2.265 
roll: 2.265 
roll: 1.843 
roll: 2.265 
roll: 2.265 
roll: 2.75 
roll: 2.265 

我也實現了這個算法來限制在屏幕我的性格的運動,但它是不夠的,嚴重限制了速度和響應能力角色的移動與當前的滾動數據有關(我希望角色的位置儘可能接近1-1)。

public void onSensorChanged(SensorEvent event) 
{ 
    if (event.sensor.getType() == Sensor.TYPE_ORIENTATION) 
    { 
    float newX = -(int)event.values[2] * CCDirector.sharedDirector().displaySize().width/10.0f + 
      CCDirector.sharedDirector().displaySize().width/2.0f; 

      sam.updatePosition(newX); 

    Log.e(TAG, "roll: " + event.values[2]); 
    } 
} 

public void updatePosition(float newX) 
{ 
    float winSizeX = CCDirector.sharedDirector().displaySize().width; 
    float contentWidth = this.sprite.getContentSize().width; 

    //tries to eliminate jumpy behaviour by the character by limiting his speed 
    double tolerance = 0.01; 
    if (Math.abs(newX - this.sprite.getPosition().x) > winSizeX * tolerance) 
     newX = this.sprite.getPosition().x - ((this.sprite.getPosition().x - newX) * 0.1f); 

    this.sprite.setPosition(newX, this.sprite.getPosition().y); 
} 

我想知道如果這是正常的,或者如果它是我測試它的手機(索尼的Xperia Play下架。)

感謝任何輸入。

+0

codez在哪裏? – Coffee 2012-03-04 06:02:32

+0

剛剛編輯成問題 – mknutso2 2012-03-04 20:58:16

回答

0

moving average如何讓您的數據在飛行中平滑?

它會稍微滯後一些,但不會引人注意。

另一方面,我不會使用Euler angles(如roll)。

+0

我已經納入了這個位置移動限制算法(現在包括在問題中),但是有太多的滯後。我沒有遇到來自您的鏈接的問題。我不希望任何人在極端的方向上使用該設備玩遊戲,因此不存在不規則角度行爲的問題。 – mknutso2 2012-03-04 21:02:20