How to animate a tile?

How to animate a tile?
0.0 0


I tried to animate a tile by using the tile’s CCSprite.After calling CCTMXLayer::TileAt() and adding some animation, when I called CCTMXLayer::removeTileAt(position),not only the tile at position, some other tiles also disappeared from the screen. These tiles are not removed from the layer but can’t be shown.Besides,those animated tile seems strange. I’m confused by this. Is it a bug or I did it wrong? My cocos2d-x version is 2.1.1.

My codes:

bool Floor::processTileInfo(int gid, int row, int col)
    CCDictionary* dic = m_map->propertiesForGID(gid);
    bool ifAnim = dic->valueForKey("anim")->boolValue();

    if (ifAnim)     //has animation
        bool animRow = animRow = dic->valueForKey("animRow")->boolValue();

        if (animRow)
            CCSprite* sp = m_layer->tileAt(ccp(col, row));
            animateTile(gid, sp);
    return true;

bool Floor::animateTile( int gid, CCSprite* sprite )
    //init texture
    CCTexture2D* tex = CCTextureCache::sharedTextureCache()->addImage("monsters.png");

    //because there is only one tile set image, get the positions by gid
    gid -= 1;       //make gid begin with 0
    int beginRow = gid / TILE_SET_WIDTH;
    int beginCol = gid - beginRow * TILE_SET_WIDTH;
    int beginX = beginCol * TILE_WIDTH;
    int beginY = beginRow * TILE_WIDTH;

    //init frames array
    CCArray* sprFrames = NULL;
    sprFrames = CCArray::createWithCapacity(4);

    for (int index = 0; index < 4; index++)
        CCSpriteFrame* frame = NULL;
        int x = beginX + index * TILE_WIDTH;
        frame = CCSpriteFrame::createWithTexture(tex, 
            CCRect(x, beginY, TILE_WIDTH, TILE_WIDTH));

    //init animation
    CCAnimation* playerAnim = NULL;
    playerAnim = CCAnimation::createWithSpriteFrames(sprFrames, 0.2f);

    //init action
    CCRepeatForever* act  = NULL;
    act  = CCRepeatForever::create(CCAnimate::create(playerAnim));

    return true;

//call this function cause problem
void Floor::removeObjAt( CCPoint pos )
    pos.y = m_size.height - pos.y - 1;
    CCSprite* sprite = m_layer->tileAt(pos); //problem sometime won't show without this call,but I didn't do anything with sprite

未标题-1 拷贝.jpg (62.0 KB)