In-app purchases with cocos2d-x

My game is almost done.

It’s time to put in-app purchases in it. Can anyone point me in the right direction for how to implement this for a cocos2d-x game on Ios platform?

Just a bit of feedback….

Where I work, they are thinking of not using cocos2d-x for future games after this game. They are starting to regret using it because of how difficult it is to implement in-app purchases and Admob.

As a programmer, I don’t want them to do this because developing games with cocos2d-x is simple enough, it’s in c++, and I don’t want to have to learn how to program with LUA.

If there was more help with the subjects of implementing In-app purchases, e.t.c…, I’m sure it would be very useful to a lot of people. Most companies expect these things present in their games and might be a deciding factor when choosing an engine or IDE.

First you need to learn how to do this using Objective-C, after that, is just a matter of including your objective-c classes on your xcode project. If you set your compiler to compile using ObjectiveC**, you can call any Obj-C without it complaining! You can build a regular c** class style and call all objective-c you want from with it.

Now, if you also share code to build your game on android and they share the same class, you have to think on abstraction using headers and separeted class files. But thats only if they share the same code. It is really not that difficult, although i never used in app neither admob. Guessing it isnt a walk in the park, but have faith! you can do it! :wink:

Awesome. Thanks for the reply Joe Espindola.

I think I can figure out how to use the In-app purchases, but like you said, the problem is calling the objective c tools from the game which is programmed in c++.

I have no idea how to do this. I know once I can figure this one out, I will be able to implement admob, in-app purchases, or anything else objective-c after that.

I know people are going to say, look at the “SimpleAudioEngine” it does this. But there is so much going on in the simpleaudioengine files that it is hard to dissern which parts of this code is the solution for the question at hand.

If someone could post a simplified example of how to do this?

I have searched the internet all morning and I do not see anyone answering this question anywhere.

In the meantime I will keep trying.

I’ve been checking out SimpleAudioEngine.mm and this is what I’ve been able to reproduce from what I learned looking at it.
Hopefully this will be a step in the right direction for implementing Admob and In-app purchase.

If anyone has ideas and comments….

The following is how I’ve managed to create an objective c class and call one of it’s functions from my code in C++.

OBJECTIVE C CLASS I WANT TO INVOKE:

//PrintMessage_objc.h
#import 

@interface PrintMessageObjc: NSObject {

}

-(void) PrintTheMessage;
@end

//PrintMessage_objc.m
#import "PrintMessage_objc.h"
#import 

@implementation PrintMessageObjc
-(void) PrintTheMessage {
    printf( "\n\n !! Hello message from objective c !! \n\n");
}

@end

GATEWAY CLASS I MADE (IN OBJECTIVE-C++):

//PrintMessage.h
#ifndef __PRINTMESSAGE_H__
#define __PRINTMESSAGE_H__

#include 

class PrintMessage
{
public:
    void PrintTheMessage();
};

#endif//__PRINTMESSAGE_H__

//PrinceMessage.mm (.mm means its objective-c++?  Which means you can access it in c++?  This file can combine c++ and objective c?)
#include "PrintMessage.h"
#include "PrintMessage_objc.h"

void PrintMessage::PrintTheMessage()
{
    PrintMessageObjc *message = [[PrintMessageObjc alloc] init];
    [message PrintTheMessage];
    [message release];
}

CALLING FROM COCOS2D C*+ GAME:
Now from anywhere in HelloWorld or e.t.c. you can call the objective-c*+ class which calls the objective-c class.
The following prints “ Hello message from objective c ”

PrintMessage* test = new PrintMessage();
test->PrintTheMessage();
delete test;
test = NULL;
1 Like

joe espindola wrote:

If you set your compiler to compile using ObjectiveC**, you can call any Obj-C without it complaining! You can build a regular c** class style and call all objective-c you want from with it.

Could you explain this more thouroghly. I do not quite understand.

Do you mean I don’t have to do like in my example above? I just need to change some compile settings and this will allow me to use objective-c classes directly in HelloWorld?

For anyone who want it, here is a bit of sample code similar to the above.

Just download the zip, unzip it, add the four files to your project.

Use the following code to open Game Center Achievements List:-

ObjCCalls* objc = new ObjCCalls();
if( objc->IsGameCenterAPIAvailable() )
      objc->ShowAchievements();
delete objc;
objc = NULL;

There is also a call to open a URL:

@ObjCCalls* objc = new ObjCCalls();
objc->OpenURL("http://www.google.com") ;
delete objc;
objc = NULL@;

Thanks for sharing.

Gav GMTDev wrote:

For anyone who want it, here is a bit of sample code similar to the above.
>
Just download the zip, unzip it, add the four files to your project.
>
Use the following code to open Game Center Achievements List:-
>
[…]
>
>
There is also a call to open a URL:
>
[…]

I added the four files to the project but it is giving a unresolved externals error.
I am using the WP8 Port v2.0 of cocos2d-x.

Muhammad Wajahat wrote:

Gav GMTDev wrote:
I added the four files to the project but it is giving a unresolved externals error.
I am using the WP8 Port v2.0 of cocos2d-x.

Those files are for iOS only, to call ObjC code on iOS from Cocos2d-x.

Can you tell me how can I do it on Windows phone 8??? I just want to post a facebook status using the url.

help!! where are the four files?

That info is from 2012/2013, the forums have been updated since and Cocos is now v3, this stuff was for and old version of V2. Probably won’t work but they are here on my blog:

http://www.gmtdev.com/blog/2012/04/18/calling-ios-obj-c-from-cocos2d-x-and-more/

You might want to be looking into V3’s Plugin-X feature:
http://www.cocos2d-x.org/wiki/How_to_Integrate_a_3rd_party_SDK_into_Plugin-X?project_id=cocos2d-x

Thanks :slight_smile: is very usefull