SKTexture得到图像名称
有没有可能从SKSpriteNode从SKTexture获取当前的图像名称?
我是SpriteKit中的新成员,我在写一个小游戏。 我需要侦测忍者什么时候会击中敌人。 像这样的东西
我正在做SKAction
- (void)setUpHit { SKTextureAtlas *hitAtlas = [SKTextureAtlas atlasNamed:@"Ninja_hit"]; SKTexture *hit1 = [hitAtlas textureNamed:@"Ninja_hit_1"]; SKTexture *hit2 = [hitAtlas textureNamed:@"Ninja_hit_2"]; SKTexture *hit3 = [hitAtlas textureNamed:@"Ninja_hit_3"]; SKTexture *hit4 = [hitAtlas textureNamed:@"Ninja_hit_4"]; SKAction *hitAnimation = [SKAction animateWithTextures:@[hit1, hit2, hit3, hit4] timePerFrame:0.1]; SKAction *wait = [SKAction waitForDuration:0.3]; SKAction *goBack = [SKAction animateWithTextures:@[hit1, [SKTexture textureWithImageNamed:@"Ninja"]] timePerFrame:0.1]; self.hitAction = [SKAction sequence:@[hitAnimation, wait, goBack]]; }
但是只有图像Ninja_hit_2.png或Ninja_hit_3.png。
所以我需要检测当前的纹理图像名称,当我正在与敌人交叉忍者忍者。
现在我正在做类似的事情
if ([ninja intersectsNode:node] && !self.isKilled) { SKTexture *currentTexture = [ninja texture]; if ([self isHit:currentTexture]) { //kill enemy here } }
哪里
- (BOOL)isHit:(SKTexture *)texture { NSString *description = [texture description]; NSRange range = [description rangeOfString:@"'"]; NSString *textureName = [description substringFromIndex:NSMaxRange(range)]; range = [textureName rangeOfString:@"'"]; textureName = [textureName substringToIndex:NSMaxRange(range) - 1]; if ([textureName isEqualToString:@"Ninja_hit_2.png"] || [textureName isEqualToString:@"Ninja_hit_3.png"]) { return YES; } return NO; }
我知道这是不正确的,但我无法find如何采取当前的纹理名称或正确地做。 你可以帮帮我吗?
嗯,我的战略是这样的:
在你的忍者类有一个属性为您的纹理。 保持在身边
@property (nonatomic, strong) NSArray * hitTextures;
然后存储命中纹理(注意这是一个延迟加载getter方法)
-(NSArray *)hitTextures{ if (_hitTextures == nil){ SKTexture *hit1 = [SKTexture textureWithImageNamed:@"Ninja_hit_1"]; SKTexture *hit2 = [SKTexture textureWithImageNamed:@"Ninja_hit_2"]; SKTexture *hit3 = [SKTexture textureWithImageNamed:@"Ninja_hit_3"]; SKTexture *hit4 = [SKTexture textureWithImageNamed:@"Ninja_hit_4"]; _hitTextures = @[hit1, hit2, hit3, hit4]; } return _hitTextures; }
请注意,我们不需要显式地创buildSKTextureAtlas
对象:
加载纹理数据时,Sprite Kit会在应用程序包中search具有指定文件名的图像文件。 如果找不到匹配的图像文件,则Sprite Kit会在存储在应用程序包中的任何纹理地图上search纹理。 如果指定的图像不存在于束中的任何位置,Sprite Kit会创build一个占位符纹理图像。
使用这个纹理数组来填充你的SKAction
SKAction *hitAnimation = [SKAction animateWithTextures:self.hitTextures timePerFrame:0.1];
这可以让你改变你的-isHit
方法,如下所示:
- (BOOL)isHit:(SKTexture *)texture { NSUInteger index = [self.hitTextures indexOfObject:texture]; if (index == 1 || index == 2) { return YES; } return NO; }
这样你就不会依赖可以改变的-description
方法的实现细节。
从SKAtlasanimation中检测当前的名字,这不是最好的方法,但它对我有用。
var tempstr = yoursprite.texture!.description if(tempstr.rangeOfString("currentFrameName", options: NSStringCompareOptions.LiteralSearch, range: nil, locale: nil) != nil){ // do something }
- 使SKScene的背景透明不工作…这是一个错误?
- 在SpriteKit中检测对象的子节点上的触摸
- 用atlas文件和SpriteKit进行批处理绘图的问题
- 我应该在游戏中拥有多less个视图控制器?
- SKShapeNode使用错误的宽度和高度
- 如何在GameScene中从ViewController中调用方法
- 当运行一个动作时,Sprite Kit会崩溃,或者仅在iPad Air和iPad mini上加载video
- 如果在另一个节点“后面/下面”,我如何触摸节点?
- Xcode 7:'supportedInterfaceOrientations'实现中的返回types冲突:'UIInterfaceOrientationMask'vs'NSUInteger'