Search code examples
cocos2d-iphonetexturesccsprite

CCSprite setTexture creates black square cocos2d (iOS)


I am trying to make a screen where I am listing questions with anwers. Good answer should have a green marker, bad answers should have red markers.

When I create the screen, I set all markers to green or red. When I load the question and answers from a website I am changing the markers to represent the right/wrong answers.

My problem is that instead of having the new marker appear, I am receiving a black square where the new maker should be. The weird thing is that if I have set the markers originally green than the green marker still appear, but the red ones are not and viva-versa.

Here is my code for the marker change:

-(void) SetMarker:(int)ToColor {

switch (ToColor) {
    case Marker_Red: {
        [Marker_Sprite setTexture:[[CCSprite spriteWithFile:@"RedDot.png"] texture]];
            break;
    }
    case Marker_Green: {
        [Marker_Sprite setTexture:[[CCSprite spriteWithFile:@"GreenDot.png"] texture]];
        break;
    }
    default:
        break;
}
}

I am using Cocos2d version 2.


Solution

  • I have actually found the answer for this question. It seems that the texture change is made originally out of the main thread and this caused the problem.

    The solution was to put the call of the function which was calling the setMarker function into the main thread like this:

    [self performSelectorOnMainThread:@selector(SetGoodAnswer) withObject:nil waitUntilDone:YES];

    -(void) SetGoodAnswer {

    for (int i=1; i<5; i++) {
        [ButtonSprites[i] SetMarker:AnswerGood[i-1]];
    }
    

    }

    After this call is making the changes, all markers are appearing correctly.