Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Basic Drawing and Animation in Cocos2d-x

Tech May 12 2

Displayinng text

Use CCLabelTTF for text rendering.

Steps:
1. Initialize: CCLabelTTF* label = CCLabelTTF::create("Hello Cocos2d-x", "Arial", 24);
2. Validate: CC_BREAK_IF(!label);
3. Position: CCSize winSize = CCDirector::sharedDirector()->getWinSize();
   label->setPosition(ccp(winSize.width / 2, winSize.height - 50));
4. Add to scene: this->addChild(label, 1);

Rendering a image

Use CCSprite for images.

Steps:
1. Initialize: CCSprite* sprite = CCSprite::create("HelloWorld.png");
2. Validate: CC_BREAK_IF(!sprite);
3. Position: CCSize winSize = CCDirector::sharedDirector()->getWinSize();
   sprite->setPosition(ccp(winSize.width / 2, winSize.height / 2));
4. Add to scene: this->addChild(sprite, 1);

Creating interactive buttons

Use CCMenu with CCMenuItem subclasses for button management.

Text-based options:
- CCLabelTTF + CCMenuItemLabel
- CCMenuItemFont

Image-based option:
- CCMenuItemImage

Using CCLabelTTF with CCMenuItemLabel

// Create label
CCLabelTTF* btnLabel = CCLabelTTF::create("Click Me", "Arial", 20);
CC_BREAK_IF(!btnLabel);

// Wrap in menu item
CCMenuItemLabel* menuItem = CCMenuItemLabel::create(btnLabel);
CC_BREAK_IF(!menuItem);
menuItem->setPosition(ccp(100, 100));

// Group into menu
CCMenu* menu = CCMenu::create(menuItem, nullptr);
CC_BREAK_IF(!menu);
menu->setPosition(CCPointZero);
this->addChild(menu, 1);

Using CCMenuItemFont for text buttons

// Direct font-based button creation
CCMenuItemFont* fontItem = CCMenuItemFont::create("Submit", this, menu_selector(YourClass::onButtonPressed));
CC_BREAK_IF(!fontItem);
fontItem->setPosition(ccp(300, 300));

CCMenu* menu = CCMenu::create(fontItem, nullptr);
CC_BREAK_IF(!menu);
menu->setPosition(CCPointZero);
this->addChild(menu, 1);

Using CCMenuItemImage for image buttons

// Image-based button with normal and selected states
CCMenuItemImage* imgItem = CCMenuItemImage::create(
    "CloseNormal.png",
    "CloseSelected.png",
    this,
    menu_selector(YourClass::onImageButtonPressed)
);
CC_BREAK_IF(!imgItem);
imgItem->setPosition(ccp(300, 200));

CCMenu* menu = CCMenu::create(imgItem, nullptr);
CC_BREAK_IF(!menu);
menu->setPosition(CCPointZero);
this->addChild(menu, 1);

Creating a sprite animation

Prepare frames from a sprite sheet:
1. Load texture: CCTexture2D* texture = CCTextureCache::sharedTextureCache()->addImage("animation_sheet.png");
2. Extract each frame: CCSpriteFrame* frame = CCSpriteFrame::createWithTexture(texture, CCRectMake(x, y, width, height));
3. Store all frames: CCArray* frames = CCArray::createWithCapacity(frameCount);
4. Build animation: CCAnimation* anim = CCAnimation::createWithSpriteFrames(frames, delayPerFrame);
5. Initialize sprite using first frame: CCSprite* sprite = CCSprite::createWithSpriteFrame(firstFrame);
6. Generate action: CCAnimate* animate = CCAnimate::create(anim);
7. Apply to sprite: sprite->runAction(CCRepeatForever::create(animate));

Cocos2d-x 2.0 code example

CCTexture2D* texture = CCTextureCache::sharedTextureCache()->addImage("gril.png");
CCSpriteFrame* frame0 = CCSpriteFrame::create(texture, CCRectMake(0, 0, 32, 48));
CCSpriteFrame* frame1 = CCSpriteFrame::create(texture, CCRectMake(32, 0, 32, 48));
CCSpriteFrame* frame2 = CCSpriteFrame::create(texture, CCRectMake(64, 0, 32, 48));
CCSpriteFrame* frame3 = CCSpriteFrame::create(texture, CCRectMake(96, 0, 32, 48));

CCArray* frameArray = CCArray::create(4);
frameArray->addObject(frame0);
frameArray->addObject(frame1);
frameArray->addObject(frame2);
frameArray->addObject(frame3);

CCAnimation* animation = CCAnimation::create(frameArray, 0.5f);
CC_BREAK_IF(!animation);

CCSprite* girlSprite = CCSprite::create(frame0);
CC_BREAK_IF(!girlSprite);
CCSize winSize = CCDirector::sharedDirector()->getWinSize();
girlSprite->setPosition(ccp(winSize.width / 2, winSize.height / 2 + 100));
this->addChild(girlSprite, 2);

CCAnimate* animateAction = CCAnimate::actionWithAnimation(animation);
CC_BREAK_IF(!animateAction);
girlSprite->runAction(CCRepeatForever::create(animateAction));

Cocos2d-x 2.1.1 code example

CCTexture2D* texture = CCTextureCache::sharedTextureCache()->addImage("smailgirl.png");
CCSpriteFrame* frame0 = CCSpriteFrame::createWithTexture(texture, CCRectMake(0, 0, 32, 48));
CCSpriteFrame* frame1 = CCSpriteFrame::createWithTexture(texture, CCRectMake(32, 0, 32, 48));
CCSpriteFrame* frame2 = CCSpriteFrame::createWithTexture(texture, CCRectMake(64, 0, 32, 48));
CCSpriteFrame* frame3 = CCSpriteFrame::createWithTexture(texture, CCRectMake(96, 0, 32, 48));

CCArray* frameArray = CCArray::createWithCapacity(4);
frameArray->addObject(frame0);
frameArray->addObject(frame1);
frameArray->addObject(frame2);
frameArray->addObject(frame3);

CCAnimation* animation = CCAnimation::createWithSpriteFrames(frameArray, 0.5f);
CC_BREAK_IF(!animation);

CCSprite* girlSprite = CCSprite::createWithSpriteFrame(frame0);
CC_BREAK_IF(!girlSprite);
CCSize winSize = CCDirector::sharedDirector()->getWinSize();
girlSprite->setPosition(ccp(winSize.width / 2, winSize.height / 2 + 100));
this->addChild(girlSprite, 2);

CCAnimate* animateAction = CCAnimate::create(animation);
CC_BREAK_IF(!animateAction);
girlSprite->runAction(CCRepeatForever::create(animateAction));

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.