将旧项目基于cocos2dx 2.x的调试绘制转移到cocos2dx 3.x中

前端之家收集整理的这篇文章主要介绍了将旧项目基于cocos2dx 2.x的调试绘制转移到cocos2dx 3.x中前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
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,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,flags);
	renderer->addCommand(&_customCommand);
}


2、修改新的绘制方式之后,能看到画出的图形了,不过在移动的过程中,图形并没有发生变化,这是因为没有应用新的变换矩阵。在3.x中,绘制图形推荐使用CCDrawNode,所以改用CCDrawNode进行绘制

先将drawNode节点添加到父节点中

_drawNode = CCDrawNode::create();
addChild(_drawNode);


在draw前需要先clear

_drawNode->clear();

_drawNode->clear();
	for (int i = 0; i < terrain->getWidth(); ++i)
	{
		GridPosition down = GridPosition(i,GridUtil::sharedGridUtil().gridEdgeToPix(up).y);
		_drawNode->drawLine(downPoint,upPoint,ccColor4F(1.0f,0.5f));
		//ccDrawLine(downPoint,upPoint);
	}
原文链接:https://www.f2er.com/cocos2dx/340214.html

猜你在找的Cocos2d-x相关文章