Reputation: 6079
i have a problem with texture in SpriteKit game:
in touchesBegan
i do:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInNode:self];
SKNode *node = [self nodeAtPoint:location];
if ([node.name isEqualToString:@"worm"]) {
[node removeAllActions];
SKAction *change = [SKAction setTexture:[SKTexture textureWithImageNamed:@"worm2"]];
[node runAction:change];
this code works but the new texture "worm2" is scaled, and you see bad compared to how it should.
From Apple Documentation, https://developer.apple.com/library/ios/documentation/SpriteKit/Reference/SKAction_Ref/Reference/Reference.html#//apple_ref/occ/clm/SKAction/setTexture:resize:
there should be the method: setTexture:resize:
but as you can see from the picture that I put, this method is not present..
What am I missing?
thanks everybody
Upvotes: 0
Views: 825
Reputation: 6079
Like suggest from @user867635 [SKAction setTexture:resize:]
is available from 7.1 .. so I found another solution to this problem:
when init the scene:
_texture = [SKTexture textureWithImageNamed:@"worm2"];
[_texture preloadWithCompletionHandler:^{}];
and after in touchesBegan
call this texture as above, preloadWithCompletionHandler
solved my problem
Upvotes: 0
Reputation: 64477
This code:
SKAction *change = [SKAction setTexture:[SKTexture textureWithImageNamed:@"worm2"]];
[node runAction:change];
is equivalent of changing the texture directly:
SKSpriteNode* sprite = (SKSpriteNode*)node;
sprite.texture = [SKTexture textureWithImageNamed:@"worm2"];
// optional: update sprite size to match texture size
sprite.size = sprite.texture.size;
There's no need (and less efficient) to use actions to change a texture unless you want to delay the texture change.
Upvotes: 1
Reputation:
I think [SKAction setTexture:resize:]
is only available in iOS 7.1. Take a look at the API Differences.
Maybe update Xcode to the latest version (5.1), if you are running an older version.
Upvotes: 2