本文介绍了iOS的SpriteKit动画不会出现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我加载使用纹理从TextureAtlas一个动画。
但动画不会出现在屏幕上。
我只当我的NSLog的纹理会出现!看起来像一个bug。
是否有人提出了类似的经历吗?
SKTextureAtlas *地图集= [SKTextureAtlas atlasWithRetinaCorrection:spriteSheetName_];
如果([atlas.textureNames计数] == 0)回报;
NSArray的* R = [自我getSprites:WEAPON_FIREANIMATION ofDict:atlas.textureNames];
如果([R计数] == 0)回报;
SKSpriteNode * firstSprite =(SKSpriteNode *)[自childNodeWithName:tFIRSTSPRITE];
[firstSprite removeActionForKey:aFIRE_ANIM];NSMutableArray里* walkAnimFrames = [NSMutableArray的阵列]
INT K = 0;
如果(self.weaponAnimDuration == 0)
{
K =(self.weaponShootInterval / 0.1F)/ [R计数]
}其他
{
K =(self.weaponAnimDuration / 0.1F)/ [R计数]
}
的for(int i = 0; I< k;我++)
{
对于(的NSString * r中的sname)
{
[walkAnimFrames ADDOBJECT:[SKTexture textureWithImageNamed:SNAME]];
}}
的NSLog(@%@,walkAnimFrames);SKAction * weaponAnim = [SKAction animateWithTextures:walkAnimFrames timePerFrame:0.1F];
[firstSprite runAction:weaponAnim withKey:aFIRE_ANIM];
#进口SKTextureAtlas + MySKTextureAtlas.h
@implementation SKTextureAtlas(MySKTextureAtlas)+(SKTextureAtlas *)atlasWithRetinaCorrection:(* NSString的)名称
{
如果([UIScreen mainScreen] respondsToSelector:@selector(比例)] == YES&放大器;&安培; [UIScreen mainScreen]规模] == 2.00)
{
命名= [的NSString stringWithFormat:@%@@ 2X,名字]
} * NSString的路径= [[一个NSBundle mainBundle] pathForResource:名称ofType:@atlasc];
如果(路径==无)返回零; 返回[SKTextureAtlas atlasNamed:姓名]
}
@implementation的NSArray(工具)
- (NSArray的*)animationTextureFrames
{
NSMutableArray里·RR = [NSMutableArray的新]
对于(自我的NSString *名称)
{
SKTexture * TEX = [SKTexture textureWithImageNamed:姓名]
[RR ADDOBJECT:TEX];
}
返回RR;
}
解决方案
我发现的bug!
我不得不preLOAD纹理。
[SKTexture preloadTextures:explosionTextures withCompletionHandler:^(无效){}];
I'm loading an animation using textures from a TextureAtlas.
But the animation does not appear on screen.
I appears only when I NSLog the Textures! Looks like a bug.
Does somebody made similar experience?
SKTextureAtlas *atlas = [SKTextureAtlas atlasWithRetinaCorrection:spriteSheetName_];
if ([atlas.textureNames count] == 0) return;
NSArray *r = [self getSprites:WEAPON_FIREANIMATION ofDict:atlas.textureNames];
if ([r count] == 0)return;
SKSpriteNode *firstSprite = (SKSpriteNode*)[self childNodeWithName:tFIRSTSPRITE];
[firstSprite removeActionForKey:aFIRE_ANIM];
NSMutableArray *walkAnimFrames = [NSMutableArray array];
int k =0;
if (self.weaponAnimDuration == 0)
{
k = (self.weaponShootInterval / 0.1f)/[r count];
} else
{
k = (self.weaponAnimDuration / 0.1f)/[r count];
}
for (int i = 0; i<k; i++)
{
for (NSString *sname in r)
{
[walkAnimFrames addObject:[SKTexture textureWithImageNamed:sname]];
}
}
NSLog(@"%@",walkAnimFrames);
SKAction *weaponAnim = [SKAction animateWithTextures:walkAnimFrames timePerFrame:0.1f];
[firstSprite runAction:weaponAnim withKey:aFIRE_ANIM];
#import "SKTextureAtlas+MySKTextureAtlas.h"
@implementation SKTextureAtlas (MySKTextureAtlas)
+(SKTextureAtlas*)atlasWithRetinaCorrection:(NSString*)name
{
if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)] == YES && [[UIScreen mainScreen] scale] == 2.00)
{
name = [NSString stringWithFormat:@"%@@2x",name];
}
NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:@"atlasc"];
if (path == Nil) return Nil;
return [SKTextureAtlas atlasNamed:name];
}
@implementation NSArray (Tools)
-(NSArray*)animationTextureFrames
{
NSMutableArray *rr = [NSMutableArray new];
for (NSString *name in self)
{
SKTexture *tex = [SKTexture textureWithImageNamed:name];
[rr addObject:tex];
}
return rr;
}
解决方案
I found the bug!
I had to preload the textures.
[SKTexture preloadTextures:explosionTextures withCompletionHandler:^(void){}];
这篇关于iOS的SpriteKit动画不会出现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!