用节点填充屏幕

时间:2016-05-31 22:01:23

标签: ios swift sprite-kit

我正在努力寻找一种用spriteNodes行和行填充屏幕的方法。我现在只能得到一条这样的实线。 Picture of simulator

贝娄是我的代码,非常感谢任何帮助。

let world = SKNode()
let ground = Ground()

override func didMoveToView(view: SKView) {
    /* Setup your scene here */
    self.backgroundColor = UIColor(red: 0.4, green: 0.6, blue: 0.95, alpha: 1)
    self.addChild(world)
    var groundPosition = CGPoint(x: -self.size.width, y: 600)
    let groundSize = CGSize(width: self.size.width * 3, height: 0)
    ground.spawn(world, position: groundPosition, size: groundSize)


}

class Ground: SKSpriteNode, GameSprite{
    var textureAtlas: SKTextureAtlas = SKTextureAtlas(named: "ground.atlas")

    // Property named groundTexture to store current ground texture: 
    var groundTexture:SKTexture?
    func spawn(parentNode: SKNode, position: CGPoint , size: CGSize) {
        parentNode.addChild(self)
        self.size = size
        self.position = position
        self.anchorPoint = CGPointMake(0, 1)
        //default to the the mud texture
        if groundTexture == nil {
            groundTexture = textureAtlas.textureNamed("grass")
        }
        // Repeate the texture
        createChildren()
    }

    //Builds nodes to repeate the ground
    func createChildren() {
        if var texture = groundTexture{
            var tileCount:CGFloat = 0
            let textureSize = texture.size()
            let tileSize = CGSize(width: 25, height: 15)
            // Build nodes until we cover the screen
            while tileCount * tileSize.width < self.size.width{
                // randomly chooses the texture that will be used
                let random = arc4random_uniform(50)
                if random < 25{
                    texture = textureAtlas.textureNamed("grass")
                }else{
                    texture = textureAtlas.textureNamed("mud")
                }
                let tileNode = SKSpriteNode(texture: texture)
                tileNode.size = tileSize
                tileNode.position.x = tileCount * tileSize.width
                tileNode.anchorPoint = CGPoint(x: 0, y: 1)
                self.addChild(tileNode)
                tileCount++
            }
        }
    }

0 个答案:

没有答案