使用 Spritekit 制作简单的 Gif 类动画 [英] Simple Gif like animation with Spritekit

查看:22
本文介绍了使用 Spritekit 制作简单的 Gif 类动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的找不到一个简单的解决方案,我看到的每个示例都只显示了非常复杂的解决方案,但我想要的只是 2-3 个循环的图像,所以它看起来好像是动画的.与动画 Gif 相同的效果.现在我有这个来创建图像

I can't really find a simple solution for this, every example I see only shows very complex solutions, but all I want is 2-3 images that cycle so it appears as if it is animated. Same effect as an animated Gif. For now I have this to create an image

MonsterNode = SKSpriteNode(imageNamed: "MonsterNode_GameScene")

但是如何将 MonsterNode 变量设置为此类动画?我真的在寻找实现这一目标所需的最少代码量.

but how would I set MonsterNode variable to an animation of this sort? I am really looking for the very least amount of code needed to achieve this.

推荐答案

主要思想是使用 animateWithTextures 来完成这个任务.您需要设置精灵需要动画的所有帧以及每帧的显示时间.然后使用 repeatActionForever 运行动画循环.

The main idea is to use animateWithTextures for this task. You need to set all the frames that the sprite needs to animated and the displayed time of each frame. Then use repeatActionForever to run the animation loop.

// Add 3 frames
let f0 = SKTexture.init(imageNamed: "MonsterNode_GameScene_0")
let f1 = SKTexture.init(imageNamed: "MonsterNode_GameScene_1")
let f2 = SKTexture.init(imageNamed: "MonsterNode_GameScene_2")
let frames: [SKTexture] = [f0, f1, f2]

// Load the first frame as initialization
monsterNode = SKSpriteNode(imageNamed: "MonsterNode_GameScene_0")
monsterNode.position = CGPoint(x: CGRectGetMidX(self.frame), y: CGRectGetMidY(self.frame))

// Change the frame per 0.2 sec
let animation = SKAction.animateWithTextures(frames, timePerFrame: 0.2)
monsterNode.runAction(SKAction.repeatActionForever(animation))

self.addChild(monsterNode)

这篇关于使用 Spritekit 制作简单的 Gif 类动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆