Android 振动崩溃
我想为我正在开发的游戏添加振动反馈,以及音乐播放,例如“获胜者”和振动模式。
问题是,当我添加振动句子时,我的应用程序崩溃了。
例如:
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,所以不是问题。事实上,我在菜单上得到了振动。
I want to add a vibration feedback for a game i'm developing, together a music play, like "winner" and a vibrate pattern.
The problem is that when I add the vibrate sentences, my app crashes.
For example:
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);
}
}
}
}
It is not inside an activity, so I can't implement something like this Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE)
, because it is an activity statement. I tried to declare public Vibrator vib;
and then implement the code I showed above, but when the game lost is recived the application crashes.
I also tried to do it via "notification", but the same result, app crashes.
Any idea how can I implement vibrate on it?
Thanks!!
P.S.: I have android.permission.VIBRATE, so is not the problem. In fact I got virbation working on menu.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我在调用游戏对象的活动中声明一个公共振动器对象。在我的例子中,onCreate 活动启动了一个扩展 SurfaceView 的 MainGamePanel。因此,在 onCreate 活动中,我声明了振动器对象并使用它进行初始化
希望这会有所帮助!
I declare a public vibrator object in the activity that calls the game object. In my case the onCreate activity launches a MainGamePanel that extends a surfaceView. So in the onCreate activity I declare the vibrator object and initialize it with
Hope this helps!