2014-04-20 13 views
5

Czy istnieje sposób na zmianę obrazu ikonki, która została już zainicjalizowana innym obrazem?Zmiana ikon Sprite w Sprite-Kit

Próbowałem:

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] 

      }]; 

}

Odpowiedz

12

Jest. Wewnętrznie metoda klasy spriteNodeWithImageNamed: używa nazwy obrazu, którą przekazujesz, aby ustawić właściwość tekstury węzła. Biorąc to pod uwagę, jeśli w dowolnym momencie chcesz dowolnie zmienić teksturę węzła, możesz po prostu ustawić ją bezpośrednio.

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

Istnieje również kilka SKActions, aby to zrobić, w przypadku, gdy chcesz, aby węzeł zmieniał rozmiar lub animował różne tekstury.

[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]]; 
+0

wolę nowoczesne notacji dot: self.sprite.texture = [SKTexture ...] – LearnCocos2D

2

Musisz utworzyć teksturę tablicę takich jak to:

[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]; 
Powiązane problemy