• Advertisement
Sign in to follow this  

Android Shake Method

This topic is 1984 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

In my game I want the player to shake his phone, at any point during the game, and every shake will result in switching weapons.
Example: Player has knife, [shakes phone] and switches to a katana.

[CODE]
if (accelerometer.x >= 5 || accelerometer.x <= -5
|| accelerometer.y >= 5 || accelerometer.y <= -5
|| accelerometer.z >= 5 || accelerometer.z <= -5 )
switchWep();
[/CODE]

This works, the problem is it has a[color=#FF0000] [b]side effect.[/b][/color]When the player shakes the phone sometimes it switches weapons twice. So I want to limit it so that if there is a big shake the game doesn't switch from weapon 0 to weapon 2.

Please help.

Share this post


Link to post
Share on other sites
Advertisement
I guess the problem is that he shakes for too long, and the phone registers the shake as a new action, thus your code is called again.

The simplest workaround would be that the switchWep(); uses timestamps when the weapon was switched the last time. When the timestamps have a smaller than for ex. 0.2 seconds difference, switchWep() does nothing.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement