diff --git a/cocos/2d/CCParallaxNode.cpp b/cocos/2d/CCParallaxNode.cpp index d5f44a5693..b8c7f1a52f 100644 --- a/cocos/2d/CCParallaxNode.cpp +++ b/cocos/2d/CCParallaxNode.cpp @@ -100,7 +100,7 @@ void ParallaxNode::addChild(Node *child, int z, const Point& ratio, const Point& obj->setChild(child); ccArrayAppendObjectWithResize(_parallaxArray, (Object*)obj); - Point pos = _position; + Point pos = this->absolutePosition(); pos.x = -pos.x + pos.x * ratio.x + offset.x; pos.y = -pos.y + pos.y * ratio.y + offset.y; child->setPosition(pos); diff --git a/samples/Cpp/TestCpp/Classes/ParallaxTest/ParallaxTest.cpp b/samples/Cpp/TestCpp/Classes/ParallaxTest/ParallaxTest.cpp index 994be54d62..06c9cf6157 100644 --- a/samples/Cpp/TestCpp/Classes/ParallaxTest/ParallaxTest.cpp +++ b/samples/Cpp/TestCpp/Classes/ParallaxTest/ParallaxTest.cpp @@ -148,6 +148,85 @@ std::string Parallax2::title() const return "Parallax: drag screen"; } +//------------------------------------------------------------------ +// +// Issue2572 +// +//------------------------------------------------------------------ +Issue2572::Issue2572() +: _preListSize(0) +, _printCount(0) +, _moveTimer(0.0f) +, _addTimer(0.0f) +{ + // create a parallax node, a parent node + _paraNode = ParallaxNode::create(); + addChild(_paraNode, 0, kTagNode); + + this->scheduleUpdate(); +} + +const static float _addChildStep = 1.0f; +const static float _wholeMoveTime = 3.0f; +const static Point _wholeMoveSize = Point(-300, 0); +void Issue2572::update(float dt) +{ + _addTimer += dt; + _moveTimer += dt; + if (_moveTimer >= _wholeMoveTime) { + this->unscheduleUpdate(); + return; + } + + _paraNode->setPosition(_paraNode->getPosition() + _wholeMoveSize * dt / _wholeMoveTime); + + if (_addTimer >= _addChildStep) { + _addTimer = 0.0f; + + auto child = Sprite::create("Images/Icon.png"); + Size viewSize = Director::getInstance()->getVisibleSize(); + Point offset = Point(viewSize.width / 2, viewSize.height/2); + _paraNode->addChild(child, 1, Point( 1, 0 ), offset ); + + _childList.pushBack(child); + } + + // After a child added, output the position of the children 3 times. + // Bug : The first output is much different with the second one & the third one. + if (_childList.size() != _preListSize) { + switch (_printCount) { + case 0: + case 1: + case 2: + log( "--child count-- %zd", _childList.size()); + for (auto obj : _childList) + { + Sprite* obstacle = dynamic_cast( obj ); + log("child size : (%.2f, %.2f)", obstacle->getPositionX(), obstacle->getPositionY()); + } + log("-------------------"); + _printCount++; + break; + case 3: + _preListSize = _childList.size(); + _printCount = 0; + break; + default: + break; + } + } +} + +std::string Issue2572::title() const +{ + return "Issue 2572"; +} + +std::string Issue2572::subtitle() const +{ + return "Look at the output in console"; +} + //------------------------------------------------------------------ // // ParallaxDemo @@ -156,7 +235,7 @@ std::string Parallax2::title() const static int sceneIdx = -1; -#define MAX_LAYER 2 +#define MAX_LAYER 3 Layer* createParallaxTestLayer(int nIndex) { @@ -164,6 +243,7 @@ Layer* createParallaxTestLayer(int nIndex) { case 0: return new Parallax1(); case 1: return new Parallax2(); + case 2: return new Issue2572(); } return NULL; diff --git a/samples/Cpp/TestCpp/Classes/ParallaxTest/ParallaxTest.h b/samples/Cpp/TestCpp/Classes/ParallaxTest/ParallaxTest.h index 1840174406..19d0631a9c 100644 --- a/samples/Cpp/TestCpp/Classes/ParallaxTest/ParallaxTest.h +++ b/samples/Cpp/TestCpp/Classes/ParallaxTest/ParallaxTest.h @@ -48,6 +48,25 @@ public: virtual std::string title() const override; }; +class Issue2572 : public ParallaxDemo +{ +protected: + ParallaxNode* _paraNode; + float _moveTimer; + float _addTimer; + Vector _childList; + int _preListSize; + int _printCount; + + virtual void update(float dt) override; + +public: + Issue2572(); + + virtual std::string title() const override; + virtual std::string subtitle() const override; +}; + class ParallaxTestScene : public TestScene { public: