【问题标题】:Why it is so hard to draw a red rectangle Sprite on white background Layer with cocos2d-x?为什么用 cocos2d-x 在白色背景图层上绘制红色矩形 Sprite 这么难?
【发布时间】:2014-02-16 07:43:15
【问题描述】:

该死的!我想通过 cocos2d-x 做世界上最简单的事情,但我遇到了问题。我需要一个红色矩形上的白色层。首先,我发现为了给必须从cocos2d::LayerColorLayerColor::initWithColor(Color4B(255, 255, 255, 255)) 派生的图层着色,然后我了解到,为了绘制一个矩形,我应该像这样覆盖 draw 方法:

void HelloWorld::draw()
{
DrawPrimitives::setDrawColor4F(1.0f, 0.0f, 0.0f, 1.0f);
DrawPrimitives::drawRect(Point(100,100), Point(400,400));
}

这些东西是分开工作的,但它们不能一起工作。我想当我覆盖绘图时,所有绘图都会减少到这两个函数调用,因此我的图层背景变为黑色。这个最简单的事情的解决方案在哪里?

编辑:

我曾尝试在这样的覆盖中调用LayerColor::draw

void HelloWorld::draw()
{
    LayerColor::draw();
DrawPrimitives::setDrawColor4F(1.0f, 0.0f, 0.0f, 1.0f);
DrawPrimitives::drawRect(Point(100,100), Point(400,400));
}

没有帮助。尝试从 sprite 派生并 addChild 到图层,如下所示:

class BoardView : public Sprite 
{
public:
    BoardView() : Sprite() 
    {

    }

    virtual void draw() override 
    {
        DrawPrimitives::setDrawColor4F(1.0f, 0.0f, 0.0f, 1.0f);
        DrawPrimitives::drawRect(Point(100,100), Point(400,400));
    }
};

但这也没有用!我怎么能做这个最简单的事情?我错过了什么吗?

【问题讨论】:

    标签: c++ cocos2d-x cocos2d-x-3.0


    【解决方案1】:

    我在这里找到了 Beta 版的测试: http://www.cocos2d-x.org/reference/native-cpp/V3.0alpha0/d1/d89/namespacecocos2d_1_1_draw_primitives.html#a196155c1b3410485d0b77379acf22e64

    我创建了一个简单的测试类来测试它:

    #ifndef __ccxtest__DrawTest__
    #define __ccxtest__DrawTest__
    
    #include <iostream>
    #include "cocos2d.h"
    
    class DrawTestLayer : public cocos2d::Layer
    {
    public:
        CREATE_FUNC(DrawTestLayer);
    
    protected:
        void draw();
    };
    
    #endif /* defined(__ccxtest__DrawTest__) */
    

    以及实现:

    #include "DrawTest.h"
    
    USING_NS_CC;
    
    void DrawTestLayer::draw()
    {
    glLineWidth(1);
    DrawPrimitives::setDrawColor4B(255,255,255,255);
    DrawPrimitives::setPointSize(1);
    
    // Anti-Aliased
    glEnable(GL_LINE_SMOOTH);
    
    // filled poly
    glLineWidth(1);
    Point filledVertices[] = { Point(10,120), Point(50,120), Point(50,170), Point(25,200), Point(10,170) };
    DrawPrimitives::drawSolidPoly(filledVertices, 5, Color4F(0.5f, 0.5f, 1, 1 ) );
    }
    

    终于在父场景/层的init中:

    auto myLayer = DrawTestLayer::create();
    this->addChild(myLayer);
    

    结果是一个 5 点多边形。我在一个通用的 hello world 项目上对此进行了测试,背景图像、颜色、字体等都显示出来了。

    编辑:如何显示/隐藏绘图:

    void DrawTestLayer::draw()
    {
        if ( drawFlagBoolean ) // controlled by timer or scheduled action
        {
            // draw code here
            // every frame primitives are drawn
        }
    }
    

    【讨论】:

    • 有 3 个问题: 1) 如何在彩色图层上绘图,这是最初的问题? 2) 如何删除其中一个绘制的形状? 3)当你画的时候你addChild这条线还是一个矩形到this
    • 1) 我相信覆盖 LayerColor 中的绘制命令会取消图元的绘制。最好添加另一个层或节点,因为对性能的影响很小。 2) 使用动作序列或计时器来控制使绘制代码在括号中执行的布尔值。不执行代码时,形状将消失。请参阅上面的编辑。 3)不,你不添加孩子。
    • 谢谢,这完全解释了事情! :)
    猜你喜欢
    • 2015-08-07
    • 2017-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-20
    • 2012-07-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多