Improve ActionCoroutine callback style (#1481)

This commit is contained in:
Lich 2023-12-08 13:46:46 +09:00 committed by GitHub
parent e77b54b785
commit 0f114baf8c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 14 additions and 13 deletions

View File

@ -80,10 +80,10 @@ bool Coroutine::moveNext() const
//
// ActionCoroutine
//
ActionCoroutine* ActionCoroutine::create(Coroutine&& coroutine)
ActionCoroutine* ActionCoroutine::create(const std::function<Coroutine()>& function)
{
auto ret = new (std::nothrow) ActionCoroutine();
if (ret && ret->initWithCoroutine(std::forward<Coroutine>(coroutine)))
if (ret && ret->initWithCoroutine(function))
{
ret->autorelease();
return ret;
@ -92,6 +92,12 @@ ActionCoroutine* ActionCoroutine::create(Coroutine&& coroutine)
return nullptr;
}
bool ActionCoroutine::initWithCoroutine(const std::function<Coroutine()>& function) noexcept
{
_coroutine = std::forward<Coroutine>(function());
return true;
}
bool ActionCoroutine::isDone() const
{
auto action = _coroutine.currentAction();
@ -112,10 +118,4 @@ void ActionCoroutine::step(float dt)
_coroutine.moveNext();
}
bool ActionCoroutine::initWithCoroutine(Coroutine&& coroutine) noexcept
{
_coroutine = std::forward<Coroutine>(coroutine);
return true;
}
NS_AX_END

View File

@ -128,7 +128,7 @@ public:
* @return A pointer of ActionCoroutine. If creation failed, return nil.
* @lua NA
*/
static ActionCoroutine* create(Coroutine&& coroutine);
static ActionCoroutine* create(const std::function<Coroutine()>& function);
//
// Overrides
@ -143,7 +143,7 @@ public:
/** initializes the action with the Coroutine
* @lua NA
*/
bool initWithCoroutine(Coroutine&& coroutine) noexcept;
bool initWithCoroutine(const std::function<Coroutine()>& function) noexcept;
protected:
/** Coroutine */

3
tests/.gitignore vendored
View File

@ -1 +1,2 @@
build-*/
build-*/
build_*/

View File

@ -2428,7 +2428,7 @@ void ActionCoroutineTest::onEnter()
_frameCount = 1;
centerSprites(0);
auto action = ActionCoroutine::create(coroutineCallback());
auto action = ActionCoroutine::create(AX_CALLBACK_0(ActionCoroutineTest::coroutineCallback, this));
this->runAction(action);
auto s = Director::getInstance()->getWinSize();
@ -2443,7 +2443,7 @@ void ActionCoroutineTest::onEnter()
void ActionCoroutineTest::update(float delta)
{
_frameCount++;
_label->setString(StringUtils::format("frame count : %" PRIu64 ")", _frameCount));
_label->setString(StringUtils::format("frame count : %" PRIu64 "", _frameCount));
}
std::string ActionCoroutineTest::title() const