iOS用SpriteKit写动画

作者: loser_彼岸花 | 来源:发表于2017-06-14 14:02 被阅读52次

    这个类要放在skview上 ————>

    /* Setup your scene here */

    - (void)addNodeWithScene:(NSString *)filePath{

    dispatch_async(dispatch_get_global_queue(0, 0), ^{

    NSFileManager * fileManager = [NSFileManager defaultManager];

    NSString  *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];

    NSString *directryPath = [path stringByAppendingPathComponent:filePath];

    //当前文件夹下所有文件的名字

    NSArray *tempFileList = [[NSArray alloc] initWithArray:[fileManager contentsOfDirectoryAtPath:directryPath error:nil]];

    // 得到本地沙盒中名为"MyImage"的路径,"MyImage"是保存的图片名

    self.backgroundColor = [SKColor clearColor];

    _ShoesNode = [[SKNode alloc]init];

    _ShoesNode.position = CGPointMake([[UIScreen mainScreen] bounds].size.width/2, [[UIScreen mainScreen] bounds].size.height/2);

    NSMutableArray *walkFrames = [NSMutableArray array];

    for (int i = 0; i <= tempFileList.count; i++) {

    NSString *imageFilePath = [directryPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@%d",filePath,i]];

    UIImage *animationImage = [UIImage imageWithContentsOfFile:imageFilePath];

    if (animationImage) {

    DebugLog(@"%@",animationImage);

    SKTexture *temp  = [SKTexture textureWithImage:animationImage];

    temp.filteringMode = SKTextureFilteringNearest;

    [temp preloadWithCompletionHandler:^{

    DebugLog(@"啊啊啊啊啊啊啊");

    }];

    [walkFrames addObject:temp];

    }

    }

    _bearWalkingFrames = walkFrames;

    dispatch_async(dispatch_get_main_queue(), ^{

    _bear = [SKSpriteNode new];

    _bear.size = [[UIScreen mainScreen] bounds].size;

    [_ShoesNode addChild:_bear];

    [self addChild:_ShoesNode];

    [self walkingBear];

    });

    });

    }

    -(void)walkingBear{

    //播放纹理动画

    [_bear runAction:[SKAction repeatAction:

    [SKAction animateWithTextures:_bearWalkingFrames

    timePerFrame:0.1f

    resize:NO

    restore:YES] count:1] completion:^{

    NSLog(@"执行完毕");

    [_ShoesNode removeAllChildren];

    _ShoesNode = nil;

    NSNotification *notification =[NSNotification notificationWithName:LastAnimationFinished object:self userInfo:nil];

    [[NSNotificationCenter defaultCenter] postNotification:notification];

    }];

    }

    - (void)dealloc{

    [[NSNotificationCenter defaultCenter] removeObserver:self];

    }

    相关文章

      网友评论

        本文标题:iOS用SpriteKit写动画

        本文链接:https://www.haomeiwen.com/subject/biisqxtx.html