【问题标题】:C++ cocos2d-x cannot call protected CCParallaxNode constructorC++ cocos2d-x 无法调用受保护的 CCParallaxNode 构造函数
【发布时间】:2014-05-08 00:31:42
【问题描述】:

使用教程 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() 时我在这里做错了什么?

谢谢。

【问题讨论】:

  • 在c++中,默认构造函数是由继承的构造函数默认调用的...所以,它应该只是CCParallaxNodeExtras::CCParallaxNodeExtras() { },它会产生这个错误
  • 然后我在代码中出现错误,说 CCParallaxNode 未初始化...

标签: android c++ inheritance cocos2d-x protected


【解决方案1】:

好的,我按照Ray的教程找到了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();

附:你们中的一些人可能需要调整偏移值。

编码愉快! 如果这解决了您的问题,请投票:)

【讨论】:

    猜你喜欢
    • 2012-06-04
    • 2011-05-30
    • 2021-10-05
    • 2011-05-20
    • 2016-04-07
    • 1970-01-01
    • 2014-11-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多