前面写了那么多,现在就是用前面信息的时候,把Level中的关卡显示出来。
首先,需要有一个关卡类的引用,这样才能得到我们想要的信息。
然后,需要一个Vector来存储RoleSprite。
代码:
GameLayer.h
private: Level * m_pLevel;//<关卡数据的引用 cocos2d::Vector<RoleSprite*> m_pRoleSpriteVec ;//<存储角色精灵
GameLayer.cpp
#include "RoleSprite.h" #include "data/Role.h" #include "data/Level.h" GameLayer::GameLayer() :m_pLevel(nullptr) { m_pLevel = Level::s_levelVec.at(0) ; CC_SAFE_RETAIN(m_pLevel) ; } GameLayer::~GameLayer() { CC_SAFE_RELEASE_NULL(m_pLevel) ; } <p>bool GameLayer::init() { Size size = Director::getInstance()->getWinSize(); auto bgSprite = Sprite::create("main_bg0.png") ; bgSprite->setPosition(size.width / 2,size.height / 2) ; addChild(bgSprite);</p><p>initMap() ;</p><p>return true ; }</p>void GameLayer::initMap() { Size size = Director::getInstance()->getWinSize(); float x = 0 ; float y = 0 ; float paddingLeft = 10.0f ; float paddingBottom = 53.0f ; for (unsigned i = 0 ; i < m_pLevel->getRoleID().size() ;i++) { Role * pRole = Role::s_roleVec.at(m_pLevel->getRoleID().at(i)) ; Sprite * pp = Sprite::create(pRole->getImageName()->getCString()); int col = m_pLevel->getRolePos().at(i).x ; int row = m_pLevel->getRolePos().at(i).y ; x = paddingLeft + col * 1.0f * 75.0f ; y = paddingBottom + row * 1.0f * 75.0f ; RoleSprite * pRoleSprite = RoleSprite::create(pRole,row,col,Rect(x,y,pp->getContentSize().width,pp->getContentSize().height),this,this->getLocalZOrder()) ; m_pRoleSpriteVec.pushBack(pRoleSprite) ; } }
运行: