- Cocos2d for Mac Nov 21 st 2010. Published in Cocoa, Development. Cocos2d for iPhone is an open-source framework that is used to create 2D graphics and games for the iPhone and iPad, based on the original cocos2d project (which is written in Python). Cocos2d for iPhone is a port to iOS and is written in Objective-C, and is built upon OpenGL. The cocos2d for iPhone project has been successful.
- Targets the Operating Systems linux, mac or windows on Pc-like hardware. Provides some conventions and classes to help you structure a “scene based application”. A cocos2d application consists of several scenes, and a workflow connecting the different scenes.
- I'll describe how to set up Cocos2d-x for a build and create a basic project in this section. If you are already familiar with Cocos2d-x, you can skip ahead to the next chapter. Setting up the cocox2d-x installation. This section describes how you can get cocos2d-x running on a Mac with Xcode.
PreviousNext
Cocos Creator
First of all, Cocos2d-X is included as a sub-project. It makes available many different targets that can be built and included in your game. These include the cocos2dx library for iOS & Mac, the CocosDenshion sound engine for iOS & Mac, the Box2d & Chipmunk physics engines, and many other libraries.
Cocos Creator is a script development, entity-component and>
Cocos Creator is more powerful than others. Plug-in development is easy to use, supports multi-platform, reduces the workload for game developers so that they can be more focused on the games development. Their official technical support is timely to solve problems.
Hangzhou Electronic Soul Co. Ltd
Caicai (Game Producer)
We can fully feel the openness and agility when collaborating with the Cocos team. The developer's experience is paramount, all for a better Engine tool chain.
Lightspeed & Quantum Studios Group
Tongxing Ma (Technical Director)
The emergence of Cocos Creator really brightens my eyes. There has been a fundamental change in development thinking, from code-driven to completely componentized, data-driven thinking, which is in accordance with the development experience of game creation.
37 Interactive Entertainment Technology Co. Ltd
Zhou Liu (Vice President)
The openness and flexibility of Cocos has laid the foundation for our success in a series of games, such as Happy Aquarium, Happy Recreation and Seaside Recreation. The long-term support of Cocos community has enabled us to operate and iterate our products faster and more effectively.
As a free cross-platform engine, Cocos always supports the developer in many ways. Each update shows their compatibility and stability. As a developer, I’m very touched by their efforts.
Shanghai Mu77 Network Technology Co. Ltd
Jiaxian Lu (CEO)
Cocos is committed to the use of technology to change lives. I hope they can deeply explore the future, and push the Cocos engine to another high reaching level.
Shengqu Games Rexue Studio
Zaiwei Zhang (General Manager)
We’ve added nested prefabs and Android x86_64 support before we start moving on to version 3.0.
A new interactive video game built by Tencent using Cocos Creator makes big waves in China!
TAPCLAP moved their big hit to Cocos and gained four times the revenue. See why they changed and how they bring their games to VK and OK.ru.
The official C++ client handles all communication in realtime with the server. It implements all features in the server and is written with C++11.
Download¶
The client SDK can be downloaded from GitHub releases. You can download 'nakama-cocos2d-x-sdk_$version.zip'.
For upgrades you can see changes and enhancements in the CHANGELOG before you update to newer versions.
Help and contribute
The Cocos2d-x C++ client is open source on GitHub. Please report issues and contribute code to help us improve it.
Setup¶
When you've downloaded the Nakama Cocos2d archive and extracted it to
NAKAMA_COCOS2D_SDK
folder, you should include it in your project.We don't recommend to copy Nakama Cocos2d SDK to your project because it's quite big in size (~600 Mb).
Copy NakamaCocos2d folder¶
Copy
NakamaCocos2d
folder from NAKAMA_COCOS2D_SDK
to your Classes
folder.Setup for Mac and iOS projects¶
- Add
NAKAMA_COCOS2D_SDK/include
inBuild Settings > Header Search Paths
- Add libs folder in
Build Settings > Library Search Paths
:NAKAMA_COCOS2D_SDK/libs/ios
- for iOSNAKAMA_COCOS2D_SDK/libs/mac
- for Mac
- Add all
.a
files located in libs folder toGeneral > Frameworks, Libraries, and Embedded Content
- Add
NAKAMA_COCOS2D_SDK/NakamaCocos2d
folder to your XCode project as group (not a reference).
Setup for Android projects¶
If you use
CMake
then see Setup for CMake projects section.If you use
ndk-build
then add following to your Android.mk
file:Android uses a permissions system which determines which platform services the application will request to use and ask permission for from the user. The client uses the network to communicate with the server so you must add the 'INTERNET' permission.
Setup for CMake projects¶
Open for edit your
add next code before:CMakeLists.txt
file and find following existing code:At bottom of your
CMakeLists.txt
file add following:Setup for Visual Studio projects¶
In
Project Settings
add following:- Add
NAKAMA_COCOS2D_SDK/include
toC/C++ > General > Additional Include Directories
- Add folder to
Linker > General > Additional Library Directories
:NAKAMA_COCOS2D_SDK/libs/win32/v140
- for VS 2015 x86NAKAMA_COCOS2D_SDK/libs/win64/v140
- for VS 2015 x64NAKAMA_COCOS2D_SDK/libs/win32/v141
- for VS 2017 x86NAKAMA_COCOS2D_SDK/libs/win64/v141
- for VS 2017 x64NAKAMA_COCOS2D_SDK/libs/win32/v142
- for VS 2019 x86NAKAMA_COCOS2D_SDK/libs/win64/v142
- for VS 2019 x64
- Add all
.lib
files located in above folder toLinker > Input > Additional Dependencies
- Add sources from
Classes/NakamaCocos2d
to your Visual Studio project.
Usage¶
Include nakama helper header.
Initialize logger with debug logging level.
The client object is used to execute all logic against the server.
Note
By default the client uses connection settings '127.0.0.1' and 7350 port to connect to a local Nakama server.
Tick¶
The
tick
method pumps requests queue and executes callbacks in your thread. You must call it periodically (recommended every 50ms) in your thread.Without this the default client and realtime client will not work, and you will not receive responses from the server.
Authenticate¶
With a client object you can authenticate against the server. You can register or login a user with one of the authenticate options.
To authenticate you should follow our recommended pattern in your client code:
1. Build an instance of the client.
2. Authenticate a user. By default Nakama will try and create a user if it doesn't exist.
Tip
It's good practice to cache a device identifier on Android when it's used to authenticate because they can change with device OS updates.
In the code above we use
authenticateDevice()
but for other authentication options have a look at the code examples.Sessions¶
When authenticated the server responds with an auth token (JWT) which contains useful properties and gets deserialized into a
NSession
object.Cocos2d For Mac Tutorial
It is recommended to store the auth token from the session and check at startup if it has expired. If the token has expired you must reauthenticate. The expiry time of the token can be changed as a setting in the server.
A full example class with all code above is here.
Send requests¶
When a user has been authenticated a session is used to connect with the server. You can then send messages for all the different features in the server.
This could be to add friends, join groups, or submit scores in leaderboards. You can also execute remote code on the server via RPC.
Cocos2d For Android
All requests are sent with a session object which authorizes the client.
Have a look at other sections of documentation for more code examples.
Realtime client¶
The client can create one or more realtime clients. Each realtime client can have it's own event listener registered for responses received from the server.
Note
The socket is exposed on a different port on the server to the client. You'll need to specify a different port here to ensure that connection is established successfully.
Don't forget to call
tick
method. See Tick section for details.You can use realtime client to send and receive chat messages, get notifications, and matchmake into a multiplayer match. You can also execute remote code on the server via RPC.
To join a chat channel and receive messages:
There are more examples for chat channels here.
Handle events¶
A realtime client has event handlers which are called on various messages received from the server.
Event handlers only need to be implemented for the features you want to use.
Callbacks | Description |
---|---|
onDisconnect | Handles an event for when the client is disconnected from the server. |
onNotification | Receives live in-app notifications sent from the server. |
onChannelMessage | Receives realtime chat messages sent by other users. |
onChannelPresence | It handles join and leave events within chat. |
onMatchState | Receives realtime multiplayer match data. |
onMatchPresence | It handles join and leave events within realtime multiplayer. |
onMatchmakerMatched | Received when the matchmaker has found a suitable match. |
onStatusPresence | It handles status updates when subscribed to a user status feed. |
onStreamPresence | Receives stream join and leave event. |
onStreamState | Receives stream data sent by the server. |
Logging¶
Client logging is off by default.
To enable logs output to console with debug logging level:
Errors¶
The server and the client can generate logs which are helpful to debug code.
To enable client logs see Logging section.
In every request in the client you can set error callback. It will be called when request fails. The callback has
NError
structure which contains details of the error:The client writes all errors to logger so you don't need to do this.
Full Cocos2d-x C++ example¶
You can find the Cocos2d-x C++ example here
Cocos2d For Mac Download
Client reference¶
Cocos2d Macos
You can find the C++ Client Reference here.