1、首先必须修改原先在draw函数中绘制渲染的方式。3.x不是直接调用draw函数进行绘制的,而是通过renderCommand进行延时渲染。
老项目的渲染方式-draw函数中调用
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS || CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
glLineWidth(2.0f);
//glEnable(GL_LINE_WIDTH);
#else
glLineWidth(2.0f);
glEnable(GL_LINE_SMOOTH);
#endif
ccDrawColor4F(1.0f, 1.0f, 1.0f, 0.5f);
BattleTerrain *terrain = BattleScene::getCurrentScene()->getTerrain();
if(!terrain) return;
for (int i = 0; i < terrain->getWidth(); ++i)
{
GridPosition down = GridPosition(i, 0);
GridPosition up = GridPosition(i, BattleScene::getCurrentScene()->getTerrain()->getHeight());
CCPoint downPoint = ccp(GridUtil::sharedGridUtil().gridEdgeToPix(down).x,
GridUtil::sharedGridUtil().gridEdgeToPix(down).y);
CCPoint upPoint = ccp(GridUtil::sharedGridUtil().gridEdgeToPix(up).x,
GridUtil::sharedGridUtil().gridEdgeToPix(up).y);
ccDrawLine(downPoint, upPoint);
}
新项目的方式,重写新的draw函数,在onDraw中绘制
void SceneLayer::draw(Renderer *renderer, const Mat4& transform, uint32_t flags)
{
_customCommand.init(_globalZOrder, transform, flags);
_customCommand.func = CC_CALLBACK_0(SceneLayer::onDraw, this, transform, flags);
renderer->addCommand(&_customCommand);
}
先将drawNode节点添加到父节点中
_drawNode = CCDrawNode::create();
addChild(_drawNode);
_drawNode->clear();
_drawNode->clear();
for (int i = 0; i < terrain->getWidth(); ++i)
{
GridPosition down = GridPosition(i, 0);
GridPosition up = GridPosition(i, BattleScene::getCurrentScene()->getTerrain()->getHeight());
CCPoint downPoint = ccp(GridUtil::sharedGridUtil().gridEdgeToPix(down).x,
GridUtil::sharedGridUtil().gridEdgeToPix(down).y);
CCPoint upPoint = ccp(GridUtil::sharedGridUtil().gridEdgeToPix(up).x,
GridUtil::sharedGridUtil().gridEdgeToPix(up).y);
_drawNode->drawLine(downPoint, upPoint, ccColor4F(1.0f, 1.0f, 1.0f, 0.5f));
//ccDrawLine(downPoint, upPoint);
}