문제

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.. enter image description here

What am I missing?

thanks everybody

도움이 되었습니까?

해결책 3

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

다른 팁

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.

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top