【2014-01-21】cocos2dx学习笔记:常用代码备份
[历史归档]本文原发布于 cstriker1407.info 个人博客内容为历史存档仅供参考。发布时间2014-01-21 标题cocos2dx学习笔记常用代码备份分类编程 / C C / cocos2dx 标签cocos2dxcocos2dx学习笔记常用代码备份常用代码备份效果淡入淡出菜单图像菜单文字菜单矩形点常用矩形点的宏动画缓存动画创建动画使用时间获取当前秒最近项目中用到了cocos2dx来开发一个基于陀螺仪的demo忙了半个星期一边学习一边开发总算搞了出来。这里备份下开发中学习到的一些cocos2dx知识。常用代码备份效果淡入淡出CCFadeIn*fadeInCCFadeIn::create(1.5f);CCFadeOut*fadeOutCCFadeOut::create(1.5f);CCSequence*seqCCSequence::createWithTwoActions(fadeIn,fadeOut);CCRepeatForever*repCCRepeatForever::create(seq);sprite-runAction(rep);菜单图像菜单CCMenuItemImage*pCatchBtnItemCCMenuItemImage::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, its an autorelease objectpCatchBtnMenuCCMenu::create(pCatchBtnItem,NULL);pCatchBtnMenu-setPosition(CCPointZero);this-addChild(pCatchBtnMenu,2);文字菜单CCLabelTTF*singlePersonlabelCCLabelTTF::create( ,Arial,200);CCMenuItemLabel*pSinglePersonMenuItemCCMenuItemLabel::create(singlePersonlabel,this,menu_selector(XXX::showBeforeYouGame));pSinglePersonMenuItem-setPosition(ccp(0,0));pSinglePersonMenuItem-setAnchorPoint(ccp(0,0));CCMenu*pMenuCCMenu::create(pSinglePersonMenuItem,NULL);pMenu-setPosition(CCPointZero);beforeyougameSp-addChild(pMenu,5);矩形点常用矩形点的宏CCRect scopeCCRectMake(x,y,size.width,size.height);CCPoint pointccp(x,y);ccpAdd();ccpSub();//判断点是否在矩形内if(scope.containsPoint(targetPos)){//。。。}动画缓存动画创建CCSpriteFrameCache*cacheCCSpriteFrameCache::sharedSpriteFrameCache();cache-addSpriteFramesWithFile(background/animation.plist);charstr[64]{0};CCAnimation*pShootCCAnimation::create();pShoot-setDelayPerUnit(0.1f);pShoot-setRestoreOriginalFrame(true);for(inti0;i5;i){sprintf(str,shoot%d.png,i);pShoot-addSpriteFrame(cache-spriteFrameByName(str));}CCAnimationCache::sharedAnimationCache()-addAnimation(pShoot,shoot);动画使用CCAnimation*pACCAnimationCache::sharedAnimationCache()-animationByName(shoot);CCSprite*spCCSprite::create();sp-setPosition(ccp(pTelescope-getContentSize().width/2,pTelescope-getContentSize().height/2));pTelescope-addChild(sp);sp-runAction(CCAnimate::create(pA));时间获取当前秒longXX::getCurrSeconds(){structcc_timevaltv;CCTime::gettimeofdayCocos2d(tv,NULL);returntv.tv_sectv.tv_usec/1000000;}