首页 > 代码库 > iOS游戏开发游戏功能之外的东西

iOS游戏开发游戏功能之外的东西

对于一个游戏的开发,我们除了完成游戏的功能之外,还有多少东西我们需要考虑呢?


很多,也很烦!


但做过一遍之后下一次就会很简单。


都有什么东西我们想添加到游戏当中呢?


(1)分享功能

(2)评分功能

(3)游戏中心(GameCenter)

(4)广告(iAd以及其他广告比如Admob)

(5)应用内购买

(6)。。。


这些功能并不是完全必要的,要根据情况考虑。但比如分享,评分,这些功能能提高一个游戏的扩散速度,显示是值得每一个游戏都添加的功能。


下面稍微总结一下每一个功能的基本用法。

PS:这只是一个总结帖,具体每个功能的用法,网上都有相应的Tutorial。


(1)分享功能

最简单最直接的方法就是利用iOS自带的分享功能,使用UIActivityViewController:

NSString *initialString = @"Smash Bug! is a Great App! Have Fun with it!";
    NSURL *url = [NSURL URLWithString:@"https://itunes.apple.com/us/app/air-drum-*/id901397384?ls=1&mt=8"];
    //UIImage *showImage = [UIImage imageNamed:@"Default-568h@2x"];
    UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:@[initialString,url] applicationActivities:nil];
    [self presentViewController:activityViewController animated:YES completion:nil];

至于要实现分享到朋友圈,QQ空间等,大家可以在网上找到相应的分享代码。


(2)评分

就是点击之后直接跳转到App Store,这个非常简单也非常重要:

NSString *str = @"itms-apps://ax.itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?type=Purple+Software&id=901397384";
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];

大家在使用时把id改成自己应用的id就ok了。


(3)GameCenter

这个国内可能用得比较少,更喜欢微信之类,但在国外恐怕还是比较重要的一个方式。


这个大家得在iTunesConnect上启用GameCenter,并创建相应的LeaderShip和Achievement。

Raywenderlich上有相应的Tutorial。


而对于使用其实就两个流程:

(1)验证本地玩家,如果没登陆,弹出窗口登陆。

- (void)authenticateLocalPlayer
{
    GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer];
    
    localPlayer.authenticateHandler = ^(UIViewController *viewController,NSError *error) {
        //3
        [self setLastError:error];
        
        if (viewController != nil) {
            [self setAuthenticationViewController:viewController];
        } else if ([GKLocalPlayer localPlayer].isAuthenticated) {
            _enableGameCenter = YES;
        } else {
            _enableGameCenter = NO;
        }
    };
}


- (void)setAuthenticationViewController:(UIViewController *)authenticationViewController
{
    if (authenticationViewController != nil) {
        _authenticationViewController = authenticationViewController;
        [[NSNotificationCenter defaultCenter] postNotificationName:PresentAuthenticationViewController object:self];
    }
}

(2)实时发送分数等数据到GameCenter

2.1发送Achievement

创建Achievement成就的方法:


+ (GKAchievement *)reach10Achievement:(NSUInteger)numberOfReach
{
    CGFloat percent = numberOfReach/10 * 100.0;
    
    GKAchievement *reachAchievement = [[GKAchievement alloc] initWithIdentifier:kSmashBugReach10AchievementId];
    reachAchievement.percentComplete = percent;
    reachAchievement.showsCompletionBanner = YES;
    return reachAchievement;
    
}

发送成就


- (void)reportAchievements:(NSArray *)achievements
{
    if (!_enableGameCenter) {
        NSLog(@"Local play is not authenticated");
    }
    
    [GKAchievement reportAchievements:achievements withCompletionHandler:^(NSError *error) {
        [self setLastError:error];
    }];
}

2.2 发送得分等到LeaderShip(排行榜)


- (void)reportScore:(int64_t)score forLeaderboardID:(NSString *)leaderboardID
{
    if (!_enableGameCenter) {
        NSLog(@"Local Play is not authenticated");
    }
    
    GKScore *scoreRep
除此之外,我们还想点击GameCenter按钮之类显示GameCenter的界面:

orter = [[GKScore alloc] initWithLeaderboardIdentifier:leaderboardID]; scoreReporter.value = http://www.mamicode.com/score; scoreReporter.context = 0; NSArray *scores = @[scoreReporter]; [GKScore reportScores:scores withCompletionHandler:^(NSError *error) { [self setLastError:error]; }];}

除此之外,我们还想点击GameCenter按钮之类显示GameCenter的界面:

- (void)showGKGameCenterViewController:(UIViewController *)viewController
{
    if (!_enableGameCenter) {
        NSLog(@"Local Play is not authenticated");
    }
    
    GKGameCenterViewController *gameCenterViewController = [[GKGameCenterViewController alloc] init];
    
    gameCenterViewController.gameCenterDelegate = self;
    
    gameCenterViewController.viewState = GKGameCenterViewControllerStateAchievements;
    
    [viewController presentViewController:gameCenterViewController animated:YES completion:nil];
}

(4)广告

iAd最基本的横幅广告现在实在是太简单了,iOS7:

在要显示广告的ViewController中添加一句代码就行:


    self.canDisplayBannerAds = YES;

而Admob(我只用Google的广告)也很简单,到Admob注册后,然后下载其SDK,添加SDK到工程。

重要一步:添加-ObjC到Linker Flag

然后就简单了,只需以下代码copy到ViewController:


// Admob
    [self addAdmob];

#pragma mark - Admob

- (void)addAdmob
{
    // Initialize the banner at the bottom of the screen.
    CGPoint origin = CGPointMake(0.0,
                                 self.view.frame.size.height -
                                 CGSizeFromGADAdSize(kGADAdSizeBanner).height);
    
    // Use predefined GADAdSize constants to define the GADBannerView.
    self.adBanner = [[GADBannerView alloc] initWithAdSize:kGADAdSizeBanner origin:origin];
    
    // Note: Edit SampleConstants.h to provide a definition for kSampleAdUnitID before compiling.
    self.adBanner.adUnitID = ADMOB_ID;
    self.adBanner.delegate = self;
    self.adBanner.rootViewController = self;
    [self.view addSubview:self.adBanner];
    [self.adBanner loadRequest:[self request]];
}

#pragma mark GADRequest generation

- (GADRequest *)request {
    GADRequest *request = [GADRequest request];
    
    // Make the request for a test ad. Put in an identifier for the simulator as well as any devices
    // you want to receive test ads.
    request.testDevices = @[
                            // TODO: Add your device/simulator test identifiers here. Your device identifier is printed to
                            // the console when the app is launched.
                            GAD_SIMULATOR_ID
                            ];
    return request;
}

#pragma mark GADBannerViewDelegate implementation

// We‘ve received an ad successfully.
- (void)adViewDidReceiveAd:(GADBannerView *)adView {
    NSLog(@"Received ad successfully");
}

- (void)adView:(GADBannerView *)view didFailToReceiveAdWithError:(GADRequestError *)error {
    NSLog(@"Failed to receive ad with error: %@", [error localizedFailureReason]);
}

万事OK!根据游戏的具体情况再做改动!


(5)应用内购买

这个在我另一篇blog有讲,这里就不再说。


ok,就写这么多了。