0

In my new game, I have to handle multi touch with cocos2d to move two players at the same time. However, sometimes it looks like the touches are being laggy! When I play, everything is really smooth but then I get a lag out of nowhere with the players' movement, but the other objects move smoothly! So I decided to run profiling, and everything was fine, my game was always running between 56-60 fps, even with the "lag". So I guess it's not a memory, nor FPS issue, but more a touch handling problem... Here is my code :

- (void) ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    for (UITouch *touch in touches) {
        tapCount ++;

        CGPoint location = [touch locationInView:[touch view]];
        location = [[CCDirector sharedDirector] convertToGL:location];

        //Do my stuff here...

        NSLog(@"Tap Count:%d", tapCount);
    }
}

- (void) ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    if (tapCount == 0) return;

    for (UITouch *touch in touches) {
        CGPoint location = [touch locationInView:[touch view]];
        location = [[CCDirector sharedDirector] convertToGL:location];
        CGPoint prevLocation = [[CCDirector sharedDirector] convertToGL:[touch previousLocationInView:[touch view]]];

        float diff = (location.y - prevLocation.y) / ipadScale * SENSIVITY;

        //MOVE MY PLAYERS HERE
    }
}

- (void) ccTouchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{
    [self ccTouchesEnded:touches withEvent:event];
}

- (void) ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    for (UITouch *touch in touches) {
        tapCount--;
        NSLog(@"Tap Count:%d", tapCount);
    }

    if (tapCount <= 0) {
        tapCount = 0;
        [self pauseGame];
    }
}

I'm also registering my game scene as a Standard Delegate, is it the problem? I guess not because it is required for multi touch! I believe that there ain't nothing wrong with this code neither, am I right? And when I say laggy, it's like running at 25 FPS, that's not a big deal, but kind of annoying!

Please help me! Thank you!

CodeSmile
  • 64,284
  • 20
  • 132
  • 217
RaphBlanchet
  • 575
  • 6
  • 23
  • HOW do you move the players? If you use move actions that will be your issue as the have a 1-frame lag before taking effect. That means restarting a move action in the next frame may not move the node at all. – CodeSmile Mar 17 '14 at 08:27
  • I am currently moving my players according to the touch movement. I am aware that moving the finger really fast can create a lag in the movement, but the lag I am talking about occurs even if I swipe slowly... – RaphBlanchet Mar 17 '14 at 17:47
  • this is because you receive touche events up to 60 times per second, hence if you use actions for the movement sometimes (several frames) the sprite won't move at all or continue to stutter – CodeSmile Mar 17 '14 at 18:28
  • I'm sorry, I forgot to tell you that actually I don't use actions to move my player. Let's say I move my finger (1, -1) away from my last touch, then the player will also move (1, -1) from it's actual position! – RaphBlanchet Mar 18 '14 at 01:00
  • Then it's hard to tell what exactly is "laggy". Maybe it's normal behavior (due to variation in touch movement speed), maybe it isn't. Can you post a video? – CodeSmile Mar 18 '14 at 09:52

0 Answers0