简单的Gif就像Spritekit的动画一样

简单的Gif就像Spritekit的动画一样

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

问题描述

我真的找不到一个简单的解决方案,我看到的每个例子都只显示非常复杂的解决方案,但我想要的只是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)

这篇关于简单的Gif就像Spritekit的动画一样的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 09:14