首页 > 代码库 > 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 碰撞检测
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。