Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing sprite Images in Sprite-Kit

Is there a way to change the image of a sprite which has already been initialized with another image?

I tried:

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]

            }];

}

like image 895
user3138007 Avatar asked Apr 20 '14 15:04

user3138007


People also ask

How do I change the size of a sprite?

Change the sprite image to a new loaded image. If the new image has a different size or shape from the old image the sprite may look distorted as a result, you can fix this by resetting the sprite dimensions using SetSpriteSize. If the sprite is animated you may also need to call SetSpriteAnimation again.

How do I add an image to a sprite animation sequence?

By calling this command we’re telling AGK to add “item0.png” into sprite 1s animation sequence. The alternative method moves the LoadImage call directly into the second parameter of AddSpriteAnimationFrame. By doing this we bypass the need to store the image ID.

How do you create a custom sprite in Java?

Sprites combine an image, with position and animation details. You can create a sprite using Create Sprite, draw it with Draw Sprite, move it using the Sprite Velocity with Update Sprite, and animate it using an Animation Script. The sprite event handler function pointer is used when you want to register to receive events from a Sprite.

What are splashkit sprites?

SplashKit Sprites allows you to create images you can easily move and animate. SplashKit sprites are game elements that can be moved, and animated. Sprites are located at a position in the game, have a velocity, and an animation. The sprite can also have arbitary data associated with it for game specific purposes.


2 Answers

There is. Internally, the spriteNodeWithImageNamed: class method just uses the image name you pass it to set the node's texture property. That being said, if at any point you want to arbitrarily change the node's texture, you can just set it directly.

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

There are also some SKActions for doing this, in case you want the node to resize or animate between different textures.

[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]];
like image 147
Mick MacCallum Avatar answered Sep 28 '22 09:09

Mick MacCallum


You must create texture array such as this:

 [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];
like image 35
erdikanik Avatar answered Sep 28 '22 09:09

erdikanik