How to move sprites smoothly using Schedule(...)? RRS feed

  • Question

  • User101148 posted


    In my app a small sprite moves constantly, which means sprite position changes with every small time. To do so, I use:

    Schedule (LoopAnimation);

    And in LoopAnimation I change sprite coordinates every time the function is called. The issue is that sprite does not move smoothly. Once in a few seconds the function call is delayed for ~200msec, seems to be, so the sprite goes to "pause" state and visually it does not look good. CPU load is almost 0%, the app is not heavy at all and the sprite is small too. I found that in "Debug" mode of the app, it works more smooth, actually. I do not see "pauses" at all, while in "Release" mode I see a lot. Any help is appreciated!

    Thursday, July 30, 2015 9:31 AM

All replies

  • User86376 posted

    What do you exactly do in your LoopAnimation ? Would it be an option to use CCMoveTo or CCBezierTo instead?

    Thursday, July 30, 2015 10:07 AM
  • User101148 posted

    What do you exactly do in your LoopAnimation ?

    Inside it I have something very similar to:

    void LoopAnimation(float frameTimeInSeconds) { ... i++; spriteSign.PositionX += a[i]; spriteSign.PositionY += b[i]; ... }

    So I simply change sprite position every time, by adding to its X and Y coordinates fixed values from predefined array. Again, I have "pauses" not at some specific coordinates or time, but very randomly. So it's definitely not an issue of a[] or b[] values.

    Would it be an option to use CCMoveTo or CCBezierTo instead?

    I haven't tried it yet, will do. But in theory, if it's a problem of LoopAnimation "fickle"calls, then my CCMoveTo will be called in the same scenario and will not help.

    Thursday, July 30, 2015 2:26 PM
  • User68494 posted


    Could something like this work from the Smooth Follow code?

    Friday, July 31, 2015 5:32 AM
  • User101148 posted

    Could something like this work from the Smooth Follow code?

    I tried to study, but failed unfortunately. I can't get the point of EventDispatcher mechanism. I understand that we create an event, which is "sprite needs a move" in our case, by creating of public class SmoothFollowTest : EventDispatcherTest class with some methods (the methods are easy to get). But when the event suppose to happen, and how to setup the event schedule? More, it must happen on a periodic basis, I would even say "more" periodic than by using of Schedule(). Is there any simple example of how to work with such events - to schedule them, create and delete, and how to "connect" them with main application classes?

    Saturday, August 1, 2015 7:57 AM
  • User68494 posted


    There are quite a few specific simple examples here. There are also spread throughout the CocosSharp tests where you can see specific uses.

    Still not exactly sure what you are after if the Smooth Follow code did not help.

    Saturday, August 1, 2015 8:06 AM
  • User101148 posted

    Hello! I'm finally almost happy as I found out the issue, or better say the reason of explained strange (for me) behavior. So I thought that my problem is that method given to Schedule() is not invoking evenly, say with exact constant time between invokes. And it looked so, because in that method I move some sprites, and they don't move smoothly. I commented Schedule() and moved my invoke to CCApplication.Update(), because Update() invoked always, many times per second and basically I made it the point to start my investigation from. Nothing changed, literally, I even thought I forgot to recompile. So it's how I realized my method given to Schedule() is finally invoked from Update(), somehow. And it means Update() is not invoking evenly! Okay, so I decided to debug the app. Ooops!!! In debug mode it works superb! No any delays in my sprites moves! So I started to change my "release" project settings to those values in "debug" mode, in order to find which setting causes the behavior. And I found it:

    This setting marked in red is checked in debug mode and unchecked in release. And it causes my problem. I checked it in release and the problem is gone. Next step is to understand why...

    Wednesday, November 11, 2015 10:31 AM
  • User68494 posted

    You can also try the latest v1.7.0.0 to see if that smooths things out. There was some work done in the revamp that may help with this as well.

    Wednesday, November 11, 2015 4:55 PM
  • User245391 posted

    You can also try with IsNear() method of touch.location that will detect automatically with the surroundings and assign the current touch point to sprite

    Monday, April 29, 2019 1:14 PM
  • User342125 posted

    You could calculate the time between each call instead of relying on it being accurate.

    E.g each time you receive the event

    // Module level
    private DateTime _lastcalltime = DateTime.Now;
    // In your callback event
    TimeSpan expiretime = new Timespan(DateTime.Now - _lastcalltime);
    // Store now as the last time it was called. This is for the next time we get the callback
    _lastcalltime = DateTime.Now; 
    // Now use the milliseconds in the calculated time instead of assuming the call was accurate each time. This should now be an accurate reflection of how much time expired

    HTH Paul

    Thursday, May 2, 2019 12:11 PM