mirror of https://github.com/axmolengine/axmol.git
Merge pull request #11491 from milos1290/tween
Adding FloatAction to animate any property over time.
This commit is contained in:
commit
fb112995d8
|
@ -2511,4 +2511,60 @@ void TargetedAction::setForcedTarget(Node* forcedTarget)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ActionFloat
|
||||||
|
|
||||||
|
ActionFloat* ActionFloat::create(float duration, float from, float to, ActionFloatCallback callback)
|
||||||
|
{
|
||||||
|
auto ref = new (std::nothrow) ActionFloat();
|
||||||
|
if (ref && ref->initWithDuration(duration, from, to, callback))
|
||||||
|
{
|
||||||
|
ref->autorelease();
|
||||||
|
return ref;
|
||||||
|
}
|
||||||
|
CC_SAFE_DELETE(ref);
|
||||||
|
return ref;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ActionFloat::initWithDuration(float duration, float from, float to, ActionFloatCallback callback)
|
||||||
|
{
|
||||||
|
if (ActionInterval::initWithDuration(duration))
|
||||||
|
{
|
||||||
|
_from = from;
|
||||||
|
_to = to;
|
||||||
|
_callback = callback;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
ActionFloat* ActionFloat::clone() const
|
||||||
|
{
|
||||||
|
auto a = new (std::nothrow) ActionFloat();
|
||||||
|
a->initWithDuration(_duration, _from, _to, _callback);
|
||||||
|
a->autorelease();
|
||||||
|
return a;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ActionFloat::startWithTarget(Node *target)
|
||||||
|
{
|
||||||
|
ActionInterval::startWithTarget(target);
|
||||||
|
_delta = _to - _from;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ActionFloat::update(float delta)
|
||||||
|
{
|
||||||
|
float value = _to - _delta * (1 - delta);
|
||||||
|
|
||||||
|
if (_callback)
|
||||||
|
{
|
||||||
|
// report back value to caller
|
||||||
|
_callback(value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ActionFloat* ActionFloat::reverse() const
|
||||||
|
{
|
||||||
|
return ActionFloat::create(_duration, _to, _from, _callback);
|
||||||
|
}
|
||||||
|
|
||||||
NS_CC_END
|
NS_CC_END
|
||||||
|
|
|
@ -1525,6 +1525,58 @@ private:
|
||||||
CC_DISALLOW_COPY_AND_ASSIGN(TargetedAction);
|
CC_DISALLOW_COPY_AND_ASSIGN(TargetedAction);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @class ActionFloat
|
||||||
|
* @brief Action used to animate any value in range [from,to] over specified time interval
|
||||||
|
*/
|
||||||
|
class CC_DLL ActionFloat : public ActionInterval
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
/**
|
||||||
|
* Callback function used to report back result
|
||||||
|
*/
|
||||||
|
typedef std::function<void(float value)> ActionFloatCallback;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates FloatAction with specified duration, from value, to value and callback to report back
|
||||||
|
* results
|
||||||
|
* @param duration of the action
|
||||||
|
* @param from value to start from
|
||||||
|
* @param to value to be at the end of the action
|
||||||
|
* @param callback to report back result
|
||||||
|
*
|
||||||
|
* @return An autoreleased ActionFloat object
|
||||||
|
*/
|
||||||
|
static ActionFloat* create(float duration, float from, float to, ActionFloatCallback callback);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Overrided ActionInterval methods
|
||||||
|
*/
|
||||||
|
void startWithTarget(Node* target) override;
|
||||||
|
void update(float delta) override;
|
||||||
|
ActionFloat* reverse() const override;
|
||||||
|
ActionFloat* clone() const override;
|
||||||
|
|
||||||
|
CC_CONSTRUCTOR_ACCESS:
|
||||||
|
ActionFloat() {};
|
||||||
|
virtual ~ActionFloat() {};
|
||||||
|
|
||||||
|
bool initWithDuration(float duration, float from, float to, ActionFloatCallback callback);
|
||||||
|
|
||||||
|
protected:
|
||||||
|
/* From value */
|
||||||
|
float _from;
|
||||||
|
/* To value */
|
||||||
|
float _to;
|
||||||
|
/* delta time */
|
||||||
|
float _delta;
|
||||||
|
|
||||||
|
/* Callback to report back results */
|
||||||
|
ActionFloatCallback _callback;
|
||||||
|
private:
|
||||||
|
CC_DISALLOW_COPY_AND_ASSIGN(ActionFloat);
|
||||||
|
};
|
||||||
|
|
||||||
// end of actions group
|
// end of actions group
|
||||||
/// @}
|
/// @}
|
||||||
|
|
||||||
|
|
|
@ -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(ActionFloatTest);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string ActionsDemo::title() const
|
std::string ActionsDemo::title() const
|
||||||
|
@ -2218,3 +2219,43 @@ std::string ActionRemoveSelf::subtitle() const
|
||||||
{
|
{
|
||||||
return "Sequence: Move + Rotate + Scale + RemoveSelf";
|
return "Sequence: Move + Rotate + Scale + RemoveSelf";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//------------------------------------------------------------------
|
||||||
|
//
|
||||||
|
// ActionFloat
|
||||||
|
//
|
||||||
|
//------------------------------------------------------------------
|
||||||
|
void ActionFloatTest::onEnter()
|
||||||
|
{
|
||||||
|
ActionsDemo::onEnter();
|
||||||
|
|
||||||
|
centerSprites(3);
|
||||||
|
|
||||||
|
auto s = Director::getInstance()->getWinSize();
|
||||||
|
|
||||||
|
// create float action with duration and from to value, using lambda function we can easly animate any property of the Node.
|
||||||
|
auto actionFloat = ActionFloat::create(2.f, 0, 3, [this](float value) {
|
||||||
|
_tamara->setScale(value);
|
||||||
|
});
|
||||||
|
|
||||||
|
float grossiniY = _grossini->getPositionY();
|
||||||
|
|
||||||
|
auto actionFloat1 = ActionFloat::create(3.f, grossiniY, grossiniY + 50, [this](float value) {
|
||||||
|
_grossini->setPositionY(value);
|
||||||
|
});
|
||||||
|
|
||||||
|
auto actionFloat2 = ActionFloat::create(3.f, 3, 1, [this] (float value) {
|
||||||
|
_kathia->setScale(value);
|
||||||
|
});
|
||||||
|
|
||||||
|
_tamara->runAction(actionFloat);
|
||||||
|
_grossini->runAction(actionFloat1);
|
||||||
|
_kathia->runAction(actionFloat2);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string ActionFloatTest::subtitle() const
|
||||||
|
{
|
||||||
|
return "ActionFloat";
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -580,4 +580,13 @@ private:
|
||||||
cocos2d::Vector<Node*> _pausedTargets;
|
cocos2d::Vector<Node*> _pausedTargets;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class ActionFloatTest : public ActionsDemo
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
CREATE_FUNC(ActionFloatTest);
|
||||||
|
|
||||||
|
virtual void onEnter() override;
|
||||||
|
virtual std::string subtitle() const override;
|
||||||
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Reference in New Issue