[Resolved] How to embed Cocos2d-x game into an iOS application project?

[Resolved] How to embed Cocos2d-x game into an iOS application project?
0.0 0

#1

Hi everyone,

We have an iOS application (which is built with Objective-C). Now we want to build some game inside the app and we would like to use Cocos2d-X. Therefore, we need to find a way to embed / integrate Cocos2d-x into our Xcode project. The reason we choose Cocos2d-X is we need to build those games on Android as well.

PS: I found some tips do that but they are very old now.

Thanks !

UPDATE:

I resolved this by myself. This issue should be closed !


#2

Hello ngoclt,
We have the same question to embed / integrate Cocos2d-x into an iOS project. We work for the question for a week, but still failed with some error. Can you help me?
Thanks!
PS:My email 344748930@qq.com


#3

Sorry for late response @vivi ,

This is how I did :

1, Added cocos2dx ios library into project

2, Set library search paths:
Header Search Paths

Library Search Paths

3, Try to build projects, there should be some Error that inform you that can’t find some Frameworks. You will need to add those frameworks to your project.

4, Change the extension of AppDelegate implementation file to .mm (that allows your code support Objective-C++)

Add this code to AppDelegate.mm

And this, (that notify your game controller - will show u below - when app is put into background or back to foreground):

5, Define your game controller (a view that will display your game):

@interface TVGameController : NSObject

@property (nonatomic, strong) UIView *view;

- (instancetype)initWithFrame:(CGRect)frame;

- (void)backToApp:(NSDictionary *)message;

@end

@implementation TVGameController

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super init]) {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appIsInBackground:) name:TVNotificationAppIsInBackground object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appBackToForeground:) name:TVNotificationAppBackToForeground object:nil];
        
        // Init the CCEAGLView
        CCEAGLView *eaglView = [CCEAGLView viewWithFrame:frame
                                             pixelFormat:(__bridge NSString*)cocos2d::GLViewImpl::_pixelFormat
                                             depthFormat:cocos2d::GLViewImpl::_depthFormat
                                      preserveBackbuffer:NO
                                              sharegroup:nil
                                           multiSampling:NO
                                         numberOfSamples:0];
        // Enable or disable multiple touches
        [eaglView setMultipleTouchEnabled:NO];
        
        self.view = eaglView;
        
        GLView *glview = GLViewImpl::createWithEAGLView((__bridge void*)eaglView);
        Director::getInstance()->setOpenGLView(glview);
        
        Application::getInstance()->run();
        
        [IOSNDKHelper setNDKReceiver:self];
    }
    
    return self;
}

- (void)appIsInBackground:(NSNotification *)notification
{
    cocos2d::Application::getInstance()->applicationDidEnterBackground();
}

- (void)appBackToForeground:(NSNotification *)notification
{
    cocos2d::Application::getInstance()->applicationWillEnterForeground();
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

#pragma mark - Receiver Methods 

- (void)backToApp:(NSDictionary *)message
{
    [IOSNDKHelper setNDKReceiver:nil];
    Director::getInstance()->end();

}

@end

6, If you have prefix file, you need to use this to block your Objective-C definition:

#ifdef __cplusplus

#endif

#ifdef __OBJC__

#define CORNER_RADIUS     10
 
#endif

Tell me if you need more help !

ping me via: ngoc.le [at] unify [dot] com


#4

So many thanks for your help !
They are useful, and i resolved it too.
Your save my time !


#5

Hi @ngoclt

I’m trying to do the same on OS X but the AppDelegate is quite different in Cocos2d-x v3.10, would you mind sharing your thoughts on how to accomplish this on a mac application?

Regards.


#6

Hi ngocl

May you upload the screen shots one more time please?
There are not available.

Best regards


#7

Hi,

I dont have the screenshot anymore, however I can share my code with you when I back home (in next 12 hours), Im at work right now.

Regards,


#8

Hello ngoclt,

It would be greatfull :slight_smile:


#9

Hi @ngoclt
Can you please share the code, it would be more helpfull.
Thanks in advance


#10

Hello all
Anyone knows exact steps to embed cocos2dx game into an ios application?
It would be more greatfull.


#11

I’m really sorry since I can’t access to that project anymore since it was a project in my previous company. I’m working on a sample project which will embed latest cocos2dx version into an iOS application. Hopefully, I will be able to finish it by tomorrow.


#12

@ngoclt Thanks for your reply.


#13

Steps to integrate cocos2d into IOS application
1: Copy the cocos2d folder from cocos2dx project paste to application project

2: Add the cocos2-libs reference to project hierarchy( /cocos2d/build/cocos2d_libs.xcodeproj)

3 In build phases add these following frameworks.

4 Add the Below paths in Project/BuildSetting Header search path. (Note : Depends on cocos2d library path)
$(SRCROOT)/cocos2d
$(SRCROOT)/cocos2d/cocos/audio/include
$(SRCROOT)/cocos2d/cocos
$(SRCROOT)/cocos2d/extensions
$(SRCROOT)/cocos2d/external
$(SRCROOT)/cocos2d/external/chipmunk/include/chipmunk

5 Add the Below paths in Target Header search path
$(inherited)

6 Add the Below c++ compiler settings in Target/BuildSetting 27 PM48 PM

7 Make objective file extension should be .mm instead of .m. for the file, which includes cocos2d header file


#14

Hi everyone

I have a swift application project and cocos2d-x game
how to embed the cocos2d-x game in the swift application project?

best regards


#15

You might want to talk with the cocos2d-swift guys. We are Cocos2d-x, JS and Lua plus Cocos Creator.


#16

Really sorry for couldn’t keep following on this topic. Anyway, I actually created an example project for doing this last year. But just couldn’t finish it and share with you guys. Here is what I have been done so far: https://github.com/ngoclt/iOSCocos2dXApp

There is only one issue left which is the app will crash after you open the game again. You will need to call the method to end game from C++, here is the code:

void BaseLayer::backToApp(Ref *sender)
{
    // This is the method to call a method in Objective-C (I used EasyNDK lib)
    sendMessageWithParams("backToApp", Value::Null);
    Director::getInstance()->end();
}

Method backToApp is implemented in GameController.

Hope this can be useful for you guys.

Regards,