CC_BREAK_IF(! CCLayer::init());
count=0;
_tiled=CCTMXTiledMap::create("tiled.tmx");
this->addChild(_tiled);
//獲取英雄的對象層,再根據層獲取某英雄的對象
CCTMXObjectGroup *object =_tiled->objectGroupNamed("hero");
CCDictionary *spawnPoint=object->objectNamed("hero1");
winsize =CCDirector::sharedDirector()->getWinSize();
int px=spawnPoint->valueForKey("x")->floatValue();
int py=spawnPoint->valueForKey("y")->floatValue();
//CCLog("x=%f,y=%f",px,py);
_player=CCSprite::create("Player.png");
CC_BREAK_IF(!_player);
_player->setAnchorPoint(CCPoint(0,0));
_player->setPosition(ccp(px,py));
//獲取所有敵人位置,再其中創建敵人
object=_tiled->objectGroupNamed("enemy");
for(int i=0;i<int(object->getObjects()->count());i++)
{
CCDictionary *enemy=(CCDictionary *)object->getObjects()->objectAtIndex(i);
if(enemy->valueForKey("n")->intValue()==1)
{
CCSprite *s=CCSprite::create("enemy.png");
float x=enemy->valueForKey("x")->floatValue();
float y=enemy->valueForKey("y")->floatValue();
s->setPosition(ccp(x,y));
s->setAnchorPoint(CCPoint(0,0));
_tiled->addChild(s,4);
CCActionInterval *move=CCMoveBy::create(2,CCPoint(_player->getPosition().x-s->getPosition().x>0?10:-10,_player->getPosition().y-s->getPosition().y>0?10:-10));//
CCFiniteTimeAction *func=CCCallFuncN::create(this,callfuncN_selector(HelloWorld::goon));
s->runAction(CCSequence::create(move,func,NULL));
}
}
代碼下載地址 :http://download.csdn.net/detail/five50/6829985