From 32d3bf03bd3c475cfe735b984028c9575cb166f9 Mon Sep 17 00:00:00 2001 From: andyque Date: Tue, 25 Feb 2014 15:30:54 +0800 Subject: [PATCH] fixed #3877. make fadeIn and fadeOut behaviours correct --- cocos/2d/CCActionInterval.cpp | 69 +++++++++++---- cocos/2d/CCActionInterval.h | 102 ++++++++++++---------- tests/Classes/ActionsTest/ActionsTest.cpp | 6 +- 3 files changed, 109 insertions(+), 68 deletions(-) diff --git a/cocos/2d/CCActionInterval.cpp b/cocos/2d/CCActionInterval.cpp index 293dcdb471..56a2c5e364 100644 --- a/cocos/2d/CCActionInterval.cpp +++ b/cocos/2d/CCActionInterval.cpp @@ -1664,7 +1664,7 @@ FadeIn* FadeIn::create(float d) { FadeIn* action = new FadeIn(); - action->initWithDuration(d); + action->initWithDuration(d,255.0f); action->autorelease(); return action; @@ -1674,25 +1674,42 @@ FadeIn* FadeIn::clone() const { // no copy constructor auto a = new FadeIn(); - a->initWithDuration(_duration); + a->initWithDuration(_duration,255.0f); a->autorelease(); return a; } -void FadeIn::update(float time) +void FadeIn::setReverseAction(cocos2d::FadeTo *ac) { - if (_target) - { - _target->setOpacity((GLubyte)(255 * time)); - } - /*_target->setOpacity((GLubyte)(255 * time));*/ + _reverseAction = ac; } -ActionInterval* FadeIn::reverse() const + +FadeTo* FadeIn::reverse() const { - return FadeOut::create(_duration); + auto action = FadeOut::create(_duration); + action->setReverseAction(const_cast(this)); + return action; + } +void FadeIn::startWithTarget(cocos2d::Node *target) +{ + ActionInterval::startWithTarget(target); + + if (nullptr != _reverseAction) { + this->_toOpacity = this->_reverseAction->_fromOpacity; + }else{ + _toOpacity = 255.0f; + } + + if (target) { + _fromOpacity = target->getOpacity(); + } +} + + + // // FadeOut // @@ -1701,7 +1718,7 @@ FadeOut* FadeOut::create(float d) { FadeOut* action = new FadeOut(); - action->initWithDuration(d); + action->initWithDuration(d,0.0f); action->autorelease(); return action; @@ -1711,23 +1728,37 @@ FadeOut* FadeOut::clone() const { // no copy constructor auto a = new FadeOut(); - a->initWithDuration(_duration); + a->initWithDuration(_duration,0.0f); a->autorelease(); return a; } -void FadeOut::update(float time) +void FadeOut::startWithTarget(cocos2d::Node *target) { - if (_target) - { - _target->setOpacity(GLubyte(255 * (1 - time))); + ActionInterval::startWithTarget(target); + + if (nullptr != _reverseAction) { + _toOpacity = _reverseAction->_fromOpacity; + }else{ + _toOpacity = 0.0f; + } + + if (target) { + _fromOpacity = target->getOpacity(); } - /*_target->setOpacity(GLubyte(255 * (1 - time)));*/ } -ActionInterval* FadeOut::reverse() const +void FadeOut::setReverseAction(cocos2d::FadeTo *ac) { - return FadeIn::create(_duration); + _reverseAction = ac; +} + + +FadeTo* FadeOut::reverse() const +{ + auto action = FadeIn::create(_duration); + action->setReverseAction(const_cast(this)); + return action; } // diff --git a/cocos/2d/CCActionInterval.h b/cocos/2d/CCActionInterval.h index 8867315496..e5a36d4ec1 100644 --- a/cocos/2d/CCActionInterval.h +++ b/cocos/2d/CCActionInterval.h @@ -707,53 +707,6 @@ private: CC_DISALLOW_COPY_AND_ASSIGN(Blink); }; -/** @brief Fades In an object that implements the RGBAProtocol protocol. It modifies the opacity from 0 to 255. - The "reverse" of this action is FadeOut - */ -class CC_DLL FadeIn : public ActionInterval -{ -public: - /** creates the action */ - static FadeIn* create(float d); - - // - // Overrides - // - virtual void update(float time) override; - virtual FadeIn* clone() const override; - virtual ActionInterval* reverse(void) const override; - -protected: - FadeIn() {} - virtual ~FadeIn() {} - -private: - CC_DISALLOW_COPY_AND_ASSIGN(FadeIn); -}; - -/** @brief Fades Out an object that implements the RGBAProtocol protocol. It modifies the opacity from 255 to 0. - The "reverse" of this action is FadeIn -*/ -class CC_DLL FadeOut : public ActionInterval -{ -public: - /** creates the action */ - static FadeOut* create(float d); - - // - // Overrides - // - virtual void update(float time) override; - virtual FadeOut* clone() const override; - virtual ActionInterval* reverse(void) const override; - -protected: - FadeOut() {} - virtual ~FadeOut() {} - -private: - CC_DISALLOW_COPY_AND_ASSIGN(FadeOut); -}; /** @brief Fades an object that implements the RGBAProtocol protocol. It modifies the opacity from the current value to a custom one. @warning This action doesn't support "reverse" @@ -780,11 +733,64 @@ protected: GLubyte _toOpacity; GLubyte _fromOpacity; - + friend class FadeOut; + friend class FadeIn; private: CC_DISALLOW_COPY_AND_ASSIGN(FadeTo); }; +/** @brief Fades In an object that implements the RGBAProtocol protocol. It modifies the opacity from 0 to 255. + The "reverse" of this action is FadeOut + */ +class CC_DLL FadeIn : public FadeTo +{ +public: + /** creates the action */ + static FadeIn* create(float d); + + // + // Overrides + // + virtual void startWithTarget(Node *target) override; + virtual FadeIn* clone() const override; + virtual FadeTo* reverse(void) const override; + + void setReverseAction(FadeTo* ac); + +protected: + FadeIn():_reverseAction(nullptr) {} + virtual ~FadeIn() {} + +private: + CC_DISALLOW_COPY_AND_ASSIGN(FadeIn); + FadeTo* _reverseAction; +}; + +/** @brief Fades Out an object that implements the RGBAProtocol protocol. It modifies the opacity from 255 to 0. + The "reverse" of this action is FadeIn +*/ +class CC_DLL FadeOut : public FadeTo +{ +public: + /** creates the action */ + static FadeOut* create(float d); + + // + // Overrides + // + virtual void startWithTarget(Node *target) override; + virtual FadeOut* clone() const override; + virtual FadeTo* reverse(void) const override; + + void setReverseAction(FadeTo* ac); + +protected: + FadeOut():_reverseAction(nullptr) {} + virtual ~FadeOut() {} +private: + CC_DISALLOW_COPY_AND_ASSIGN(FadeOut); + FadeTo* _reverseAction; +}; /** @brief Tints a Node that implements the NodeRGB protocol from current tint to a custom one. @warning This action doesn't support "reverse" @since v0.7.2 diff --git a/tests/Classes/ActionsTest/ActionsTest.cpp b/tests/Classes/ActionsTest/ActionsTest.cpp index fd13d0d53f..e0fddfec18 100644 --- a/tests/Classes/ActionsTest/ActionsTest.cpp +++ b/tests/Classes/ActionsTest/ActionsTest.cpp @@ -647,9 +647,13 @@ void ActionFade::onEnter() auto action2 = FadeOut::create(1.0f); auto action2Back = action2->reverse(); + auto action2BackReverse = action2Back->reverse(); + auto action2BackReverseReverse = action2BackReverse->reverse(); + _tamara->setOpacity(122); _tamara->runAction( Sequence::create( action1, action1Back, NULL)); - _kathia->runAction( Sequence::create( action2, action2Back, NULL)); + _kathia->setOpacity(122); + _kathia->runAction( Sequence::create( action2, action2Back,action2BackReverse,action2BackReverseReverse, NULL)); } std::string ActionFade::subtitle() const