views:

187

answers:

0

Hi,

I have a character that goes through multiple states. The state changes are reflected by means of a sprite image (texture) change. The state change is triggered by a user tapping on the sprite. This works consistently and quite well.

I then added an animation during State0. Now, when the user taps - setTexture gets executed to change the texture to reflect State1, however some of the times (unpredictable) it does not change the texture.

The code flows as below:

// 1. // Create the animation sequence

CGRect frame1Rect = CGRectMake(0,32,32,32);
CGRect frame2Rect = CGRectMake(32,32,32,32);
CCTexture2D* texWithAnimation = [[CCTextureCache sharedTextureCache]
        addImage:@"Frames0_1_thinkNthickoutline32x32.png"];
id anim = [[[CCAnimation alloc] initWithName:@"Sports" delay:1/25.0] autorelease];
[anim addFrame:[CCSpriteFrame frameWithTexture:texWithAnimation rect:frame1Rect offset:ccp(0,0)]];
[anim addFrame:[CCSpriteFrame frameWithTexture:texWithAnimation rect:frame2Rect offset:ccp(0,0)]];

// Make the animation sequence repeat forever
id myAction = [CCAnimate actionWithAnimation: anim restoreOriginalFrame:NO];

// 2. // Run the animation:

    sports = [[CCRepeatForever alloc] init];
    [sports initWithAction:myAction];
    [self.sprite runAction:sports];

// 3. stop action on state change and change texture:

      NSLog(@"Stopping  action");
      [sprite stopAction:sports];
      NSLog(@"Changing texture for kCJSports");
      [self setTexture: [[CCTextureCache sharedTextureCache] addImage:@"SportsOpen.png"]];
      [self setTextureRect:CGRectMake(0,0,32,64)];
      NSLog(@"Changed texture for kCJSports");

Note that all the NSLog lines get logged - and the texture RECT changes - but the image/texture changes only some of the times - fails for around 10-30% of times. Locking/threading/timing issue somewhere? My app (game) is single threaded and I only use the addImage and not the Async version. Any help much appreciated.