码迷,mamicode.com
首页 > 其他好文 > 详细

cocos2dx tiledmap 碰撞检测

时间:2014-10-07 20:11:13      阅读:196      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   os   ar   for   sp   div   

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 碰撞检测

标签:style   blog   color   io   os   ar   for   sp   div   

原文地址:http://www.cnblogs.com/yufenghou/p/4009524.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!