使用cocos studio界面自适应遇到问题及解决方法

来源:互联网 发布:龙泉驿广电网络全称 编辑:程序博客网 时间:2024/06/07 04:43

如上图用Cocos Studio 3.10 创建的界面,其中两个Hello word 文本框都绑定在 一个 Node节点上,该Node节点限定在左上都20个像素位置。

模拟运行 iphone4 及 iphone5 确实达到了我的效果。

 


发布资源到工程中运行后发现在 iphone4 分辨率情况下 Hello Word 消失了。

经过一番盘查发现在界面加载的时候存在一定问题。

1.通过继承的如下方法来解析获得节点

virtual cocos2d::Node* createNodeWithFlatBuffers(constflatbuffers::Table* nodeOptions) 


2.解析过程会借用 LayoutComponent 这个类进行自适应设置


    voidLayoutComponent::setPositionPercentX(float percentMargin)

    {

        _positionPercentX = percentMargin;


        if (_usingPositionPercentX ||_horizontalEdge == HorizontalEdge::Center)

        {

            Node* parent =this->getOwnerParent();

            if (parent !=nullptr)

            {

                _owner->setPositionX(parent->getContentSize().width * _positionPercentX);

                this->refreshHorizontalMargin();

            }

        }

    }


3.但是在运行到获得父节点返回为空,原因是先解析后 addChild。




那么只要在 node->addChild(child);

在进行一次设置即可。

加载过程会调用如下代码 

    LayoutComponent*LayoutComponent::bindLayoutComponent(Node* node)

    {

        LayoutComponent * layout = (LayoutComponent*)node->getComponent(__LAYOUT_COMPONENT_NAME);

        if (layout !=nullptr)

            return layout;


        layout = new (std::nothrow)LayoutComponent();

        if (layout && layout->init())

        {

            layout->autorelease();

            node->addComponent(layout);

            return layout;

        }

        CC_SAFE_DELETE(layout);

        returnnullptr;

    }


Component* Node::getComponent(conststd::string& name)

{

    if (_componentContainer)

        return_componentContainer->get(name);

    

    returnnullptr;

}


此可见其数据还储存在 _componentContainer

最终如下修改 


使用过程中场景文件的根节点都必须设为 sceneroot,然后对其大小进行根据winSize大小一次设定。


阅读全文
0 0
原创粉丝点击