地图跟着主角视角滚动

roleSprite

map

 

map:addChild(roleSprite)  //角色加在地图上,

先设置role的位置,再调用下面函数设置地图位置

roleSprite:setPotion()

 

void setSceneScrollPosition() 
{ 
    CCPoint position = roleSprite->getPosition(); 
    CCSize screenSize = CCDirector::sharedDirector()->getWinSize(); 
    CCSize mapSizeInPixel = CCSizeMake(map->getMapSize().width * map->getTileSize().width,  map->getMapSize().height * map->getTileSize().height); 
 
    if (mapSizeInPixel.width>screenSize.width) 
    { 
        float x=position.x-screenSize.width/2.0f; 
        limit(0.0f,x,mapSizeInPixel.width-screenSize.width); 
        map->setPosition(ccp(-x,this->getPosition().y)); 
    } 
 
    if (mapSizeInPixel.height>screenSize.height) 
    { 
        float y=position.y-screenSize.height/2.0f; 
        limit(0.0f,y,mapSizeInPixel.height-screenSize.height); 
        map->setPosition(ccp(this->getPosition().x,-y)); 
    }    
} 

  

//防止滚出地图黑边
template<typename T> inline void limit(T min,T &val, T max) { if (val<min) val=min; else if (val>max) val=max; }

  

posted on 2013-08-23 19:10  110440  阅读(384)  评论(0编辑  收藏  举报

导航