2014년 4월 3일 목요일

Cocos2d-x 함수 사용

Sprite 생성 

CCTexture2D* texture;
texture = CCTextureCache::sharedTextureCache()->addImage("Images/white-512x512.png");
CCSprite* bricks = CCSprite::createWithTexture(texture, CCRectMake(0, 0, 64, 40)); 

2초후에 동작시키기 

CCFiniteTimeAction* action = CCSequence::create(
CCDelayTime::create(2),
CCCallFunc::create(this, callfunc_selector(TestScene3::startGame)),
NULL);
this->runAction(action); 

Touch 동작시키기

virtual void ccTouchesBegan(CCSet *pTouches, CCEvent* event);
virtual void ccTouchesMoved(CCSet *pTouches, CCEvent* event);
virtual void ccTouchesEnded(CCSet *pTouches, CCEvent* event);
virtual void ccTouchesCancelled(CCSet *pTouches, CCEvent* event);

this->setTouchEnabled(true); // 터치 활성화
void TestScene3::ccTouchesBegan(CCSet *pTouches, CCEvent* event){
CCSetIterator it = pTouches->begin();
CCTouch* touch = (CCTouch*)(*it);
CCPoint touchPoint = touch->getLocation();

CCRect rect = paddle->boundingBox();
if (rect.containsPoint(touchPoint)) { // 점을 포함하고 있는지 체크한다.
}

스캐쥴 사용

this->schedule(schedule_selector(TestScene3::gameLogic), 2.0f/60.0f); // 2/60 Interval로 gameLogic을 실행한다. 

 메뉴 처리

CCMenuItemImage *pMenuItem1 = CCMenuItemImage::create( "Images/btn-play-normal.png",
Images/btn-play-selected.png,
this,
menu_selector(HelloWorld::doClick1));
pMenuItem1->setTag(1);
CCMenu* pMenu = CCMenu::create(pMenuItem1, pMenuItem2, pMenuItem3, pMenuItem4, pMenuItem5, NULL);
pMenu->alignItemsVertically();
this->addChild(pMenu);

void HelloWorld::doClick1(CCObject *p){
CCMenuItem *tItem = (CCMenuItem*) p;
switch ( tItem->getTag () ){
if ( (CCSprite*)getChildByTag(1) == NULL ) {
}
}

문자열 출력하기 

CCLabelTTF *label = CCLabelTTF::create("callback 1 called", "Marker Felt", 16);
label->setPosition(ccp(120,160));
label->setColor(ccBLACK);
label->setTag(100);
addChild(label); 

문자열 Atlas 출력하기 

CCLabelAtlas *pLabel3 = CCLabelAtlas::create("1234", "fps_images.png",  16, 24, '.');
pLabel3->setPosition(ccp(size.width / 2, 50));
this->addChild(pLabel3);



화면 전환 

CCScene* pScene = TestScene2::scene();
CCDirector::sharedDirector()->pushScene( createTransition(14, 1, pScene));
CCDirector::sharedDirector()->replaceScene(pScene);
// pushScene, replaceScene 두가지.. 

plist사용하기

CCSpriteFrameCache *cache = CCSpriteFrameCache::sharedSpriteFrameCache();
cache->addSpriteFramesWithFile("animations/sample.plist");

CCArray* animFrames = CCArray::createWithCapacity(15);

char str[100] = {0};
for ( int i=1 ; i< 15 ; i ++){
sprintf(str, "grossini_dance_%02d.png", i);
CCSpriteFrame* frame = cache->spriteFrameByName(str);
animFrames->addObject(frame);
}

CCSprite *pMan= CCSprite::createWithSpriteFrameName("grossini_dance_01.png");
pMan->setPosition( ccp(240, 160));
this->addChild(pMan);

CCAnimation* animation = CCAnimation::createWithSpriteFrames(animFrames, 0.5f);
CCAnimate* animate = CCAnimate::create(animation);
CCAction* rep = CCRepeatForever::create(animate);
pMan->runAction(rep); 

댓글 없음:

댓글 쓰기