Hi everyone,
I’m newbie to cocos2d-x game development. (sorry, I don’t speak english very well.)
I use keyboard EventListener, and I design if user push Left arrow button, My chracter start moving to left, also when user release left arrow button, my chracter will stop immediately.
So I implemented as follows code, but when I left arrow button released move action is stop immediately, but character’s animate didn’t stop immediately until animation’s all Frame is looped.
void GameScene::onKeyPressed(EventKeyboard::KeyCode keyCode, Event* event_unused)
{
auto sprMan = (Sprite*)this->getChildByTag(TAG_SPRITE_MAN);
auto sprBlock = (Sprite*)this->getChildByTag(TAG_SPRITE_BLOCK);
switch (keyCode)
{
case EventKeyboard::KeyCode::KEY_LEFT_ARROW: // 왼쪽 방향키를 눌렀을 때.
{
auto action_0 = MoveBy::create(0.8, Point(-50, 0));
auto action_1 = (Animate *)Director::getInstance()->getActionManager()->getActionByTag(TAG_ANIMATE_MAN, this);
auto action_2 = Spawn::create(action_0, action_1,NULL);
auto action_3 = RepeatForever::create(action_2);
action_3->setTag(TAG_ACTION_MAN_LMOVE);
sprMan->runAction(action_3);
if (isTurnLeft != true)
{
sprMan->setFlippedX(true);
isTurnLeft = true;
isTurnRight = false;
}
isTurnLeft = true;
isTurnRight = false;
isMove = true;
break;
}
case EventKeyboard::KeyCode::KEY_RIGHT_ARROW: // 오른쪽 방향키를 눌렀을 때.
{
auto action_0 = MoveBy::create(0.8, Point(50, 0)); // 애니메이션 전프레임 반복 주기가 0.8이므로 이동 소요 시간도 똑같이 설정함.
auto action_1 = (Animate *)Director::getInstance()->getActionManager()->getActionByTag(TAG_ANIMATE_MAN, this);;
auto action_2 = Spawn::create(action_0, action_1, NULL);
auto action_3 = RepeatForever::create(action_2);
action_3->setTag(TAG_ACTION_MAN_RMOVE);
sprMan->runAction(action_3);
if (isTurnRight != true)
{
sprMan->setFlippedX(false);
isTurnLeft = false;
isTurnRight = true;
}
isTurnLeft = false;
isTurnRight = true;
isMove = true;
break;
}
}
}
// 키보드 버튼이 떼졌을 때 호출됨
void GameScene::onKeyReleased(EventKeyboard::KeyCode keyCode, Event* event_unused)
{
auto sprMan = (Sprite*)this->getChildByTag(TAG_SPRITE_MAN);
auto sprBlock = (Sprite*)this->getChildByTag(TAG_SPRITE_BLOCK);
switch (keyCode)
{
case EventKeyboard::KeyCode::KEY_LEFT_ARROW: // 왼쪽 방향키 버튼이 떼졌을 때.
{
auto action_0 = (Action*)sprMan->getActionByTag(TAG_ACTION_MAN_LMOVE);
sprMan->getActionManager()->removeAction(action_0);
isMove = false;
break;
}
case EventKeyboard::KeyCode::KEY_RIGHT_ARROW: // 오른쪽 방향키 버튼이 떼졌을 때.
{
auto action_0 = (Action*)sprMan->getActionByTag(TAG_ACTION_MAN_RMOVE);
sprMan->getActionManager()->removeAction(action_0);
isMove = false;
break;
}
}
}
How stop immediately my animate action?
I already tried removeAction, pauseTarget, stop(), …etc.
Please let me know solution, thanks you