cocos2d-x 小技巧

1.字符串 与 数据结构互转

CCPoint:  CCPointFromString(); {x, y}
CCSize:   CCSizeFromString(); {w, h}
CCRect:  CCSizeFromString(); {x, y, w, h}

 2.plist文件使用,配置文件使用

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>level1</key>
    <dict>
        <key>bg_far_scene</key>
        <dict>
            <key>path</key>
            <string>images/far_scene.png</string>
            <key>pos</key>
            <string>{358, 309}</string>
        </dict>
        <key>bg_near_scene</key>
        <dict>
            <key>path</key>
            <string>images/near_scene.png</string>
            <key>pos</key>
            <string>{360, 100}</string>
        </dict>
    </dict>
</dict>
</plist>
CCDictionary *data = CCDictionarycreateWithContentsOfFile("parm.plist");
data->objectForKey("gravity")

 3.触摸拖动变化

ccTouchesMoved
CCPoint diff = touch->getDelta();

 4.tile map

CCTMXTiledMap *map = CCTMXTiledMap::create("TileMaps/iso-test-vertexz.tmx");
    addChild(map, 0, kTagTileMap);

/* 直接根据位置获取地图上的精灵 */
CCTMXLayer* layer = map->layerNamed("Trees");
    m_tamara = layer->tileAt( ccp(29,29) );//获取的是CCSprite对象
    m_tamara->retain();

/* 设置某个点的GID值,GID数字标识图块对应的图片样子 */
layer->setTileGID(1, ccp( x, y ) );
//遮挡排序
// tile height is 64x32 //斜45度
// map size: 30x30
CCPoint p = m_tamara->getPosition();
p = CC_POINT_POINTS_TO_PIXELS(p);
float newZ = -(p.y+32) /16;
m_tamara->setVertexZ( newZ );

// tile height is 101x81 //正交
// map size: 12x12
CCPoint p = m_tamara->getPosition();
p = CC_POINT_POINTS_TO_PIXELS(p);
m_tamara->setVertexZ( -( (p.y+81) /81) );

//缩小,2秒缩小一半
CCArray* pChildrenArray = map->getChildren();
CCSpriteBatchNode* child = NULL;
CCObject* pObject = NULL;
CCARRAY_FOREACH(pChildrenArray, pObject)
{
        child = (CCSpriteBatchNode*)pObject;

        if(!child)
            break;

        child->getTexture()->setAntiAliasTexParameters();
}
map->runAction( CCScaleBy::create(2, 0.5f) );

 5.给Scene层抹上底色

CCLayerColor* color = CCLayerColor::create( ccc4(64,64,64,255) );
addChild(color, -1);

6.动作移动地图

// move map to the center of the screen
    CCSize ms = map->getMapSize();
    CCSize ts = map->getTileSize();
    map->runAction( CCMoveTo::create(1.0f, ccp( -ms.width * ts.width/2, -ms.height * ts.height/2 )) );

7.动作

CCActionInterval* move = CCMoveBy::create(0.5f, ccp(0,160));
    CCActionInterval* rotate = CCRotateBy::create(2, 360);
    CCActionInterval* scale = CCScaleBy::create(2, 5);
    CCActionInterval* opacity = CCFadeOut::create(2);
    CCActionInterval* fadein = CCFadeIn::create(2);
    CCActionInterval* scaleback = CCScaleTo::create(1, 1);
CCActionInstant* finish = CCCallFuncN::create(this, callfuncN_selector(TMXReadWriteTest::removeSprite));
    CCSequence* seq0 = CCSequence::create(move, rotate, scale, opacity, fadein, scaleback, finish, NULL);

void TMXReadWriteTest::removeSprite(CCNode* sender)
{
    ////----CCLOG("removing tile: %x", sender);
    CCNode* p = ((CCNode*)sender)->getParent();

    if (p)
    {
        p->removeChild((CCNode*)sender, true);
    }    
    
    //////----CCLOG("atlas quantity: %d", p->textureAtlas()->totalQuads());
}

 8.对于对象节点

CCTMXTiledMap* map = (CCTMXTiledMap*) getChildByTag(kTagTileMap);
CCTMXObjectGroup* group = map->objectGroupNamed("Object Group 1");

CCArray* objects = group->getObjects();
    CCDictionary* dict = NULL;
    CCObject* pObj = NULL;
    CCARRAY_FOREACH(objects, pObj)
{
       dict = (CCDictionary*)pObj;
        
        if(!dict)
            break;
        const char* key = "x";
        int x = ((CCString*)dict->objectForKey(key))->intValue();
        key = "y";
        int y = ((CCString*)dict->objectForKey(key))->intValue();
        key = "width";
        int width = ((CCString*)dict->objectForKey(key))->intValue();
        key = "height";
        int height = ((CCString*)dict->objectForKey(key))->intValue();
        
        glLineWidth(3);
        ccDrawLine( ccp((float)x, (float)y), ccp((float)(x+width), (float)y) );
        ccDrawLine( ccp((float)(x+width), (float)y), ccp((float)(x+width), (float)(y+height)) );
        ccDrawLine( ccp((float)(x+width), (float)(y+height)), ccp((float)x, (float)(y+height)) );
        ccDrawLine( ccp((float)x, (float)(y+height)), ccp((float)x, (float)y) );
        //获取对象的名字
        key = "name";
    CCString * str = ((CCString*)dict->objectForKey(key));
        glLineWidth(1);
}

 9、弹框提示

CCMessageBox("this is content","title");

 10、vs中va功能---自动添加头文件查找

工具->选项->键盘

VAssistX.RefactorAddInclude设置快捷键即可

 11、整个游戏速度设置^^!

float multi = 1.0f;
CCDirector::sharedDirector()->getScheduler()->setTimeScale(multi);

12、tile map 绘制的椭圆Object,x,y 表示 坐下交位置 , 中心点 centerX = x + width/2  centerY = y + height/2

原文地址:https://www.cnblogs.com/hewei2012/p/3383693.html