实现角色与墙壁的来回碰撞

来源:互联网 发布:巧克力知乎 编辑:程序博客网 时间:2024/04/28 19:50

根据上章的扩展:(这是部分打码)

void JsLayer::update(float dt){CCSize mapTiledNum = _map->getMapSize();  CCSize tiledSize = _map->getTileSize(); CCPoint point1 =this->_jsSprite->getPosition();point1.x=point1.x - this->speed;this->_jsSprite->setPosition(ccp(point1.x,point1.y));int x =this->_jsSprite->getPosition().x/ tiledSize.width;  int y =this->_jsSprite->getPosition().y/ tiledSize.height;  /* Cocos2d-x的默认Y坐标是由下至上的,所以要做一个相减操作 */  y = mapTiledNum.height - y;        int tiledGid=_map->layerNamed("zhangai")->tileGIDAt(ccp(x,y));  int tiledGid2=_map->layerNamed("zhangai2")->tileGIDAt(ccp(x,y));  if(tiledGid!=0)  {       CCDictionary* propertiesDict = _map->propertiesForGID(tiledGid);          /* 取得格子的zhezhao属性值 */           const CCString* prop = propertiesDict->valueForKey("zhezhao");            /* 判断zhezhao属性是否为true,是的话,不让玩家移动 */          if(prop->m_sString.compare("true") == 0) {                //point1.x=point1.x +50;this->isB=true;           }    }  if(isB==true)  {      CCPoint point3 =this->_jsSprite->getPosition();     point3.x =point3.x +speed*2;     this->_jsSprite->setPosition(point3);  }  if(tiledGid2!=0)  {  CCDictionary* propertiesDict = _map->propertiesForGID(tiledGid2);    const CCString* prop = propertiesDict->valueForKey("fanhui");    if(prop->m_sString.compare("true") == 0) {  this->isB=false;  }    }   }




0 0
原创粉丝点击