Could somebody show me how to detect collision? Are there any examples? Really Appreciate!


#1

I’m developing a tiledmap strategy game, but i get stuck on collision detection for weeks. I looked up some cocos2d-x examples, but i’m not really family with those languages, I tried so many times and still can’t find the way to detect collision. My sprites move from one tile to another, if there’s no enemy or wall in front of them.

Could some one show me a example, really really appreciate!


#2

My sprites move from one tile to another, if there’s no enemy or wall in front of them

cant you do a check before you move your sprite


#3

yes, my problem is “how to check whether there’s A WALL in front of it”. The wall is generate by Tiled Map Editor, it’s tileset has a property “collidable:true”. But i dont know how to get this value. It’s tiledmap collision detection that I dont know how to handle. Could you please show me how? A little showcase would be wonderful, Thanks!


#4

We are porting Cocos2d-html5 to -x v2.0 now. And the new version will support the feature of reading tile map property.

The feature will be added today, and you may check it out in github before this Friday.


#5

Probably a bit late for you, but for anyone else looking for this. Reads a tile from a tile map layer:-

The code below returns the CCSprite of that tile, or NULL if there is no tile there.

Some of the variables used:

CCTMXLayer *m_Layer; 
CCSize m_cszMapInPoints;                    // Size of map in points
CCSize m_cszMapInTiles;                     // Size of map in Tiles
m_Layer = m_Map->layerNamed("Layer 1");
m_cszMapInPoints = m_Layer->getContentSize();
m_cszMapInTiles = m_Layer->getLayerSize();

The function:

CCSprite* GetTileAtScreenPoint( CCPoint posScreen )
{
    // In map format (no conversion to other corrds)
    CCNode *node = getChildByTag(kTagTileMap);
    m_ptCurrentPos = node->getPosition();
    CCPoint posCur;
    posCur.x = -m_ptCurrentPos.x;
    posCur.y = -m_ptCurrentPos.y;
    CCPoint posMap = ccpAdd( posCur, posScreen ); 
    int iPosX = (posMap.x / m_iTileWidth);
    int iPosY = m_szMapWH.height-(posMap.y / m_iTileHeight);

    if( iPosX>=0 && iPosY>=0 && iPosX < m_szMapWH.width && iPosY < m_szMapWH.height )
        return m_Layer->tileAt( ccp(iPosX,iPosY) );

    return NULL;

}