2013년 12월 4일 수요일

자식 Sprite를 포함한 사용자 정의 CCSprite - cocos2d-x


  • 자식 Sprite를 포함하는 사용자 정의 CCSprite를 정의
  •  멤버함수 Showchild를 통해 자식 Sprite의 상태(Position, Visible 등)를 제어


CustomSprite.h
#ifndef __Fortune__CustomSprite__
#define __Fortune__CustomSprite__

#include "Common.h"

class CustomSprite:public CCSprite
{
public:
    CustomSprite();
    ~CustomSprite();
    static CustomSprite* create(CCSpriteBatchNode* batchNode, int Type);
    
    void showBag();
    
private:
    static const char* fileName(int Type);
};

#endif /* defined(__Fortune__CustomSprite__) */

CustomSprite.cpp
#include "CustomSprite.h"

CustomSprite::CustomSprite(){}
CustomSprite::~CustomSprite(){}

CustomSprite* CustomSprite::create(CCSpriteBatchNode* batchNode, int Type)
{
    CCSpriteFrame* frame = CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(fileName(Type));
    
    CustomSprite* custom = new CustomSprite();
    if (custom && custom->initWithSpriteFrame(frame))
    {
        batchNode->addChild(custom, 1);
        
        ///////////////////////////////////////////////////////////////
        // 자식 스프라이트 추가
        ///////////////////////////////////////////////////////////////
        CCSprite* child = CCSprite::createWithSpriteFrameName("child.png");
        child->setTag(1);
        child->setPosition(ccp(142.0f, 75.0f));
        custom->addChild(child, -1); // 자식 스프라이트를 부모 스프라이트의 아래 위치시킴
        child->setVisible(false);    // 자식 스프라이트를 화면에서 숨김
        
        return custom;
    }
    
    CC_SAFE_DELETE(custom);
    return NULL;
}

#pragma mark -
#pragma mark 자식 스프라이트의 visible상태 설정
void CustomSprite::showchild()
{
    this->getChildByTag(1)->setVisible(true);
}
#pragma mark -
#pragma mark 타입을 int로 받아서 char*형 파일 이름을 리턴
const char* CustomSprite::fileName(int Type)
{
    std::string name = "custom_";
    
    std::stringstream type;
    type << Type;
    
    name += type.str();
    name += ".png";
    
    return name.c_str();
}

2013년 11월 29일 금요일

TTF 폰트 추가방법 - ios


1. 사용하고자 하는 .ttf 파일을 프로젝트의 Resources디렉토리에 추가한다.

2. 프로젝트에서 다음 이미지에서와 같이 폰트를 추가한다.


TTF font

3. 추가한 폰트를 CCLabelTTF에서 사용한다.

2013년 11월 25일 월요일

Cocos2d-x IOS 프로젝트에서 Sqlite3사용하기

1. 프로젝트에 sqlite3 라이브러리 추가

2. 미리 생성된 .sqlite(또는 .db)파일을 IOS프로젝트의 Resources디렉토리에 위치시킨다.
3. 초기 구동시 DB를 복사한다.

복사방법 : 



#pragma mark -
#pragma mark Initialize DB (Resource에 있는 sqlite파일을 Document로 복사)
void initializeDB()
{
    NSFileManager* fm  = [NSFileManager defaultManager];
    const char* dbPath = filePath("nih_DB.sqlite");
    
    //[fm removeItemAtPath:[NSString stringWithUTF8String:dbPath] error:NULL];
    
    BOOL fileExists    = [fm fileExistsAtPath:[NSString stringWithUTF8String:dbPath]];
    
    if(!fileExists)
    {
        // 참조 데이터베이스 파일 경로
        NSString* origin = [[NSBundle mainBundle]pathForResource:@"nih_DB" ofType:@"sqlite"];
        
        // Copy DB
        BOOL      copyDB = [fm copyItemAtPath:origin toPath:[NSString stringWithUTF8String:dbPath] error:NULL];
        if(!copyDB)
        {
            printf("Copy failed!");
        }
    }
}

#pragma mark -
#pragma mark File Path
const char* filePath(char* _name)
{
    NSString *name      = [NSString stringWithUTF8String:_name];
    NSArray *paths      = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *path      = [paths objectAtIndex:0];
    NSString *_filePath = [path stringByAppendingPathComponent:name];
    
    return [_filePath UTF8String];
}