最近项目中用到了cocos2dx来开发一个基于陀螺仪的demo,忙了半个星期,一边学习一边开发,总算搞了出来。这里备份下开发中学习到的一些cocos2dx知识。
CONTENTS
常用代码备份:
效果:
淡入淡出:
CCFadeIn *fadeIn = CCFadeIn::create(1.5f); CCFadeOut *fadeOut = CCFadeOut::create(1.5f); CCSequence *seq = CCSequence::createWithTwoActions(fadeIn, fadeOut); CCRepeatForever *rep = CCRepeatForever::create(seq); sprite->runAction(rep);
菜单:
图像菜单:
CCMenuItemImage *pCatchBtnItem = CCMenuItemImage::create( "catchA.png", "catchB.png", this, menu_selector(XXX::menuCallback)); pCatchBtnItem->setAnchorPoint(ccp(0.5,0.5)); pCatchBtnItem->setPosition(pSensorPoint->getPosition()); pCatchBtnItem->setTag(10); // create menu, it's an autorelease object pCatchBtnMenu = CCMenu::create(pCatchBtnItem, NULL); pCatchBtnMenu->setPosition(CCPointZero); this->addChild(pCatchBtnMenu, 2);
文字菜单:
CCLabelTTF* singlePersonlabel = CCLabelTTF::create(" ", "Arial",200); CCMenuItemLabel* pSinglePersonMenuItem = CCMenuItemLabel::create(singlePersonlabel, this, menu_selector(XXX::showBeforeYouGame)); pSinglePersonMenuItem->setPosition(ccp(0,0)); pSinglePersonMenuItem->setAnchorPoint(ccp(0,0)); CCMenu* pMenu = CCMenu::create(pSinglePersonMenuItem, NULL); pMenu->setPosition(CCPointZero); beforeyougameSp->addChild(pMenu, 5);
矩形,点:
常用矩形,点的宏:
CCRect scope = CCRectMake(x, y,size.width,size.height);
CCPoint point = ccp(x,y);
ccpAdd();
ccpSub();
//判断点是否在矩形内 if (scope.containsPoint(targetPos)) { //。。。 }
动画缓存:
动画创建:
CCSpriteFrameCache *cache = CCSpriteFrameCache::sharedSpriteFrameCache(); cache->addSpriteFramesWithFile("background/animation.plist"); char str[64] = {0}; CCAnimation *pShoot = CCAnimation::create(); pShoot->setDelayPerUnit(0.1f); pShoot->setRestoreOriginalFrame(true); for (int i = 0; i < 5; i++) { sprintf(str, "shoot%d.png", i); pShoot->addSpriteFrame(cache->spriteFrameByName(str)); } CCAnimationCache::sharedAnimationCache()->addAnimation(pShoot,"shoot");
动画使用:
CCAnimation *pA = CCAnimationCache::sharedAnimationCache()->animationByName("shoot"); CCSprite *sp = CCSprite::create(); sp->setPosition(ccp(pTelescope->getContentSize().width/2, pTelescope->getContentSize().height/2)); pTelescope->addChild(sp); sp->runAction(CCAnimate::create(pA));
时间:
获取当前秒:
long XX::getCurrSeconds() { struct cc_timeval tv; CCTime::gettimeofdayCocos2d(&tv,NULL); return tv.tv_sec + tv.tv_usec / 1000000; }
发表评论