我目前正在使用Sprite Kit和Xcode来设计游戏。我的角色通常处于跑步状态,由两个图像组成 - 代码如下:
bobSKTexture* Texture1 = [SKTexture textureWithImageNamed:@"bob1"];
bobTexture1.filteringMode = SKTextureFilteringNearest;
SKTexture* bobTexture2 = [SKTexture textureWithImageNamed:@"bob2"];
bobTexture2.filteringMode = SKTextureFilteringNearest;
SKAction* run = [SKAction repeatActionForever:[SKAction animateWithTextures:@[birdTexture1, birdTexture2] timePerFrame:0.2]];
_bob = [SKSpriteNode spriteNodeWithTexture:birdTexture1];
[_bob setScale:2.0];
_bob.position = CGPointMake(self.frame.size.width / 4, CGRectGetMidY(self.frame));
[_bob runAction:run];
我希望图像在触摸屏幕时发生变化,只需要很短的时间,然后我希望它返回上面的代码。我怎么能做到这一点?
答案 0 :(得分:2)
正如JānisK所说,当你想要改变图像时,这样做会更容易:
[_bob removeAllActions];
_bob.texture = [SKTexture textureWithImageNamed:NEW_TEXTURE];
[self performSelector:@selector(resetAnimation) withObject:nil afterDelay:2.0f];
NEW_TEXTURE
是您希望其更改为的纹理/图像的名称。
self
将是场景或您调用代码的任何地方_bob
这是resetAnimation
方法,在您的场景中定义,或者在您调用代码_bob
的任何地方定义:
- (void)resetAnimation
{
SKAction* run = [SKAction repeatActionForever:[SKAction animateWithTextures:@[birdTexture1, birdTexture2] timePerFrame:0.2]];
[_bob runAction:run];
}