我是一名初學者程序員,正在使用iOS sprite-kit創建遊戲。我有一個簡單的動畫GIF(30幀)保存爲.gif文件。有沒有一種簡單的方法(幾行代碼可能類似於通過UIImage添加常規.png)在我的遊戲中顯示此GIF?我已經做了一些關於在Xcode中顯示動畫GIF的研究,並且大多數涉及導入大量的類,其中大部分是我認爲我不需要的東西(我幾乎不知道足以通過它篩選)。添加簡單的動畫GIF到iOS Spritekit遊戲?
3
A
回答
3
我認爲它的gif的方式就像動畫一個精靈。所以我要做的就是將gif作爲紋理添加到for循環中的SKSpriteNode中,然後通過SKAction.repeatActionForever()告訴它在設備上運行。 老實說,我對此也很陌生。我只是想給我最好的答案。這是用Swift編寫的,但我認爲它不會很難翻譯成Objective-C。
var gifTextures: [SKTexture] = [];
for i in 1...30 {
gifTextures.append(SKTexture(imageNamed: "gif\(i)"));
}
gifNode.runAction(SKAction.repeatActionForever(SKAction.animateWithTextures(gifTextures, timePerFrame: 0.125)));
+0
但動畫GIF是一個單一的文件,而在你的代碼中,你假設了一系列單幀圖像文件。 – Bobjt
0
邁克爾崔的回答會讓你一半在那裏。剩下的就是從gif文件中獲取單個幀。下面是我如何做到這一點(斯威夫特):
func load(imagePath: String) -> ([SKTexture], TimeInterval?) {
guard let imageSource = CGImageSourceCreateWithURL(URL(fileURLWithPath: imagePath) as CFURL, nil) else {
return ([], nil)
}
let count = CGImageSourceGetCount(imageSource)
var images: [CGImage] = []
for i in 0..<count {
guard let img = CGImageSourceCreateImageAtIndex(imageSource, i, nil) else { continue }
images.append(img)
}
let frameTime = count > 1 ? imageSource.delayFor(imageAt: 0) : nil
return (images.map { SKTexture(cgImage: $0) }, frameTime)
}
extension CGImageSource { // this was originally from another SO post for which I've lost the link. Apologies.
func delayFor(imageAt index: Int) -> TimeInterval {
var delay = 0.1
// Get dictionaries
let cfProperties = CGImageSourceCopyPropertiesAtIndex(self, index, nil)
let gifPropertiesPointer = UnsafeMutablePointer<UnsafeRawPointer?>.allocate(capacity: 0)
if CFDictionaryGetValueIfPresent(cfProperties, Unmanaged.passUnretained(kCGImagePropertyGIFDictionary).toOpaque(), gifPropertiesPointer) == false {
return delay
}
let gifProperties: CFDictionary = unsafeBitCast(gifPropertiesPointer.pointee, to: CFDictionary.self)
// Get delay time
var delayObject: AnyObject = unsafeBitCast(
CFDictionaryGetValue(gifProperties,
Unmanaged.passUnretained(kCGImagePropertyGIFUnclampedDelayTime).toOpaque()),
to: AnyObject.self)
if delayObject.doubleValue == 0 {
delayObject = unsafeBitCast(CFDictionaryGetValue(gifProperties,
Unmanaged.passUnretained(kCGImagePropertyGIFDelayTime).toOpaque()), to: AnyObject.self)
}
delay = delayObject as? TimeInterval ?? 0.1
if delay < 0.1 {
delay = 0.1 // Make sure they're not too fast
}
return delay
}
}
請注意,我假設的gif的每一幀的長度相同,這並非總是如此。
你也可以很容易地用這些圖像構造SKTextureAtlas。
相關問題
- 1. SpriteKit SKCameraNode無法動畫爲使用SpriteKit的iOS遊戲縮放
- 2. SpriteKit遊戲:移動遊戲查看
- 3. iOS動畫2D遊戲
- 4. 在spritekit遊戲中滾動
- 5. SpriteKit遊戲物理滯後
- 6. 添加排行榜到iOS遊戲
- 7. Spritekit遊戲設計
- 8. SpriteKit遊戲邏輯
- 9. 將Gif動畫添加到複選框
- 10. IOS plist簡單測驗遊戲
- 11. SpriteKit中的遊戲菜單系統
- 12. 爲SpriteKit遊戲開發界面
- 13. iOS禁用推播遊戲動畫
- 14. 編碼一個SpriteKit遊戲
- 15. iOS的:簡單的動畫
- 16. 如何創建簡單的遊戲動畫
- 17. 簡單的動畫益智遊戲開發
- 18. XNA遊戲工作室最簡單的精靈動畫
- 19. 轉換簡單的Java遊戲中的安卓遊戲到Facebook
- 20. iOS和SpriteKit - 重新啓動遊戲後的低fps
- 21. 「Drag'n'Drop」遊戲動畫
- 22. 畫布遊戲動畫
- 23. 使用Spritekit設置遊戲菜單
- 24. 更新到iOS 8讓我的SpriteKit遊戲超級慢
- 25. 在xcode spriteKit按鈕上運行動畫遊戲
- 26. 簡單遊戲的問題
- 27. 簡單的數學遊戲
- 28. 簡單遊戲的NSUserDefault
- 29. 簡單的遊戲開發
- 30. 簡單的猜謎遊戲
http://www.raywenderlich.com/45152/sprite-kit-tutorial-animations-and-texture-atlases –