首页 > 代码库 > cocos2dx tiledmap 碰撞检测

cocos2dx tiledmap 碰撞检测

bool HelloWorld::init(){    //////////////////////////////    // 1. super init first    if ( !CCLayer::init() )    {        return false;    }        CCSize winSize = CCDirector::sharedDirector()->getVisibleSize();    CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();    map=CCTMXTiledMap::create("1.tmx");    addChild(map);    CCTMXObjectGroup* objgroup=map->objectGroupNamed("objLayer");    CCDictionary* dic=objgroup->objectNamed("hero");    float x=dic->valueForKey("x")->floatValue();    float y=dic->valueForKey("y")->floatValue();    float id=dic->valueForKey("id")->floatValue();    const char* idstr=dic->valueForKey("id")->getCString();    CCLOG("%f %f %f",x,y,id);    CCLOG(idstr);    hero=CCSprite::create("hero.png");    hero->setPosition(ccp(x,y));    addChild(hero);        CCMoveBy *move=CCMoveBy::create(2,ccp(winSize.width/2,-winSize.height/2));    hero->runAction(move);    schedule(schedule_selector(HelloWorld::judeCollion));    return true;}void HelloWorld::judeCollion(float dt){    if(hero!=NULL)    {        CCTMXLayer* collionLayer=map->layerNamed("collionLayer");        CCPoint tilePos=positionToTid(hero->getPosition());        if(collionLayer->tileGIDAt(tilePos))        {            CCLOG("collion");            collionLayer->removeTileAt(tilePos);        }    }    }CCPoint HelloWorld::positionToTid(CCPoint position){    int x = position.x /map->getTileSize().width;    int y = (map->getMapSize().height*map->getTileSize().height         - position.y) /map->getTileSize().height;    return ccp(x, y);}

 

cocos2dx tiledmap 碰撞检测