Android - Play a sound on achievement unlocked

倾然丶 夕夏残阳落幕 提交于 2019-12-21 03:03:09

问题


I have an Android game and I'd like to play a sound whenever a players unlocks an achievement, but I can't seem to find a way to know when the user unlocks the achievement, since most of my achievements are incremental I have no way to code it on the client side, the only way would be some callback called when the Google API shows the achievement box, but I can't find any of those on the documentation... Only thing i found was this: Integrating Google Play services achievements and android notifications

@Override
public void onAchievementUnlocked(final String id)
{
    //
}

but I have no idea how to implement that or how to call that method and I had no luck on Google


回答1:


Use the following code:

Games.Achievements.incrementImmediate(GoogleApiClient apiClient, String id, int numSteps).setResultCallback(new  ResultCallback<Achievements.UpdateAchievementResult>() {

            @Override
            public void onResult(UpdateAchievementResult result) {
                if (result.getStatus().getStatusCode() == GamesStatusCodes.STATUS_ACHIEVEMENT_UNLOCKED) {
                    // play your sound. achievement was unlocked
                }
            }

        });

This will increment your achievement by the amount of steps you want, and when it is done, it will return some of these codes. If the code is GamesStatusCodes.STATUS_ACHIEVEMENT_UNLOCKED, then it means that increment unlocked the achievement and you can play your sound.



来源:https://stackoverflow.com/questions/22369199/android-play-a-sound-on-achievement-unlocked

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!