cocos2d-x
Cocos2d-x is a suite of open-source, cross-platform, game-development tools utilized by millions of developers across the globe. Its core has evolved to serve as the foundation for Cocos Creator 1.x & 2.x.
Top Related Projects
Godot Engine – Multi-platform 2D and 3D game engine
Desktop/Android/HTML5/iOS Java game development framework
A simple and easy-to-use library to enjoy videogames programming
Simple and Fast Multimedia Library
🐍🎮 pygame (the library) is a Free and Open Source python programming language library for making multimedia applications like games built on top of the excellent SDL library. C, Python, Native, OpenGL.
LÖVE is an awesome 2D game framework for Lua.
Quick Overview
Cocos2d-x is a popular open-source game development framework for building 2D and 3D games across multiple platforms. It provides a comprehensive set of tools and libraries for creating high-performance games using C++, with additional bindings for Lua and JavaScript.
Pros
- Cross-platform compatibility (iOS, Android, Windows, macOS, Linux)
- Extensive feature set for 2D and 3D game development
- Active community and regular updates
- Good performance and optimization capabilities
Cons
- Steeper learning curve compared to some other game engines
- Documentation can be inconsistent or outdated in some areas
- Limited visual editor capabilities compared to Unity or Unreal Engine
- Smaller ecosystem of third-party tools and plugins
Code Examples
- Creating a simple sprite:
auto sprite = Sprite::create("player.png");
sprite->setPosition(Vec2(100, 100));
this->addChild(sprite);
- Adding a touch event listener:
auto listener = EventListenerTouchOneByOne::create();
listener->onTouchBegan = [](Touch* touch, Event* event) {
// Handle touch event
return true;
};
this->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener, this);
- Creating a simple animation:
Vector<SpriteFrame*> frames;
for (int i = 1; i <= 5; i++) {
frames.pushBack(SpriteFrame::create(StringUtils::format("animation%d.png", i), Rect(0, 0, 100, 100)));
}
auto animation = Animation::createWithSpriteFrames(frames, 0.1f);
auto animate = Animate::create(animation);
sprite->runAction(RepeatForever::create(animate));
Getting Started
- Download and install Cocos2d-x from the official website.
- Set up your development environment (Visual Studio for Windows, Xcode for macOS).
- Create a new project using the command line tool:
cocos new MyGame -p com.example.mygame -l cpp -d ./projects
- Open the project in your IDE and start developing your game.
- Build and run your game on your desired platform using the provided build scripts or IDE integration.
Competitor Comparisons
Godot Engine – Multi-platform 2D and 3D game engine
Pros of Godot
- Fully open-source and free to use, with a more permissive MIT license
- Integrated development environment (IDE) with visual editor
- Supports multiple programming languages (GDScript, C#, C++)
Cons of Godot
- Smaller community and ecosystem compared to Cocos2d-x
- Less optimized for mobile platforms, especially for complex 3D games
Code Comparison
Godot (GDScript):
extends Sprite
func _ready():
position = Vector2(100, 100)
scale = Vector2(2, 2)
Cocos2d-x (C++):
auto sprite = Sprite::create("image.png");
sprite->setPosition(Vec2(100, 100));
sprite->setScale(2.0f);
this->addChild(sprite);
Both engines offer simple ways to create and manipulate sprites, but Godot's GDScript provides a more concise syntax. Cocos2d-x uses C++, which may be more familiar to some developers and potentially offers better performance. Godot's scene-based structure allows for more modular development, while Cocos2d-x follows a more traditional object-oriented approach.
Desktop/Android/HTML5/iOS Java game development framework
Pros of libGDX
- Cross-platform development with a single codebase (Java)
- Extensive documentation and active community support
- Flexible and modular architecture, allowing for easy customization
Cons of libGDX
- Steeper learning curve for developers new to Java
- Less optimized for mobile platforms compared to Cocos2d-x
- Limited built-in UI tools and editors
Code Comparison
libGDX:
public class MyGame extends ApplicationAdapter {
@Override
public void create() {
// Game initialization code
}
}
Cocos2d-x:
class MyScene : public cocos2d::Scene {
public:
static cocos2d::Scene* createScene();
virtual bool init();
};
Summary
libGDX offers cross-platform development with Java, extensive documentation, and a flexible architecture. However, it has a steeper learning curve and less optimization for mobile platforms compared to Cocos2d-x. libGDX uses Java for game development, while Cocos2d-x primarily uses C++. Both frameworks have their strengths, and the choice between them depends on the developer's preferences and project requirements.
A simple and easy-to-use library to enjoy videogames programming
Pros of raylib
- Lightweight and easy to learn, with a simple API
- Cross-platform support for desktop, web, and mobile
- No external dependencies, making it easier to set up and deploy
Cons of raylib
- Less comprehensive feature set compared to Cocos2d-x
- Smaller community and ecosystem
- Limited support for advanced 2D game development features
Code Comparison
raylib:
#include "raylib.h"
int main(void)
{
InitWindow(800, 450, "raylib [core] example - basic window");
while (!WindowShouldClose())
{
BeginDrawing();
ClearBackground(RAYWHITE);
DrawText("Congrats! You created your first window!", 190, 200, 20, LIGHTGRAY);
EndDrawing();
}
CloseWindow();
return 0;
}
Cocos2d-x:
#include "cocos2d.h"
class HelloWorld : public cocos2d::Scene
{
public:
static cocos2d::Scene* createScene();
virtual bool init();
CREATE_FUNC(HelloWorld);
};
bool HelloWorld::init()
{
if (!Scene::init()) return false;
auto label = Label::createWithTTF("Hello World", "fonts/Marker Felt.ttf", 24);
label->setPosition(Vec2(origin.x + visibleSize.width/2, origin.y + visibleSize.height - label->getContentSize().height));
this->addChild(label, 1);
return true;
}
Simple and Fast Multimedia Library
Pros of SFML
- Simpler and more lightweight, making it easier to learn and use
- More flexible and customizable, allowing for greater control over low-level operations
- Better suited for 2D game development and multimedia applications
Cons of SFML
- Less comprehensive feature set compared to Cocos2d-x
- Smaller community and ecosystem, resulting in fewer resources and third-party tools
- Limited support for mobile platforms and cross-platform development
Code Comparison
SFML example:
#include <SFML/Graphics.hpp>
int main() {
sf::RenderWindow window(sf::VideoMode(800, 600), "SFML Window");
sf::CircleShape shape(100.f);
shape.setFillColor(sf::Color::Green);
while (window.isOpen()) {
// Event handling and drawing logic
}
return 0;
}
Cocos2d-x example:
#include "cocos2d.h"
class MyScene : public cocos2d::Scene {
public:
CREATE_FUNC(MyScene);
bool init() override {
if (!Scene::init()) return false;
auto sprite = cocos2d::Sprite::create("sprite.png");
this->addChild(sprite);
return true;
}
};
🐍🎮 pygame (the library) is a Free and Open Source python programming language library for making multimedia applications like games built on top of the excellent SDL library. C, Python, Native, OpenGL.
Pros of Pygame
- Simpler learning curve, especially for Python beginners
- Extensive documentation and community support
- Lightweight and easy to set up
Cons of Pygame
- Limited built-in features compared to Cocos2d-x
- Performance may be slower for complex games
- Less suitable for cross-platform mobile development
Code Comparison
Pygame example:
import pygame
pygame.init()
screen = pygame.display.set_mode((800, 600))
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
Cocos2d-x example:
#include "cocos2d.h"
class MyScene : public cocos2d::Scene {
public:
static cocos2d::Scene* createScene();
virtual bool init();
CREATE_FUNC(MyScene);
};
Pygame is more straightforward for simple 2D games, while Cocos2d-x offers a more comprehensive framework for complex, cross-platform game development. Pygame is Python-based, making it accessible for beginners, whereas Cocos2d-x uses C++, providing better performance but with a steeper learning curve. Cocos2d-x excels in mobile game development and offers more built-in features, while Pygame is better suited for quick prototyping and educational purposes.
LÖVE is an awesome 2D game framework for Lua.
Pros of LÖVE
- Simpler and more beginner-friendly
- Lightweight and faster to set up
- Cross-platform with a single codebase
Cons of LÖVE
- Less feature-rich compared to Cocos2d-x
- Limited to 2D game development
- Smaller community and ecosystem
Code Comparison
LÖVE (main.lua):
function love.draw()
love.graphics.print("Hello World!", 400, 300)
end
Cocos2d-x (HelloWorldScene.cpp):
bool HelloWorld::init() {
if (!Scene::init()) return false;
auto label = Label::createWithTTF("Hello World", "fonts/Marker Felt.ttf", 24);
label->setPosition(Vec2(origin.x + visibleSize.width/2, origin.y + visibleSize.height - label->getContentSize().height));
this->addChild(label, 1);
return true;
}
LÖVE uses Lua, which is more concise and easier to read, especially for beginners. Cocos2d-x uses C++, offering more control but with increased complexity. LÖVE's simplicity allows for quicker prototyping, while Cocos2d-x provides more advanced features and better performance for larger projects.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
cocos2d-x
Win32 | Others |
---|---|
cocos2d-x is a multi-platform framework for building 2d games, interactive books, demos and other graphical applications. It is based on cocos2d-iphone, but instead of using Objective-C, it uses C++. It works on iOS, Android, macOS, Windows and Linux.
Cocos2d-x Framework Architecture:
cocos2d-x is:
- Fast
- Free
- Easy to use
- Community supported
Git user attention
-
Clone the repo from GitHub.
$ git clone https://github.com/cocos2d/cocos2d-x.git
-
After cloning the repo, please execute
download-deps.py
to download and install dependencies.$ cd cocos2d-x cocos2d-x $ python download-deps.py
-
After running
download-deps.py
.cocos2d-x $ git submodule update --init
Download stable versions
Documentations and samples
- All Docs in a single place!
- Online API Reference Note that Cocos2d-x and Cocos Creator have different API set
- Programmers Guide
- Latest Release Note
- Changelog
Main features
- Scene management (workflow)
- Transitions between scenes
- Sprites and Sprite Sheets
- Effects: Lens, Ripple, Waves, Liquid, etc.
- Actions (behaviours):
- Transformation Actions: Move, Rotate, Scale, Fade, Tint, etc.
- Composable actions: Sequence, Spawn, Repeat, Reverse
- Ease Actions: Exp, Sin, Cubic, Elastic, etc.
- Misc actions: CallFunc, OrbitCamera, Follow, Tween
- Basic menus and buttons
- Integrated with physics engines: Box2d and Chipmunk
- Particle system
- Skeleton Animations: Spine and Armature support
- Fonts:
- Fast font rendering using Fixed and Variable width fonts
- Support for .ttf fonts
- Tile Map support: Orthogonal, Isometric and Hexagonal
- Parallax scrolling
- Motion Streak
- Render To Texture
- Touch/Accelerometer on mobile devices
- Touch/Mouse/Keyboard on desktop
- Sound Engine support
- Integrated Slow motion/Fast forward
- Fast and compressed textures: PVR compressed and uncompressed textures, ETC1 compressed textures, and more
- Resolution Independent
- Language: C++, with Lua and JavaScript bindings
- Open Source Commercial Friendly(MIT): Compatible with open and closed source projects
- OpenGL ES 2.0 (mobile) / OpenGL 2.1 (desktop) / metal(macos and iOS) based
Build Requirements
- Mac OS X 10.7+, Xcode 8+
- or Ubuntu 14.04+, CMake 3.1+
- or Windows 7+, VS 2015
- Python 2.7.5+(NOT Python 3)
- NDK r16+ is required to build Android games
- Android Studio 3.0.0+ to build Android games(tested with 3.0.0)
- JRE or JDK 1.6+ is required for web publishing
Runtime Requirements
- iOS 8.0+ for iPhone / iPad games
- Android 3.0.0+ for Android
- OS X v10.9+ for Mac games
- Windows 7+ for Win games
Environment Setup
Should set up environment before starting a new game or running tests
$ cd cocos2d-x
$ ./setup.py
$ source FILE_TO_SAVE_SYSTEM_VARIABLE
Should invoke this script if using linux system
$ cd cocos2d-x
$ ./install-linux-deps.sh
Running Tests
$ cd cocos2d-x
$ mkdir build
$ cd build
$ cocos run --proj-dir .. -p [mac|win32|android|linux|ios]
How to start a new game
$ cd cocos2d-x
$ ./setup.py
$ source FILE_TO_SAVE_SYSTEM_VARIABLE
$ cocos new MyGame -p com.your_company.mygame -l cpp -d NEW_PROJECTS_DIR
$ cd NEW_PROJECTS_DIR/MyGame
$ mkdir build
$ cd build
$ cocos run --proj-dir .. -p [mac|win32|android|linux|ios]
You can also create a Lua project with -l lua
.
Using IDE
If need to debug program, then it is more convinent to use IDE to run and debug it. All platforms other than Android can use CMake to generate corresponding project file. Can refer to Detail CMake Guide for detail information.
For Android, the Android Studio project file lies in PROJECT_DIR/proj.android
. Can just use Android Studio to import the exsting project file.
Learning Resources
Spreading the word!
You can help us spread the word about cocos2d-x! We would surely appreciate it!
- Twitter: @CocosEngine
- Facebook: https://www.facebook.com/CocosEngine
- YouTube: https://www.youtube.com/cocosengine
- Weibo: @Cocoså¼æ
- bilibili: https://space.bilibili.com/491120849
Where to get help
- English Forums
- ä¸æ社åº
- Bug Tracker
- API Reference.
- Latest Release Note
- Changelog
- Discord Channel
cpp-tests
project. This project is our basis for testing. Use this project to learn how we implement the functionality of the engine. This project is located in cocos2d-x_root/build.
Contributing to the Project
Cocos2d-x is licensed under the MIT License. We welcome participation!
Did you find a bug? Do you have feature request? Do you want to merge a feature?
Embrace the Future: Switch to Cocos Creator for a Better Experience
Cocos Creator is the new generation of Cocos game engine with a full featured editor and content creation friendly workflow. It supports all major platforms allowing games to be quickly released for the web, iOS, Android, Windows, Mac, and various mini-game platforms. Millions of developers have built 2D / 3D experiences, from hardcore games to web instant entertainment. A pure JavaScript-developed engine runtime is available on the web and mini-game platforms for better performance and smaller packages. On other native platforms, C++ is used to implement the underlying framework, providing greater operational efficiency. The engine is completely open source, and retains the advantages of Cocos2d-x which includes high performance, customizability, ease for debugging, easy to learn, and small package size.
Therefore, we no longer recommend new users to start with Cocos2d-x. Instead, please use the brand-new Cocos Creator for project development to enjoy the best editor and 3D support.
Top Related Projects
Godot Engine – Multi-platform 2D and 3D game engine
Desktop/Android/HTML5/iOS Java game development framework
A simple and easy-to-use library to enjoy videogames programming
Simple and Fast Multimedia Library
🐍🎮 pygame (the library) is a Free and Open Source python programming language library for making multimedia applications like games built on top of the excellent SDL library. C, Python, Native, OpenGL.
LÖVE is an awesome 2D game framework for Lua.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot