Moving a TileMap over time with Collision

Hi.
I am sitting since a few days on the following problem. I want to move a tilemap over time. by the moving process i want to check collision with a player. until now it looks like that: http://www.youtube.com/watch?v=Dv4ZvUHEFdg&feature=youtu.be
The player falls from the sky until he lands on the platform. then the game should begin scrolling. you can see the scrolling works too and the collision also. but everything happens much to fast and i dont know how to slow down it. if i do it with the update for example call the function every 2 seconds then it looks like as it is stottering.
here is my code for the tilemap movement:

`void TileMap::moveTileMap(float dt)
{
//iterate through the tileMap
for(int y = 0; y < _tileMap->getMapSize().height; y++)
{
for(int x = 0; x < _tileMap->getMapSize().width; x++)
{
if(x < _tileMap->getMapSize().width - 1)
{
//set the gid id of the next tile to the actual tile
int gid = colLayer->tileGIDAt(ccp(x + 1, y));
colLayer->setTileGID(gid, ccp(x,y));
colLayer->setTileGID(0, ccp(x + 1, y));
}
}
}
}

void TileMap::update(float dt)
{
t++;

if(t % 100 == 0)
    test = true;
if(test)
    moveTileMap(dt);

}`

_tileMap is a CCTMXTiledMap
colLayer is a CCTMXLayer where the platforms are

and this code is for the collsion:

`void Player::setPlayerPosition(CCPoint position)
{
CCPoint tileCoord = tileCoordForPosition(position);
int tileGid = _map->colLayer->tileGIDAt(tileCoord);

if (tileGid) {
    CCDictionary* tileProperties = _map->_tileMap->propertiesForGID(tileGid);
    if (tileProperties) {
        const char* collision = (const char*) tileProperties->valueForKey("Collidable");
        if (collision && strcmp(collision, "True")) 
        {
            /*if (characterState == kStateFalling) 
            {
                speed = 0;
            }*/
            isGrounded = true;
        }
    }
}
else
{
    CCLog("NOOOOOO");
    isGrounded = false;
}

/*CCSprite* s = (CCSprite*)_map->tileToSprite->objectAtIndex(1);

if(CCRect::CCRectIntersectsRect(player->boundingBox(), s->boundingBox()))
{
    isGrounded = true;
}
else
{
    isGrounded = false;
}*/

player->setPosition(position);

}`

tileCoordForPosition is for calculation between player coordinate system and tilemap coordinate system like it is in the tilemap tutorial.

only problem is the speed of the tilemap movement. do you know how to fix that problem or do you have a other solution. have to get it work until next tuesday so HELP ME PLEASE! :wink:

So you are moving the tiles in the map, one tile block at a time (say 32 points)?

Each tile is actually a sprite, can’t you move the sprite 1 point (pixel) at a time and then when you’ve moved it 32 points, move the actual tile in the map?

I think there’s example code in the Tests that shows you how to get the sprite for the tile in the map and move it.

Anyway, I think you are going to have problems with collisions later on. If you have other things on the screen that need to collide with this moved platform then you are gonna see a “gap” where say the platform has moved 20 points of its 32. You might want to consider using Box2D collision detection (see [[http://www.gmtdev.com/blog/2011/08/19/how-to-use-box2d-for-just-collision-detection-with-cocos2d-x/]] What you could do is for each of the tile sprites, create a Box2D collision box around it. Have a Box2D box around that blokes feet too. Then just detect the blokes feet Box2D box hitting a tile sprites Box2D box. Now when you move the sprites of the tiles, the Box2D boxes move too (automatically as they are linked to the sprite), so you have pixel perfect collision detection and no headaches.

Thanks for your fast answer. i know every tile is a sprite. so i tried to get the sprite like this CCSprite* s = colMap~~>tileAt than i did an array to save all the sprites. but when i safe them in new sprites i can´t move the tiles by themself. so they dont move. there is no method like this:
colMaop~~>tileAt(ccp(x,y))->setPositionX(…)
is it possible for you to give me the example or the projekt in testprojekt where i can find it. or write some code with your example. in my case i havn´t anything else that collides with the platforms. i only have collectables. but therefor i have a cocos2d tutorial which i write to cocos2d-x

How about moving the tmx with CCAction class. I have a demo in testing that uses tmx files;
I load my tmx files then loop through each and find the value that is collidable, create a box2d def/shape etc…
then I let box2d do all the hardwork of figuring out the collisions. I get where it happened and reference that back to
my tmx layer to remove or edit the tile.

Or and easy quick way is to reference the characters position to the tilemap instead of looping through all of the array of sprites. ((player.pos.x+scrollX)/tilesize,player.pos.y/tilesize)
So the player’s position is always relative to the map. Compare positions and if that tile is collidable you can apply your collision function.

thank you for your answer. can you post the code of demo which is relevant for me.
i have a solution too. but it´s not as good as i hoped it would be.