Search code examples
cocos2d-x

Cocos2dx. Conditional action


I want to create player moving using actions. I created function void move(float x, float y); That function creates actions RotateTo and MoveTo. I want to move player only if angle between its forward and target - forward is less that some value; I tried to add runAction(moveAction) in if condition in update function with bool lock variable but any call of runAction inside update fails with SIGSEGV signal;

Here are fields of my class derived from Node:

cocos2d::Action* moveAction;
cocos2d::Action* rotationAction;

In move function:

void GameObject::move(float x, float y) {
  startPosition = getPosition();
  endPosition   = Vec2(x, y);

  stopAllActions();

  rotationAction = RotateTo::create(
      angleBetweenForwardAndTarget(endPosition) / rotateSpeed,
      -Mathf::normalizeAngle(Mathf::getAngle(lookAtPosition(endPosition)))
  );
  runAction(rotationAction);

  moveAction = MoveTo::create(
      endPosition.distance(startPosition) / moveSpeed,
      Vec2(x, y)
  );
  runAction(moveAction);
}

But now rotation and translation occurs in the same time How to implement it?


Solution

  • I transferred this question here http://discuss.cocos2d-x.org/t/run-action-on-condition/42201