0

我正在将我的游戏从 cocos2d (obj-c) 移植到 cocos2d-x 2.x (c++) 但我真的很难让社交功能发挥作用。是否有可能以任何方式使用下面在本机objective-c和cocos2d中完美运行的代码。

截屏

- (UIImage*) takeScreenShot
{
    [CCDirector sharedDirector].nextDeltaTimeZero = YES;
    CGSize winSize = [CCDirector sharedDirector].winSize;

    CCLayerColor* blankLayer = [CCLayerColor layerWithColor:ccc4(255, 255, 255, 0) width:winSize.width height:winSize.height];

    blankLayer.position = ccp(winSize.width/2, winSize.height/2);

    CCRenderTexture* rtx = [CCRenderTexture renderTextureWithWidth:winSize.width height:winSize.height];

    [rtx begin];
    [blankLayer visit];
    //[[[CCDirector sharedDirector] runningScene] visit];
        [layer_main visit];
        [rtx end];

        return [rtx getUIImage];
    }

在脸书上发帖

    - (void)share_score_facebook: (id) sender{
    if (SYSTEM_VERSION_LESS_THAN(@"6.0")){
        [AppVariables showAlert:@"Outdated iOS" message:@"You must have atleast iOS 6.0 to use the social features.\nPlease update to a newer iOS."];
        return;
    }

    [[CCDirector sharedDirector] pause];
    [[CCDirector sharedDirector] stopAnimation];

    AppController *app = (AppController*) [[UIApplication sharedApplication] delegate];

    UIImage * screenshot = [self takeScreenShot];

    SLComposeViewController *faceBookPost = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];

    [faceBookPost setInitialText:[NSString stringWithFormat:@"Game on! Just scored %d on Game. Can you Beat it?",total_score]];
    [faceBookPost addImage:screenshot];
    [faceBookPost addURL:[NSURL URLWithString:@"URL_HERE"]];

    [[app navController] presentViewController:faceBookPost animated:YES completion:nil];
    faceBookPost.completionHandler = ^(SLComposeViewControllerResult result)
    {
        [[CCDirector sharedDirector] resume];
        [[CCDirector sharedDirector] startAnimation];
        [[app navController] dismissViewControllerAnimated:YES completion:nil];

    };
}
4

1 回答 1

0

你可以用 cocos2d-x 编译任何 Objective-C 代码,但是你需要修改你的代码以适应 cocos2d-x API。您想截取屏幕截图并将其发布到 Facebook,对吗?使用以下代码代替您的 Objective-C 代码怎么样?

于 2014-06-20T04:36:55.037 回答