纹理ID不使用相同的ID

时间:2013-03-28 05:22:56

标签: iphone ios objective-c cocos2d-iphone

 Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'CCSprite is not using the same texture id'
*** First throw call stack:
(0x321d22a3 0x39eef97f 0x321d215d 0x32aa7ab7 0x71afd 0x48b61 0xb41cd 0xb1f7d 0xb1e4f 0xb8913 0x4123f 0x40bf5 0x41661 0x3e27f 0x9cf8f 0x9d917 0x9b331 0x33fed5f1 0x33fda801 0x33fda11b 0x35cf05a3 0x35cf01d3 0x321a7173 0x321a7117 0x321a5f99 0x32118ebd 0x32118d49 0x35cef2eb 0x3402e301 0xb0fa3 0x20e8)
libc++abi.dylib: terminate called throwing an exception
(lldb) 

以上是错误,下面是我的代码。我正在做的是我有两个不同的plist和png精灵表文件,并且都包含不同的瓷砖,但有一个船和ui。这是在我的加载场景层中,基本上在主菜单层中它推送一个int,即级别编号,然后是加载场景层,它根据级别数量打开精灵表文件。这只会在我打开1级然后打开2级然后返回并打开1级时崩溃,反之亦然。这引起了一段时间的头痛,并对我的问题提供任何帮助表示感谢。

     CGSize winSize = [CCDirector sharedDirector].winSize;
        [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:
         [NSString stringWithFormat:@"clonespritesheet%d.plist", loadlevel]];
        mainship = [Ship spriteWithSpriteFrameName:@"mainship.png"];
        mainship.position = startingshiplocation;
        mainship.speed = 10;
        [self addChild:mainship z:-4];

        joystickright = [Joystick spriteWithSpriteFrameName:@"uifirebutton.png"];
        joystickleft =  [Joystick spriteWithSpriteFrameName:@"uimovementbutton.png"];
        joystickleft.up = [[CCSpriteFrameCache sharedSpriteFrameCache]spriteFrameByName:@"uimovementbutton.png"];
        joystickleft.down = [[CCSpriteFrameCache sharedSpriteFrameCache]spriteFrameByName:@"uimovementbuttonactive.png"];
        joystickright.up = [[CCSpriteFrameCache sharedSpriteFrameCache]spriteFrameByName:@"uifirebutton.png"];
        joystickright.down = [[CCSpriteFrameCache sharedSpriteFrameCache]spriteFrameByName:@"uifirebuttonactive.png"];

        joystickright.position = startingjoystickrightlocation;
        joystickleft.position = startingjoystickleftlocation;
        [self addChild:joystickright z:1];
        [self addChild:joystickleft z:1];
        level1backdrop2.anchorPoint = ccp(0,0);
        level1backdrop1.anchorPoint = ccp(0,0);
        level1backdrop1 = [CCSprite spriteWithSpriteFrameName:@"backdrop.png"];
        level1backdrop1.position = ccp(0,190);
        level1backdrop2 = [CCSprite spriteWithSpriteFrameName:@"backdrop.png"];
        level1backdrop2.position = ccp(level1backdrop1.boundingBox.size.width-1,190);
        [self addChild:level1backdrop1 z: -5];
        [self addChild:level1backdrop2 z: -5];
        if(loadlevel == 1)
        {
        level2backdrop2.anchorPoint = ccp(0,0);
        level2backdrop1.anchorPoint = ccp(0,0);
        level2backdrop1 = [CCSprite spriteWithSpriteFrameName:@"backdrop2.png"];
        level2backdrop1.position = ccp(0,170);
        level2backdrop2 = [CCSprite spriteWithSpriteFrameName:@"backdrop2.png"];
        level2backdrop2.position = ccp(level2backdrop1.boundingBox.size.width-1,170);
        [self addChild:level2backdrop1 z: -5];
        [self addChild:level2backdrop2 z: -5];
        }


        CCSprite *uiscreenbotttom = [CCSprite spriteWithSpriteFrameName:@"uitemplate.png"];
        [self addChild:uiscreenbotttom z:0];
        uiscreenbotttom.position = ccp(winSize.width/2,43);


- (void) loadStartingTiles //16 by 24
{
    [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:
     [NSString stringWithFormat:@"clonespritesheet%d.plist", loadlevel]];
     for(int i = 0; i < amountblockslevel1; i++)
    {
        levelframes[i] =[[CCSpriteFrameCache sharedSpriteFrameCache]spriteFrameByName:
                             [NSString stringWithFormat:@"block%d.png", i]];
    }

    tiles = [CCSpriteBatchNode batchNodeWithFile:[NSString stringWithFormat:@"clonespritesheet%d.png", loadlevel]];
    [self addChild:tiles z:-3];


    CCSprite *tempsprite;
    for(int x = 0; x < 26; x++) //minus 1 for one at the begining
    {
        for(int y = 0; y < 16; y++)
        {
            tempsprite = [CCSprite spriteWithSpriteFrame:levelframes[currentscreen[y][x]]];
            tempsprite.position = ccp(x*20+10,(16-y)*20-10); //+10 for align for tile size
            [tiles addChild:tempsprite];
        }
    }

}

1 个答案:

答案 0 :(得分:1)

在将场景替换为loadlevel之前添加以下代码可以解决此问题

[[CCSpriteFrameCache sharedSpriteFrameCache] removeSpriteFrames];
 [[CCTextureCache sharedTextureCache] removeAllTextures];

希望它对某人有帮助!