Texture Atlas doesn't work

时间:2016-05-29 08:59:07

标签: ios xcode swift sprite-kit

I can't understand what is wrong. My code compiled, no errors and issues in the code, no messages in the terminal, atlas consists of .png images. So, when I compiled my code, texture doesn't display. All that I can see is red cross. How I can solve this problem?

enter image description here

这是我的代码:

import SpriteKit

class GameScene: SKScene {


    override func didMoveToView(view: SKView) {
        self.backgroundColor = UIColor(red: 0.4, green: 0.6, blue: 0.5, alpha: 1.0)

        let bee = SKSpriteNode()
        bee.position = CGPoint(x: 250, y: 250)
        bee.size = CGSize(width: 40, height: 40)
        self.addChild(bee)

        let beeAtlas = SKTextureAtlas(named: "bee")
        let beeFrames : [SKTexture] =
        [beeAtlas.textureNamed("c1.png"),
        beeAtlas.textureNamed("c2.png")]

        let flyAction = SKAction.animateWithTextures(beeFrames,    timePerFrame: 0.5)
        let beeAction = SKAction.repeatActionForever(flyAction)
        bee.runAction(beeAction)
}

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {

    }

}

1 个答案:

答案 0 :(得分:0)

我现在在我的电脑上,我已经写了这段代码,让我知道它是否有效:

import SpriteKit

class GameScene: SKScene {


var bee = SKSpriteNode()
var beeAtlas = SKTextureAtlas()
var beeFrames = [SKTexture]()

override func didMoveToView(view: SKView) {
    self.backgroundColor = UIColor(red: 0.4, green: 0.6, blue: 0.5, alpha: 1.0)

    beeAtlas = SKTextureAtlas(named: "bee")

    for i in 1...beeAtlas.textureNames.count {
        var name = "c\(i).png"
        beeFrames.append(SKTexture(imageNamed: name))
    }

    bee = SKSpriteNode(imageNamed: beeAtlas.textureNames[0] as! String)
    bee.position = CGPoint(x: 250, y: 250)
    bee.size = CGSize(width: 40, height: 40)

    bee.runAction(SKAction.repeatActionForever(SKAction.animateWithTextures(beeFrames, timePerFrame: 0.5)))

    self.addChild(bee)
}
}