Search code examples
cocos2d-iphoneios-simulatorccsprite

Cocos2d iphone: changing CCSprite.position works on simulator but not on device


I've got a problem with some cocos2d code, it runs fine on the simulator (means in that case that sprites are moving when I touch and scroll) but it doesn't work on my ipod The ccTouchesMoved is called on both, but sprite move only in the simulator

CCSprite *gradA = [CCSprite spriteWithFile:@"grad.png"];
gradA.anchorPoint = ccp(0, 0);
gradA.position = ccp(0, 0);

[...]

-(void) ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{   
    //NSLog(@"ccTouchesMoved called");

    UITouch *touch   = [touches anyObject];
    CGPoint location = [touch locationInView: [touch view]];
    location = [[CCDirector sharedDirector] convertToGL:location];

    int d = (_prevTouch.y - location.y);
    // This code should make the sprite moving
    // And it does on the simulator but not on my ipod
    gradA.position = ccp(PARALAX_X, gradA.position.y - d);

    _prevTouch = location;
}

-(void) ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch   = [touches anyObject];
    CGPoint location = [touch locationInView: [touch view]];
    location = [[CCDirector sharedDirector] convertToGL:location];

    _firstTouch = location;
}

-(void) ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{   
    UITouch *touch   = [touches anyObject];
    CGPoint location = [touch locationInView: [touch view]];
    location = [[CCDirector sharedDirector] convertToGL:location];

    _lastTouch = location;
}

BTW if I do a "gradA.position = ccp(0, gradA.position.y - d);" in any other method than ccTouchesMoved it works on device and simulator.

It could be a stupid mistake (and I hope it is) on my side because it's my first project.


Solution

  • The answer is simple, and if you have problem between a device and the simulator you should check that (as well as filename case) NSLog is very slow on device, so slow that it prevents the device to render the sprite movement.

    I had two NSLogs in my code in the ccTouchesMoved method, when I removed them it works like a charm.

    Avoid using NSLog on device.