NSMutableDictionary setObject:forKey随机设置null而不是object

时间:2016-01-12 10:38:16

标签: objective-c debugging sprite-kit nsmutabledictionary skaction

我有一个精灵查看工具,可以帮助预览动画师的SpriteKit精灵。 我有一个像这样的数据结构(伪)

NSArray POSES (sit,walk,jump,dig,etc){
    NSArray POSEFRAMES (sit0,sit1,sit2,walk0,walk1,walk2,dig0,dig1,...){
        NSArray of SIDES (side,up,down){
            SKTexture (sit0_side.png)
            SKTexture (sit0_up.png),...etc
        }
    }
}

我想要一个为每个边创建3个精灵的方法, 将它们并排放置并在它们上运行纹理动画。 我决定使用SKAction:animateWithTextures。

为了方便一次运行所有操作,我决定首先创建我需要的所有SKActions,将它们存储在某个容器中,然后在所有sprite准备好后枚举容器并启动每个元素 所以我决定使用NSMutableDictionary SKSpriteNode *作为键,SKAction *作为对象 - (SKSpriteNode *):( SKAction *) - 字典中的每条记录。

这是填充外部创建的字典的方法(可以填充更多的精灵)

-(void)addAnimatedSpritesFromPlate:(CreaturePlate*)plate ToDict:(NSMutableDictionary*)dict{
    NSArray* poseFrames = [plate getTexturesForPose:_currentPoseName]; //[1]:Here I get textures in the described above data structure
    if (poseFrames==nil)
        return;
    [NSMutableArray array];
    NSColor* color = [NSColor colorWithWhite:1 alpha:1];
    NSSize sz = NSMakeSize(340, 340);
    NSArray* sprites = @[
        [SKSpriteNode spriteNodeWithColor:color size:sz],
        [SKSpriteNode spriteNodeWithColor:color size:sz],
        [SKSpriteNode spriteNodeWithColor:color size:sz]];
    for (int i =0;i<3;++i){
        NSMutableArray* textures = [NSMutableArray array];//Here I create mutable array to fill it with a sequence of textures for each side (for example UP-only) of a particular pose frame

        for (int j =0;j<[poseFrames count];++j)
        {
            [textures addObject:poseFrames[j][i]];
        }
        SKSpriteNode* sprite = sprites[i];
        [sprite setAnchorPoint:NSZeroPoint];
        [sprite setPosition:CGPointMake(i*340, 20)];
        [sprite setTexture:textures[0]]; // at this point sprite is always valid and is not nil
        SKAction* animateWithTexturesAction = [SKAction animateWithTextures:textures timePerFrame:0.1]; //animateWithTexturesAction is also always valid and is not nil
        [dict setObject:animateWithTexturesAction forKey:sprite]; // the problem is here
    }
}

现在我在外部方法中运行两次 - 一个用于生物,一个用于生物项目

    -(void)playPose{
        if (_currentCreature){
            NSMutableDictionary* animatedSprites = [NSMutableDictionary dictionary];
            [self addAnimatedSpritesFromPlate:_currentCreature ToDict:animatedSprites];
            if (_currentItem){
                [self addAnimatedSpritesFromPlate:_currentItem ToDict:animatedSprites];
            }
//I set breakpoint here and check animatedSprites
            for (SKSpriteNode* sprite in [animatedSprites allKeys]){
                SKAction* action = animatedSprites[sprite];
                [self addChild:sprite];
                [sprite runAction:action completion:^{
                    [sprite removeFromParent];
                }];
            }
        }
    }

我有时会得到这种结果

(lldb) po animatedSprites
{
    "<SKSpriteNode> name:'(null)' texture:[<SKTexture> 'peasant_down.png' (340 x 340)] position:{680, 20} size:{340, 340} rotation:0.00" = "<SKAnimate: 0x608000027220>";
    "<SKSpriteNode> name:'(null)' texture:[<SKTexture> 'axe_side_active.png' (340 x 340)] position:{0, 20} size:{340, 340} rotation:0.00" = (null);
    "<SKSpriteNode> name:'(null)' texture:[<SKTexture> 'peasant_up.png' (340 x 340)] position:{340, 20} size:{340, 340} rotation:0.00" = "<SKAnimate: 0x60800002c040>";
    "<SKSpriteNode> name:'(null)' texture:[<SKTexture> 'peasant_side.png' (340 x 340)] position:{0, 20} size:{340, 340} rotation:0.00" = "<SKAnimate: 0x60800002fb80>";
    "<SKSpriteNode> name:'(null)' texture:[<SKTexture> 'axe_up_active.png' (340 x 340)] position:{340, 20} size:{340, 340} rotation:0.00" = "<SKAnimate: 0x600000030de0>";
    "<SKSpriteNode> name:'(null)' texture:[<SKTexture> 'axe_down_active.png' (340 x 340)] position:{680, 20} size:{340, 340} rotation:0.00" = (null);
}

但有时我会得到这个

(lldb) po animatedSprites
{
    "<SKSpriteNode> name:'(null)' texture:[<SKTexture> 'peasant_side.png' (340 x 340)] position:{0, 20} size:{340, 340} rotation:0.00" = "<SKAnimate: 0x60000002f9e0>";
    "<SKSpriteNode> name:'(null)' texture:[<SKTexture> 'axe_side_active.png' (340 x 340)] position:{0, 20} size:{340, 340} rotation:0.00" = "<SKAnimate: 0x60000002c420>";
    "<SKSpriteNode> name:'(null)' texture:[<SKTexture> 'peasant_down.png' (340 x 340)] position:{680, 20} size:{340, 340} rotation:0.00" = "<SKAnimate: 0x6000000302a0>";
    "<SKSpriteNode> name:'(null)' texture:[<SKTexture> 'peasant_up.png' (340 x 340)] position:{340, 20} size:{340, 340} rotation:0.00" = "<SKAnimate: 0x600000031120>";
    "<SKSpriteNode> name:'(null)' texture:[<SKTexture> 'axe_up_active.png' (340 x 340)] position:{340, 20} size:{340, 340} rotation:0.00" = "<SKAnimate: 0x60000002b9a0>";
    "<SKSpriteNode> name:'(null)' texture:[<SKTexture> 'axe_down_active.png' (340 x 340)] position:{680, 20} size:{340, 340} rotation:0.00" = "<SKAnimate: 0x60000002b4a0>";
}

结果总是随机的,唯一不变的特征是生物精灵总是好的,但物品精灵每次随机都是空的,有时它们都是空的,有时都不是。

另外我在png地图集中的一些spriteTextures实际上可能是空白的,在RGBA信息中为0 0 0 0,因为在某些动作中该项目可能被生物精灵遮挡。在这种情况下,整个项目精灵与生物精灵的大小相同,但其中的每个像素都有零。

我不知道该怀疑什么,如何处理这个调试问题。你能救我吗?

0 个答案:

没有答案