将旧项目基于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, 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);
}


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, 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);
	}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值