С++ cocos2d-x не может вызвать защищенный конструктор CCParallaxNode

Использование руководства на http://www.raywenderlich.com/33752/cocos2d-x-tutorial-for-ios-and-android-space-game для cocos2d-x я не могу вызвать конструктор CCParallaxNode() из наследования.

Наследование вызывает в своем конструкторе конструктор CCParallaxNode.

ЗАГОЛОВОК:

class CCParallaxNodeExtras : public CCParallaxNode {

    public :

    // Need to provide a constructor
    CCParallaxNodeExtras();

ОПРЕДЕЛЕНИЕ КОНСТРУКТОРА

#include "CCParallaxNodeExtras.h" 

// Need to provide a constructor
CCParallaxNodeExtras::CCParallaxNodeExtras() {
    CCParallaxNode(); // call parent constructor: Cannot be called!
}

Ошибка:

/home/cocos2d-x-3.0rc0/tests/MyGame/proj.android/../cocos2d/cocos/2d/CCParallaxNode.h: In constructor 'CCParallaxNodeExtras::CCParallaxNodeExtras()':
/home/cocos2d-x-3.0rc0/tests/MyGame/proj.android/../cocos2d/cocos/2d/CCParallaxNode.h:78:5: error: 'cocos2d::ParallaxNode::ParallaxNode()' is protected
     ParallaxNode();

Кажется, что CCParallaxNode() не может вызвать ParallaxNode(), потому что он защищен. Что я делаю неправильно при вызове CCParallaxNode()?

Спасибо.


person user2212461    schedule 28.03.2014    source источник
comment
В С++ конструктор по умолчанию вызывается унаследованным конструктором по умолчанию... поэтому он должен быть просто CCParallaxNodeExtras::CCParallaxNodeExtras() { }, он может вызвать эту ошибку   -  person SBKarr    schedule 28.03.2014
comment
Затем я получаю ошибки позже в коде, говорящие, что CCParallaxNode не инициализирован...   -  person user2212461    schedule 29.03.2014


Ответы (1)


Хорошо, я следовал руководству Рэя и нашел решение ошибки parallaxNode.

ОБНОВЛЕНО ДЛЯ ВЕРСИИ COCOS2DX-3.X

В ParallaxNodeExtras.h сделайте следующее:

#include "cocos2d.h"

USING_NS_CC;

class ParallaxNodeExtras : public ParallaxNode {
    public :
    static ParallaxNodeExtras* create();
    void updatePosition();
} ;

Теперь переключитесь на ParallaxNodeExtras.cpp и сделайте следующее.

#include "ParallaxNodeExtras.h"

class PointObject : public Ref
{
public:
    inline void setRation(Point ratio) {_ratio = ratio;}
    inline void setOffset(Point offset) {_offset = offset;}
    inline void setChild(Node *var) {_child = var;}
    inline Point getOffset() const {return _offset;}
    inline Node* getChild() const {return _child;}
private:
    Point _ratio;
    Point _offset;
    Node* _child;
};


ParallaxNodeExtras* ParallaxNodeExtras::create()
{
    // Create an instance of InfiniteParallaxNode
    ParallaxNodeExtras* node = new ParallaxNodeExtras();
    if(node) {
        // Add it to autorelease pool
        node->autorelease();
    } else {
        // Otherwise delete
        delete node;
        node = 0;
    }
    return node;
}

void ParallaxNodeExtras::updatePosition()
{
    int safeOffset = -10;
    // Get visible size
    Size visibleSize = Director::getInstance()->getVisibleSize();
    // 1. For each child of an parallax node
    for(int i = 0; i < _children.size(); i++)
    {
        auto node = _children.at(i);
        // 2. We check whether it is out of the left side of the visible area
       auto offsetx = convertToWorldSpace(node->getPosition()).x;
       auto offsetw = node->getContentSize().width;
       int offsetxw = offsetx + offsetw;
        if(offsetxw < safeOffset)
            // 3. Find PointObject that corresponds to current node
            for(int i = 0; i < _parallaxArray->num; i++)
            {
                auto po = (PointObject*)_parallaxArray->arr[i];
                // If yes increase its current offset on the value of visible width
                if(po->getChild() == node)
                    po->setOffset(po->getOffset() +
                                  Point(visibleSize.width + node->getContentSize().width,0));
            }
    }
}

Теперь в "HelloWorldScene.cpp" или в вашей GAME_SCENE замените

//Create the CCParallaxNode
    _backgroundNode = ParallaxNodeExtras::node();

со следующей строкой

//Create the CCParallaxNode
    _backgroundNode = ParallaxNodeExtras::create();

и следующая строка в функции обновления

_backgroundNode->incrementOffset(Vec2(2000,0),background);

с этой строкой

_backgroundNode->updatePosition();

P.S. Некоторым из вас может потребоваться настроить значения смещения.

Удачного кодирования! Пожалуйста, проголосуйте, если это решит вашу проблему :)

person LeoSarena    schedule 12.10.2014