1

I am new cocos2dx developer.I am using the version cocos2dx 2.2.6.Now i want to share image on gmail and facebook.I refer many site and suggestion but still i am not clear about sharing.

2 Answers 2

0

We can use the UIActivityViewController to create a sharing dialog and use some CCRenderTexture code to take a screenshot. When the share button is tapped, a number of options for sharing are available depending on what applications the user has available on their phone.


You can create a sharing popup using the following code:

func openShareDialog() {
    var scene = CCDirector.sharedDirector().runningScene
    var node: AnyObject = scene.children[0]
    var screenshot = screenShotWithStartNode(node as! CCNode)

    let sharedText = "Text"
    let itemsToShare = [screenshot, sharedText]

    var excludedActivities = [ UIActivityTypeAssignToContact,
        UIActivityTypeAddToReadingList, UIActivityTypePostToTencentWeibo]

    var controller = UIActivityViewController(activityItems: itemsToShare, applicationActivities: nil)
    controller.excludedActivityTypes = excludedActivities

    UIApplication.sharedApplication().keyWindow?.rootViewController?.presentViewController(controller, animated: true, completion: nil)
}

Then we can take a screenshot using a CCRenderTexture:

func takeScreenshotWithNode(node: CCNode) -> UIImage {
    CCDirector.sharedDirector().nextDeltaTimeZero = true
    var viewSize = CCDirector.sharedDirector().viewSize()
    var rtx = CCRenderTexture(width: Int32(viewSize.width), height: Int32(viewSize.height))
    rtx.begin()
    node.visit()
    rtx.end()
    return rtx.getUIImage()
}

Here's an example of what the popup would actually look like when displayed:

Example UIActivityViewController

You can find more information on the UIActivityViewController in Apple's docs.

7
  • thanks for helping me but i using the cocos2dx ,so is this code is allow in cocos2dx or not?? Commented Aug 10, 2015 at 4:49
  • i try to implement this code but some of the keyword is not allowed in cocoss2dx. Commented Aug 10, 2015 at 5:00
  • Can you copy / paste the error you're getting? @Amishabeladiya Commented Aug 10, 2015 at 12:45
  • when implement your first code in that var and let keyword is not support in cocos2dx so that any other solution for that. Commented Aug 11, 2015 at 5:12
  • @Amishabeladiya: what programming language are you trying to use? This code is for Swift Commented Aug 11, 2015 at 22:18
0

You can might find it handy to start by reading the docs here.

There are additional detailed instructions in the cocos2d-x.org site including ones for iOS.

2
  • I do in my project above the link suggestion but the sharing screen is not show.It is blank nothing change in project. Commented Aug 11, 2015 at 13:25
  • and this instruction is for cocos2dx-js .But now i use the cocos2dx c++.Any other suggestion for sharing in cocos2dx c++. Commented Aug 12, 2015 at 7:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.