Adding FloatAction to animate any property over time.

This commit is contained in:
Milos Jakovljevic 2015-04-18 14:35:14 +02:00
parent 425b2b62e1
commit a13c519882
4 changed files with 153 additions and 0 deletions

View File

@ -2511,4 +2511,60 @@ void TargetedAction::setForcedTarget(Node* forcedTarget)
} }
} }
// Float Action
FloatAction* FloatAction::create(float duration, float from, float to, FloatActionCallback callback)
{
auto ref = new (std::nothrow) FloatAction();
if (ref && ref->initWithDuration(duration, from, to, callback))
{
ref->autorelease();
return ref;
}
CC_SAFE_DELETE(ref);
return ref;
}
bool FloatAction::initWithDuration(float duration, float from, float to, FloatActionCallback callback)
{
if (ActionInterval::initWithDuration(duration))
{
_from = from;
_to = to;
_callback = callback;
return true;
}
return false;
}
FloatAction* FloatAction::clone() const
{
auto a = new (std::nothrow) FloatAction();
a->initWithDuration(_duration, _from, _to, _callback);
a->autorelease();
return a;
}
void FloatAction::startWithTarget(cocos2d::Node *target)
{
ActionInterval::startWithTarget(target);
_delta = _to - _from;
}
void FloatAction::update(float delta)
{
float value = _to - _delta * (1 - delta);
if (_callback)
{
// report back
_callback(value);
}
}
FloatAction* FloatAction::reverse() const
{
return FloatAction::create(_duration, _to, _from, _callback);
}
NS_CC_END NS_CC_END

View File

@ -1525,6 +1525,54 @@ private:
CC_DISALLOW_COPY_AND_ASSIGN(TargetedAction); CC_DISALLOW_COPY_AND_ASSIGN(TargetedAction);
}; };
/**
* @class FloatAction
* @brief Action used to animate any value in range [from,to] over specified time interval
*/
class CC_DLL FloatAction : public ActionInterval
{
public:
/**
* Callback function used to report back result
*/
typedef std::function<void(float value)> FloatActionCallback;
/**
* Creates FloatAction with specified duration, from value, to value and callback to report back
* results
* @param duration
* @param from value
* @param to value
* @param callback to report back result
*
* @return An autoreleased FloatAction object
*/
static FloatAction* create(float duration, float from, float to, FloatActionCallback callback);
/**
* Overrided ActionInterval methods
*/
void startWithTarget(Node* target) override;
void update(float delta) override;
FloatAction* reverse() const override;
FloatAction* clone() const override;
CC_CONSTRUCTOR_ACCESS:
FloatAction() {};
virtual ~FloatAction() {};
bool initWithDuration(float duration, float from, float to, FloatActionCallback callback);
protected:
float _from;
float _to;
float _delta;
FloatActionCallback _callback;
private:
CC_DISALLOW_COPY_AND_ASSIGN(FloatAction);
};
// end of actions group // end of actions group
/// @} /// @}

View File

@ -86,6 +86,7 @@ ActionsTests::ActionsTests()
ADD_TEST_CASE(Issue1327); ADD_TEST_CASE(Issue1327);
ADD_TEST_CASE(Issue1398); ADD_TEST_CASE(Issue1398);
ADD_TEST_CASE(Issue2599) ADD_TEST_CASE(Issue2599)
ADD_TEST_CASE(ActionFloat);
} }
std::string ActionsDemo::title() const std::string ActionsDemo::title() const
@ -2218,3 +2219,42 @@ std::string ActionRemoveSelf::subtitle() const
{ {
return "Sequence: Move + Rotate + Scale + RemoveSelf"; return "Sequence: Move + Rotate + Scale + RemoveSelf";
} }
//------------------------------------------------------------------
//
// ActionFloat
//
//------------------------------------------------------------------
void ActionFloat::onEnter()
{
ActionsDemo::onEnter();
centerSprites(3);
auto s = Director::getInstance()->getWinSize();
auto actionFloat = FloatAction::create(2.f, 0, 3, [this](float value) {
_tamara->setScale(value);
});
float grossiniY = _grossini->getPositionY();
auto actionFloat1 = FloatAction::create(3.f, grossiniY, grossiniY + 50, [this](float value) {
_grossini->setPositionY(value);
});
auto actionFloat2 = FloatAction::create(3.f, 3, 1, [this] (float value) {
_kathia->cocos2d::Node::setScale(value);
});
_tamara->runAction(actionFloat);
_grossini->runAction(actionFloat1);
_kathia->runAction(actionFloat2);
}
std::string ActionFloat::subtitle() const
{
return "ActionFloat";
}

View File

@ -580,4 +580,13 @@ private:
cocos2d::Vector<Node*> _pausedTargets; cocos2d::Vector<Node*> _pausedTargets;
}; };
class ActionFloat : public ActionsDemo
{
public:
CREATE_FUNC(ActionFloat);
virtual void onEnter() override;
virtual std::string subtitle() const override;
};
#endif #endif