//====生成create方法===== CREATE_FUNC(TableViewTestLayer); 这个方法要调用init方法,如果要做初始化,可以覆盖父类的init,如下: bool TableViewTestLayer::init() { if ( !CCLayer::init() ) { return false; } .... return true; } //===快速格式化String=== CCString::createWithFormat("<CCNode | Tag = %d>", m_nTag)->getCString(); //其他方法: #include<stringstream> std::stringstream ss; ss << "hello, world " << 123; ss.str().c_str(); //还有: spritf //===For Each的用法=== CCARRAY_FOREACH(m_pChildren, child) { CCNode* pNode = (CCNode*) child; if(pNode && pNode->m_nTag == aTag) return pNode; }
//===记录日志=== CCLOG("cocos2d: removeChildByTag: child not found!"); CCLOG("format", xx); // 带格式化的 //===对象引用操作=== CC_SAFE_RETAIN(actionManager); CC_SAFE_RELEASE(m_pActionManager); //===CCAssert=== CCAssert( action != NULL, "Argument must be non-nil"); //===弧度角度制转化=== float radiansX = -CC_DEGREES_TO_RADIANS(m_fRotationX); CC_RADIANS_TO_DEGREES
//===随机数=== CCRANDOM_0_1() ===〉 [0,1] CCRANDOM_MINUS1_1() ===〉[-1, 1] 这里请参考头文件:ccMacros.h //====设置位置使用VisibleRect类==== item1->setPosition(ccp(VisibleRect::center().x - item2->getContentSize().width*2, VisibleRect::bottom().y+item2->getContentSize().height/2)); //===spriteBatch=== CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize(); CCSpriteFrameCache* cache = CCSpriteFrameCache::sharedSpriteFrameCache(); cache->addSpriteFramesWithFile("lbs.plist"); CCSpriteBatchNode* batch = CCSpriteBatchNode::create("lbs.png", 50); addChild(batch, 1, 1); CCSprite* sprite1 = CCSprite::createWithSpriteFrameName("battleShip01.png"); sprite1->setPosition(ccp( visibleSize.width/2, visibleSize.height/2)); sprite1->setScale(2.5f); batch->addChild(sprite1, 0); //============================= //还可以有另外的写法: //构造函数中: CCSpriteBatchNode* BatchNode = CCSpriteBatchNode::create("Images/grossini_dance_atlas.png", 50); addChild(BatchNode, 0, kTagSpriteBatchNode); //别的函数中: CCSpriteBatchNode* batchNode = (CCSpriteBatchNode*) getChildByTag( kTagSpriteBatchNode ); CCSprite* sprite = CCSprite::createWithTexture(batchNode ->getTexture(), CCRectMake(x,y,85,121)); BatchNode->addChild(sprite); //===Animation=== CCSprite* explode = CCSprite::createWithSpriteFrameName("explosion_a_01.png"); explode->setPosition(ccp( s.width/2 +130, s.height/4 + 200)); batch->addChild(explode, 0); CCArray* animFrames = CCArray::createWithCapacity(16); for(int i=1; i<16; i++) { const char* name = CCString::createWithFormat("explosion_a_%02d.png", i)->getCString(); CCLOG(CCString::createWithFormat("frame is: %s", name)->getCString()); CCSpriteFrame* frame = cache->spriteFrameByName( name ); animFrames->addObject(frame); } CCAnimation* animation = CCAnimation::createWithSpriteFrames(animFrames, 2.f/16.f); CCRepeatForever* action = CCRepeatForever::create(CCAnimate::create(animation)); explode->runAction(action);
//===显示一个标签=== CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize(); CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin(); CCLabelTTF* pLabel = CCLabelTTF::create("Hello World", "Arial", TITLE_FONT_SIZE); pLabel->setPosition(ccp(origin.x + visibleSize.width/2, origin.y + visibleSize.height - pLabel->getContentSize().height)); this->addChild(pLabel, 1);
//===显示一个按钮=== CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize(); CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin(); CCMenuItemImage *pCloseItem = CCMenuItemImage::create( "CloseNormal.png", "CloseSelected.png", this, menu_selector(HelloWorld::menuCloseCallback)); pCloseItem->setPosition(ccp(origin.x + visibleSize.width - pCloseItem->getContentSize().width/2 , origin.y + pCloseItem->getContentSize().height/2)); CCMenu* pMenu = CCMenu::create(pCloseItem, NULL); pMenu->setPosition(CCPointZero); this->addChild(pMenu, 1); //.................. void HelloWorld::menuCloseCallback(CCObject* pSender) { CCDirector::sharedDirector()->end(); } //====自定义update==== schedule( schedule_selector(SpriteBatchNodeColorOpacity::removeAndAddSprite), 2); ... void SpriteBatchNodeColorOpacity::removeAndAddSprite(float dt) {...}