1

我想为我正在开发的游戏添加振动反馈,一起播放音乐,如“赢家”和振动模式。

问题是当我添加振动句子时,我的应用程序崩溃了。

例如:

 private void Down()
  {
    soundM.playSound(Sound.SOUND_NEWINTENT);

    for (int i=0 ; i<8 ; i++) {
      for (int j=0 ; j<12 ; j++) {
        if (Play[i][j] != null) {
          Play[i][j].moveDown();

          if (Play[i][j].getSpritePosition().y>=380) {
            Sprite.updateState(Sprite.STATE_GAME_LOST);
            endOfGame = true;

            soundM.playSound(Sound.SOUND_LOST);
            vib = (Vibrator)getSystemService(Context.VIBRATOR_SERVICE); 
            vib.vibrate(500); 

          }
        }
      }
    }

它不在活动内部,所以我无法实现这样的东西Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE),因为它是一个活动声明。我试图声明 public Vibrator vib;然后实现上面显示的代码,但是当游戏丢失时,应用程序崩溃了。

我也尝试通过“通知”来做到这一点,但同样的结果,应用程序崩溃了。

知道如何在其上实现振动吗?

谢谢!!

PS:我有android.permission.VIBRATE,所以不是问题。事实上,我在菜单上得到了振动。

4

1 回答 1

1

我在调用游戏对象的活动中声明了一个公共振动器对象。在我的例子中,onCreate 活动启动了一个扩展 SurfaceView 的 MainGamePanel。所以在 onCreate 活动中我声明了振动器对象并用它初始化它

public static Vibrator v;

v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);

//Then when I want to game to vibrate, inside of MainGamePanel I call

long[] pattern = { 0, 200, 500 };

DroidzActivity.v.vibrate(pattern, 0);

希望这可以帮助!

于 2011-07-12T16:24:20.447 回答