【问题标题】:Texture Atlas doesn't work纹理图集不起作用
【发布时间】:2016-09-27 05:47:13
【问题描述】:

我不明白出了什么问题。我的代码已编译,代码中没有错误和问题,终端中没有消息,图集由 .png 图像组成。 所以,当我编译我的代码时,纹理不显示。我能看到的只有红十字。我该如何解决这个问题?

这是我的代码:

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?) {

    }

}

【问题讨论】:

    标签: ios xcode swift sprite-kit


    【解决方案1】:

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

    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)
    }
    }
    

    【讨论】:

    • 当我第一次遇到这个问题时,我很喜欢你的视频,不幸的是它没有帮助我。我可以试试别的吗?
    • 能否在“self.addChild(bee)”之后添加这一行:print(bee),然后将控制台的内容写在这里?
    • 另一个原因可能是您使用的图像的分辨率与您使用的模拟器不匹配。无论如何我会尝试重新启动模拟器(点击模拟器窗口后,左上角选择模拟器 - >重置内容和设置)
    • 1) 当我添加print(bee) 控制台说&lt;SKSpriteNode&gt; name:'(null)' texture:['nil'] position:{250, 250} scale:{1.00, 1.00} size:{40, 40} anchor:{0.5, 0.5} rotation:0.00 2) “重置内容和设置”没有帮助(((((((
    • 这基本上表示精灵没有附加纹理(如果是,它应该说“纹理['纹理的名称'(纹理的大小)]”。项目中的蜜蜂叫什么名字?
    猜你喜欢
    • 2018-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多