c++ - 在 Cocos2dx 中绘制矩形
<p><p>我在 iOS 环境中使用 Cocos2dx 库绘制基本的白色矩形时遇到问题。我已经查看了其他几个实现以获得指导。 </p>
<p> <a href="http://discuss.cocos2d-x.org/t/draw-rectangle-with-drawrect-in-cocos2dx-not-working/14836/3" rel="noreferrer noopener nofollow">http://discuss.cocos2d-x.org/t/draw-rectangle-with-drawrect-in-cocos2dx-not-working/14836/3</a> </p>
<p> <a href="https://stackoverflow.com/questions/29062923/cocos2d-x-how-can-i-draw-a-resizing-rectangle" rel="noreferrer noopener nofollow">Cocos2d-x: How can I draw a resizing rectangle?</a> </p>
<p>基本上看起来我需要使用DrawNode::create()的类方法来制作节点,然后像openGL一样建立顶点,然后用DrawNode->drawPolygon方法绘制它并将子节点添加到cocos2d::Layer 子类的 addChild 方法的场景。</p>
<p>这是我的代码。</p>
<pre><code>bool JFScene::init()
{
if ( !Layer::init() )
{
return false;
}
auto visibleSize = Director::getInstance()->getVisibleSize();
Vec2 origin = Director::getInstance()->getVisibleOrigin();
auto closeItem = MenuItemImage::create(
"CloseNormal.png",
"CloseSelected.png",
CC_CALLBACK_1(JFScene::menuCloseCallback, this));
closeItem->setScale(2.0);
closeItem->setPosition(Vec2(origin.x + visibleSize.width - closeItem->getContentSize().width/2 ,
origin.y + closeItem->getContentSize().height/2));
auto menu = Menu::create(closeItem, NULL);
menu->setPosition(Vec2::ZERO);
this->addChild(menu, 1);
auto CButton = Sprite::create("CButton.png");
CButton->setPosition(
Point(visibleSize.width / 2 + origin.x, visibleSize.height / 2 + origin.y)
);
this->addChild(CButton, 1);
auto rectNode = DrawNode::create();
Vec2 vertices = {
Vec2(-50, -50),
Vec2(50, -50),
Vec2(50, 50),
Vec2(-50, 50)
};
rectNode->drawPolygon(vertices, 4, Color4F::WHITE, 1, Color4F::WHITE);
this->addChild(rectNode);
return true;
}
</code></pre>
<p>奇怪的是,当我在 iPhone 5s 上运行 CButton 节点以及默认文件中包含的关闭项时,它似乎会呈现,但我尝试绘制的白色矩形没有呈现。任何想法为什么?</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>你想画一个矩形……看看这段代码:</p>
<pre><code>auto rectNode = DrawNode::create();
Vec2 rectangle;
rectangle = Vec2(-50, -50);
rectangle = Vec2(50, -50);
rectangle = Vec2(50, 50);
rectangle = Vec2(-50, 50);
Color4F white(1, 1, 1, 1);
rectNode->drawPolygon(rectangle, 4, white, 1, white);
this->addChild(rectNode);
</code></pre>
<p>我希望它对你有用。</p>
<p>引用:<a href="http://discuss.cocos2d-x.org/t/how-to-draw-rect-in-node/16538" rel="noreferrer noopener nofollow">cocso2d-x forum</a> </p>
<hr/>
<p>另外,我建议你看看 <a href="https://stackoverflow.com/questions/19431922/how-to-draw-simple-rect-with-cocos2d-x-v3-0" rel="noreferrer noopener nofollow">this similar question</a>如果你没有。</p></p>
<p style="font-size: 20px;">关于c++ - 在 Cocos2dx 中绘制矩形,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/41670298/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/41670298/
</a>
</p>
页:
[1]