【问题标题】:Android accelerometer only moving to right and down (positive directions)Android 加速度计仅向右和向下移动(正方向)
【发布时间】:2018-07-28 04:18:27
【问题描述】:

我刚刚写了几行代码,试图在加速度计倾斜的方向上移动一个圆圈。

当我向右倾斜时,圆圈向右移动。 当我向下倾斜时,圆圈会向下移动。

但是现在,当我希望它上升时,它反而下降,当我希望它向左移动时,它就向右移动。

光标有两个坐标,x 和 y。 当它更新时,它会计算

x += mx;
y += my;

mx 和 my 来自 onSensorChanged 方法。

public void onSensorChanged(SensorEvent e) {
    if(e.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
        gameView.setCursorX((int)Math.pow(e.values[1], 2));
        gameView.setCursorY((int)Math.pow(e.values[0], 2));
    }
}

现在我的问题是:我怎样才能获得负面运动?好像我的 onSensorChanged 方法只收集正面方向? 有什么想法吗?

提前致谢!

【问题讨论】:

  • 每个数字在平方时都会变成正数。
  • 在发布这个问题几分钟后看到,我自己已经回答了:)

标签: android accelerometer


【解决方案1】:

好吧,伙计们,抱歉这个愚蠢的问题......在处理了我注意到的问题后,我对结果进行了平方,这意味着只有积极的结果......

  public void onSensorChanged(SensorEvent e) {
    if(e.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
        gameView.setCursorX((int)e.values[1]);
        gameView.setCursorY((int)e.values[0]);
    }
}

这是我现在的工作代码...抱歉给您带来不便!

【讨论】:

  • 你解决了吗?我的意思是当设备向下移动时被解雇?
猜你喜欢
  • 1970-01-01
  • 2011-10-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多