Cocos2d-x Simple Counter Game - Cocos2dx Typing CST Test
Loading…
Cocos2d-x Simple Counter Game — Cocos2dx Code
A basic Cocos2d-x example that creates a counter label and buttons to increment or decrement the value.
#include "CounterScene.h"
#include "ui/CocosGUI.h"
USING_NS_CC;
Scene* CounterScene::createScene() {
auto scene = Scene::create();
auto layer = CounterScene::create();
scene->addChild(layer);
return scene;
}
bool CounterScene::init() {
if (!Layer::init()) return false;
count = 0;
label = Label::createWithTTF("Count: 0", "fonts/Marker Felt.ttf", 24);
label->setPosition(Vec2(200, 200));
this->addChild(label);
auto incButton = ui::Button::create();
incButton->setTitleText("+");
incButton->setPosition(Vec2(150, 100));
incButton->addClickEventListener([&](Ref*) { updateCount(1); });
this->addChild(incButton);
auto decButton = ui::Button::create();
decButton->setTitleText("-");
decButton->setPosition(Vec2(250, 100));
decButton->addClickEventListener([&](Ref*) { updateCount(-1); });
this->addChild(decButton);
return true;
}
void CounterScene::updateCount(int delta) {
count += delta;
label->setString("Count: " + std::to_string(count));
}Cocos2dx Language Guide
Cocos2d-x is a cross-platform, open-source C++ game engine optimized for 2D games, offering high performance, a lightweight architecture, scene graph system, physics, animations, particle effects, and deployment to mobile, desktop, web, and consoles.
Primary Use Cases
- ▸2D mobile games
- ▸Casual/hypercasual games
- ▸Educational games/apps
- ▸Lightweight 2D engines for indie developers
- ▸Cross-platform 2D desktop or mobile publishing
Notable Features
- ▸High-performance C++ core
- ▸Scene graph architecture
- ▸Sprites + Actions + Transitions
- ▸Box2D & Chipmunk physics
- ▸Cross-platform deployment
Origin & Creator
Cocos2d-x originated from the Python-based Cocos2d project, created by Ricardo Quesada. It was later rewritten in C++ by Chukong Technologies and released in 2010 as a fast cross-platform engine for mobile.
Industrial Note
Cocos2d-x dominates the mobile 2D gaming industry in Asia, powering thousands of games in China, Japan, and Korea - especially hypercasual games, casino games, match-3 titles, and educational products requiring fast rendering.