Book Image

Cocos2d-x Cookbook

By : Akihiro Matsuura
Book Image

Cocos2d-x Cookbook

By: Akihiro Matsuura

Overview of this book

Table of Contents (18 chapters)
Cocos2d-x Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Using native code


In Cocos2d-x, you can write one source for the cross platform. However, you have to write an Objective-C function or a Java function for the dependency process such as a purchase or push notification. If you want to call Java for Android from C++, you have to use JNI (Java Native Interface). In particular, JNI is very confusing. To call Java from C++, you have to use JNI. In this recipe, we will explain how to call an Objective-C function or a Java function from C++.

Getting ready

In this case, we will make a new class called Platform. You can get the application version by using this class. Before writing code, you will make three files called Platform.h, Platform.mm, and Platform.cpp in your project.

It is important that you don't add Platform.cpp to Compile Sources in Xcode. That's why Platform.cpp is for an Android target and doesn't need to be built for iOS. If you added it to Compile Sources, you have to remove it from there.

How to do it...

  1. Firstly, you have to make...