用地面纹理节点填充屏幕

时间:2016-06-01 10:02:56

标签: ios swift sprite-kit

我很难找到一种用精灵节点的行和行填充屏幕的方法。

我现在只能得到一条这样的实线。

enter image description here

我想要它,所以代码会重复,所以它填充到当前行以下,我已经尝试了几件没有运气的事情。

我是SpriteKit和iOS游戏开发的新手,所以我可能会遗漏一些明显的东西。

以下是我的代码,非常感谢任何帮助。

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

1 个答案:

答案 0 :(得分:1)

尝试使用此代替createchildren()

//填充屏幕所需的行数。

var num = self.frame.height / groundTexture.size.height  

//用重复的草填充屏幕的方法:

    for no in 2..num {

       var groundTextRep = groundTexture.copy

       groundTextRep.position = CGPoint(x: groundTexture.position.x, y: (groundTexture.position.y * no)
    }