The company’s projects need to be connected to the game business, and cocos2D-X needs to be embedded in existing projects. The articles and materials found on the Internet are quite old, so summarize and record your own embedding process.
Download the cocos2D-x package
Download it at http://www.cocos2d-x.org/download/version#Cocos2d-x
This page has different versions, according to their own needs to download and unzip;
2. Generate iOS projects
- After decompressing cocos2D-x package, run
setup.py
Is executed on the terminalpython setup.py
, there will be two operations that need to enter the path, and return to continue. - After the command is executed, execute it again
cocos
Script:Cocos new < project name, such as: mygame> -p < package name, such as: com.your_company.mygame> -l < development language: lua or CPP or js> -d < project directory, such as: NEW_PROJECTS_DIR>
. - Finally, one is automatically generated in the current directory
NEW_PROJECTS_DIR
Folder, which is generated in this directorymygame
The project.
3. Copy the cocos2D-x resource
1. Existing projects:NewCocos
This project is used for future development
2. Copymygame
In the projectcocos2d-x
Files andClasses
The file toNewCocos
The root directory of the project
In 3.NewCocos
Create one in the root directoryResources
Folder and willmygame
In the projectres
,src
,config.json
Copy toNewCocos
In the projectResources
Folder.
4. ImportNewCocos
project
1. The importClasses
andResources
resources
2. The importCocos2d_libs. xcodeproj, cocos2d_lua_Bindings. Xcodeproj, libsimulator. Xcodeproj
5. Add the support library
6. Build Settings
- Close the Bitcode
- add
Header Search Paths
6. Ending
Because there is an AppDelegate in cocos, to avoid collisions, change the AppDelegate in the project to KMAppDelegate, and in main.m, change the AppDelegate to KMAppDelegate
7. Verify
1. Create a controllerRootViewController
///.h file @interface RootViewController: UIViewController @end ///.mm file#import "RootViewController.h"
#import "cocos2d.h"
#import "platform/ios/CCEAGLView-ios.h"
#include "scripting/lua-bindings/manual/CCLuaEngine.h"
#import "AppDelegate.h"
@interface RootViewController()
@end
@implementation RootViewController
AppDelegate* s_sharedApplication;
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
s_sharedApplication = new AppDelegate();
cocos2d::Application *app = cocos2d::Application::getInstance();
// Initialize the GLView attributes
app->initGLContextAttrs();
cocos2d::GLViewImpl::convertAttrs();
//cocos2d::EventListenerCustom *_listener;
CCEAGLView *eaglView = [CCEAGLView viewWithFrame: [UIScreen mainScreen].bounds
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 addSubview:eaglView];
cocos2d::GLView *glview = cocos2d::GLViewImpl::createWithEAGLView((__bridge void *)eaglView);
cocos2d::Director::getInstance()->setOpenGLView(glview);
//run the cocos2d-x game scene
cocos2d::Application::getInstance()->run();
UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[backBtn setTitle:@"Return" forState:UIControlStateNormal];
[backBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[backBtn addTarget:self action:@selector(backEvent) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:backBtn]; backBtn.frame = CGRectMake(20, 100, 60, 44); } - (void)backEvent { cocos2d::Director::getInstance()->end(); Dispatch_after (dispatch_time(DISPATCH_TIME_NOW), (int64_t)(0.15 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [self.presentingViewController dismissViewControllerAnimated:true completion:nil];
});
}
- (void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
delete s_sharedApplication;
s_sharedApplication = nullptr;
cocos2d::Application::sm_pSharedApplication = 0;
}
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
[super didRotateFromInterfaceOrientation:fromInterfaceOrientation];
auto glview = cocos2d::Director::getInstance()->getOpenGLView();
if (glview)
{
CCEAGLView *eaglview = (__bridge CCEAGLView *)glview->getEAGLView();
if (eaglview)
{
CGSize s = CGSizeMake([eaglview getWidth], [eaglview getHeight]);
cocos2d::Application::getInstance()->applicationScreenSizeChanged((int) s.width, (int) s.height);
}
}
}
@end
Copy the code
In 2.ViewController.m
Create a button and add a click event
- (IBAction)jump:(id)sender {
RootViewController *vc = [[RootViewController alloc] init];
[self presentViewController:vc animated:true completion:nil];
}
Copy the code
Over…