访问Game Center HighScores ios phonegap插件

时间:2015-09-15 13:59:38

标签: ios cordova game-center leaderboard

所以,

我正在使用phonegap和lee的ios插件来访问游戏中心。一切都运转正常,但我希望获得前10名。

我应该能够将这个[苹果示例代码] [1]集成到这个插件中。

我将- (void) getScores:(CDVInvokedUrlCommand*)command;添加到gamecenter.h

然后在gamecenter.m文件中,我正在处理该动作

- (void) getScores:(CDVInvokedUrlCommand*)command;

    {
        NSMutableDictionary *args = [command.arguments objectAtIndex:0];
        NSString *leaderboardId = [args objectForKey:@"leaderboardId"];

        __block CDVPluginResult* pluginResult = nil;
        NSMutableArray *topScores = [NSMutableArray array];
        GKLeaderboard *leaderboardRequest = [[GKLeaderboard alloc] init];
        if (leaderboardRequest != nil)
        {
            leaderboardRequest.playerScope = GKLeaderboardPlayerScopeGlobal;
            leaderboardRequest.timeScope = GKLeaderboardTimeScopeToday;
            leaderboardRequest.identifier = leaderboardId;
            leaderboardRequest.range = NSMakeRange(1,10);
            [leaderboardRequest loadScoresWithCompletionHandler: ^(NSArray *scores, NSError *error) {
                if (error != nil)
                {
                    pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR];
                }
                if (scores != nil)
                {

                    for (GKScore* score in scores)
                    {

                        NSMutableDictionary *entry = [NSMutableDictionary dictionary];
                        entry[@"score"] = score.description;

                        [topScores addObject:entry];
                    }

                    pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsArray: topScores];


                }
            [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
        }];
    }
}

然后在staging / www / plugins / cordova-plugin-gamecenter / www / gamecenter.js

GameCenter.prototype.getScores = function (success, failure, data) {
exec(success, failure, "GameCenter", "getScores", [data]);

};

接下来就是在游戏中固有地调用该函数:

function getHighScores(){
    console.log('gethighscores');

    var successCallback = function(data){
        console.log(success);
    }


    var failureCallback = function (data) {
        console.log('Fail');
    }

    var data = { leaderboardId: "escapemtl" }

    gamecenter.getScores(successCallback, failureCallback, data);}

如果我调用该功能,控制台中没有任何反应

1 个答案:

答案 0 :(得分:0)

所以经过一整天我完成了这项工作:

插入:

  GKPlayer* player = score.player;
                    NSMutableDictionary *entry = [NSMutableDictionary dictionary];
                    entry[@"score"] = [NSNumber numberWithLongLong:(score.value) ];
                    entry[@"name"] = player.alias;
                    [topScores addObject:entry];

进入:

 for (GKScore* score in scores)
                {


                    NSMutableDictionary *entry = [NSMutableDictionary dictionary];
                    entry[@"score"] = score.description;

                    [topScores addObject:entry];
                }