使用Cocos2d-x和C++,我試圖播放和暫停一個精靈的動畫。在Cocos2d上的雪碧暫停/恢復動作/動畫
我使用的是Cocos2dx的3.15.1版本。
我有一個叫做PlayerSprite
的類,它是從cocos2d::Sprite
類衍生出來的。裏面PlayerSprite
初始化,我設置我的動畫用下面的代碼:
SpriteBatchNode* playerSpriteBatch = SpriteBatchNode::create("player.png");
SpriteFrameCache* spriteFrameCache = SpriteFrameCache::getInstance();
spriteFrameCache->addSpriteFramesWithFile("player.plist");
Vector<SpriteFrame*> animFrames(2);
char str[18] = { 0 };
for (int i = 1; i < 3; i++) {
sprintf(str, "player_idle_%d.png", i);
SpriteFrame* frame = spriteFrameCache->getSpriteFrameByName(str);
animFrames.pushBack(frame);
}
Animation* idleAnim = Animation::createWithSpriteFrames(animFrames, 0.8f);
self->idleAction = self->runAction(RepeatForever::create(Animate::create(idleAnim)));
self->idleAction->setTag(0);
當我運行的代碼,它工作正常和正確的動畫循環。
在我的void update()
方法中,我試圖根據玩家正在移動或閒置的天氣來暫停/播放動作/動畫。
我做這個用下面的代碼:
const bool isIdleActionRunning = this->getNumberOfRunningActionsByTag(0) > 0 ? true : false;
const bool isMoving = !vel.isZero();
if (!isMoving && !isIdleActionRunning) {
// Player is idle AND animation is not running
// Run animation
this->runAction(idleAction);
} else if (isMoving && isIdleActionRunning) {
// Player is moving but animation is running
// Pause animation
this->stopActionByTag(0);
}
當我現在運行這段代碼,我的性格下降,只要他打gound,我在this->runAction(idleAction);
得到一個錯誤說:
訪問衝突讀取位置0xDDDDDDE5
我相信這是起因於this->stopActionByTag(0)
刪除ACTI在指針上。我試圖克隆這個行爲來避免這種情況,但沒有成功。
我想你誤解了,我認爲這種方法不適用於動畫 – Acidic
你必須在Speed中嵌入Animate :: create。我認爲它應該有效,因爲Animate也是Action。 – Makalele