將舊專案基於cocos2dx 2.x的除錯繪製轉移到cocos2dx 3.x中
阿新 • • 發佈:2019-02-04
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中繪製
2、修改新的繪製方式之後,能看到畫出的圖形了,不過在移動的過程中,圖形並沒有發生變化,這是因為沒有應用新的變換矩陣。在3.x中,繪製圖形推薦使用CCDrawNode,所以改用CCDrawNode進行繪製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);
在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); }