I'm trying to calculate the speed of my sprites in Cocos2d but I'm having some difficulties. I'm not sure which units of time/speed cocos2d uses.

My background sprite moves 2 positions to left each .004 seconds. I have a sprite that is generate each 3 seconds at position X0. The sprite moves until X1. I would like that his speed would be the same as the background.

This is how I'm scheduling the background movement and the other sprite generation:

this.schedule(this.gameLogic, 3);

this is how I'm changing the position of the background

var X = bg.getPositionX();
      var X1 = bg1.getPositionX();
      bg.setPosition(X-2, 0);
      bg1.setPosition(X1-2, 0);

      if ( X < -bg.getBoundingBox().width) {
        bg.setPosition(bg1.getPositionX() + bg1.getBoundingBox().width, bg.getPositionY() );

      if ( X1 < -bg1.getBoundingBox().width) {
        bg1.setPosition(bg.getPositionX() + bg.getBoundingBox().width, bg1.getPositionY() );

And this is how I'm determining the X0, X1 and speed of sprite.

star.setPosition(winSize.width + star.getContentSize().width/2, Y);
        var from = winSize.width + star.getContentSize().width/2;
        var to = -(winSize.width+ star.getContentSize().width/2);

        // Create the actions
        //var actionMove = cc.MoveTo.create(2, cc.p(-star.getContentSize().width/2, Y)); // 3
        var actionMove = cc.MoveTo.create(14, cc.p(to, Y));

How can I determine the speed of star to be the same as bg?

  • \$\begingroup\$ So your bg moves 2 pixels every .004 seconds and your star moves... uh, half its width (?) every second, then you could convert them to pixels per second and compare. Is that it? Am I missing something obvious? \$\endgroup\$ – Anko Apr 16 '14 at 22:09
  • \$\begingroup\$ Yes, the star moving half its width is a comment from the tutorial that I got, if you check the code again in the next line, it is the code without comments, which moves to the variable to and Y \$\endgroup\$ – Filipe Ferminiano Apr 16 '14 at 22:23

There are a number of things to ask, but if you understand the basics you should be able to not just determine the speed your sprite is moving but dictate it.

Unless you are overtaxing the processor, or you schedule a routine to "update" less often, the game "loop" is going to execute 60 times per second. Assuming the routine that moves your sprite is being executed with each loop, then the number of pixels you move the sprite multiplied by 60 is the speed that your sprite moves in a second.

Now if you are using Cocos2d actions, such as "move to", where you must indicate a duration for the action, then the number of points between the starting and ending position divided by the duration is the speed that your sprite will move each second.

In short, you determine the speed that you want your sprite to move, not Cocos2d.

Based on your code, you appear to want your background sprite to move 2 pixels every .004 seconds. That is extremely fast, as it equates to 15,000 pixels per second. I can only assume this is not what you are intending.

In any event, have your game loop execute 60 times per second, or at whatever interval works for you, and move your sprite the number of pixels per iteration as desired.

I hope this helps.


Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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