1
\$\begingroup\$

This is my problem: I have a scheduled update, here I track the movements of my objects (sprites), I move them with CCMoveBy, and I need to constantly update the zOrder. For setting the zOrder I've made a setZOrder(), which it takes the actual position of the sprite.

And here is the problem, I get all the X and Y values after the object is in the target. I know I get the values after the object is in the new position because I've made a CCLog. I can read all the values from the sprite, only when it's in the new position, so everything is well sorted only when the objects are not moving.

How can I get the CCMoveBy values on every tick update? (or how can I get the CCMoveBy values in "real-time"?)

Here is an idea of my code.

this->schedule(schedule_selector(Game::update));

void Game::update(float dt)
{
   setZOrder();
   moveObjects();
}

void Game::setZOrder()
{
  //This function takes the X and Y position and the row and column where the sprite is. 
  //Is working good. But I'm getting the "move" action values, after the object is in place.
}

void Game::moveObjects()
{
   for (i=0; i < numChildren; i++)
   {
     CCActionInterval* move = CCMoveBy::create(targetPoint, time);
     object[i]->runAction(move);
   }
}
\$\endgroup\$
1
\$\begingroup\$

If you want to do measures and move your sprite you need to use CCSpawn and CCCallFunc to call function that measures. Or even run some animation handler with scheduleScriptFunc method

|improve this answer|||||
\$\endgroup\$
  • \$\begingroup\$ Finally, I get it working; but with other method. However, I'm interested in your answer. How should I do the CCSpawn? \$\endgroup\$ – Jose M Pan Oct 7 '13 at 15:10
  • \$\begingroup\$ Create two or more CCActions, create CCArray and fill it with created CCActions, create CCSpawn with your array as a param and run through runAction like you did \$\endgroup\$ – Sugar Dec 11 '13 at 12:15
1
\$\begingroup\$

you can use void update(float deltaTime) function.

just active it in initial() function by this->scheduleUpdate() and the override void update(float deltaTime).

bool Game::init()
{
...
moveObjects
this->scheduleUpdate()
...

}

void Game::update(float deltaTime)
{
// Do whatever
setZOrder();
}

void Game::moveObjects()
{
   // Here we move object and when they reach target, move then again , and ...
   // meanwhile in update() we are setting their Z Order
   cocos2d::Vector<FiniteTimeAction*> objectMoveAction;
   for (i=0; i < numChildren; i++)
   {
     ActionInterval* move = MoveBy::create(targetPoint, time);
     objectMoveAction.pushBack(move)

   }
   object[i]->runAction(CCSequnce::create(Spawn::create(objectMoveAction), CallFunN([=](){moveObjects()},nullptr));
}

I din't test above code and it may have syntax error. notice I use lambda expression and is works with cocos2d v3.x .

Good Luck =)

|improve this answer|||||
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.