使用本教程 http://www.raywenderlich.com/33752/cocos2d-x-tutorial-for-ios-and-android-space-game 对于cocos2d-x,我不能从继承中调用CCParallaxNode()构造函数。
继承在其构造函数中调用CCParallaxNode的构造函数
HEADER:
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()时,我在这里做错了什么?
感谢。
答案 0 :(得分:0)
好的,我按照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));
}
}
}
现在在&#34; HelloWorldScene.cpp&#34;或者您的GAME_SCENE替换
//Create the CCParallaxNode
_backgroundNode = ParallaxNodeExtras::node();
使用以下行
//Create the CCParallaxNode
_backgroundNode = ParallaxNodeExtras::create();
以及更新功能中的以下行
_backgroundNode->incrementOffset(Vec2(2000,0),background);
这一行
_backgroundNode->updatePosition();
P.S。你们中的一些人可能需要调整偏移值。
快乐的编码! 如果这可以解决您的问题,请投票:)