在Sprite-Kit中更改精灵图像

时间:2014-04-20 15:13:57

标签: objective-c sprite-kit skspritenode

有没有办法更改已用其他图像初始化的精灵图像?

我试过了:

if ([node.name isEqualToString:@"NameX"]) {
        SKAction *fadeOut = [SKAction fadeOutWithDuration:0.3];
        SKAction *fadeIn = [SKAction fadeInWithDuration:0.3];

       [self.sprite runAction:fadeOut];

       [self runAction:fadeOut completion:^{

             self.sprite = [SKSpriteNode spriteNodeWithImageNamed:@"NameY"];

             [self.sprite runAction:fadeIn]

            }];
     

}

2 个答案:

答案 0 :(得分:11)

有。在内部,spriteNodeWithImageNamed:类方法只使用您传递它的图像名称来设置节点的纹理属性。话虽这么说,如果你想随意改变节点的纹理,你可以直接设置它。

[self.sprite setTexture:[SKTexture textureWithImageNamed:@"someOtherImage"]];

如果您希望节点在不同纹理之间调整大小或设置动画,还有一些SKActions可以执行此操作。

[self.sprite runAction:[SKAction setTexture:[SKTexture textureWithImageNamed:@"someOtherImage"] resize:YES]];


[self.sprite runAction:[SKAction animateWithTextures:@[tex1,tex2,tex3] timePerFrame:0.5 resize:YES restore:YES]];

答案 1 :(得分:2)

您必须创建纹理数组,例如:

 [SKAction animateWithTextures:[NSArray arrayWithObjects:
                               [SKTexture textureWithImageNamed:@"im1.png"],
                               [SKTexture textureWithImageNamed:@"im2.png"],
                               [SKTexture textureWithImageNamed:@"im3.png"],
                               [SKTexture textureWithImageNamed:@"im4.png"], nil] timePerFrame:0.5 resize:YES restore:YES];