获取不同的颜色背景以向上滚动

时间:2015-11-12 12:51:04

标签: swift

我正在为我的A2课程项目编写代码,制作一款着名的游戏“倒下来”。但是我试图在滚动时改变每个循环的背景 - 所以它从蓝色变为红色到黄色等。但是当我运行它时,这只适用于前两种颜色,然后闪回第一种颜色。这是我目前的代码

var background = SKSpriteNode()




override func didMoveToView(view: SKView) {



    let blueTexture = SKTexture(imageNamed: "BlueBackground")
    let redTexture = SKTexture(imageNamed: "RedBackground")
    let yellowTexture = SKTexture(imageNamed: "YellowBackground")
    let greenTexture = SKTexture(imageNamed: "GreenBackground")
    let purpleTexture = SKTexture(imageNamed: "PurpleBackground")
    let TextureArray = [blueTexture, redTexture, yellowTexture, greenTexture, purpleTexture]

    let levelProgress = SKAction.moveByX(0, y:blueTexture.size().height, duration: 5)
    let newLevel = SKAction.moveByX(0 , y: -blueTexture.size().height, duration: 0)
    let sequenceForever = SKAction.repeatActionForever(SKAction.sequence([ levelProgress,  newLevel]))

    var a = 0

    for var i: CGFloat = 0; i<5; i++ {
        let currentBG = TextureArray[a]
        a++
        background = SKSpriteNode(texture: currentBG)
        background.position = CGPoint(x: CGRectGetMidX(self.frame), y: -blueTexture.size().height/2 + blueTexture.size().height * i)
        background.size.width = self.frame.width
        background.runAction(sequenceForever)

        self.addChild(background)

    }

如果有人能指出我正确的方向,或者如果需要更多信息,请告诉我!这是我的第一篇文章,所以任何发布建议都会很棒。

1 个答案:

答案 0 :(得分:0)

可能因为这个原因:

let levelProgress = SKAction.moveByX(0, y:blueTexture.size().height, duration: 5)
let newLevel = SKAction.moveByX(0 , y: -blueTexture.size().height, duration: 0)
let sequenceForever = SKAction.repeatActionForever(SKAction.sequence([ levelProgress,  newLevel]))

background.position = CGPoint(x: CGRectGetMidX(self.frame), y: -blueTexture.size().height/2 + blueTexture.size().height * i

你总是使用blueTexture.size。尝试将其更改为常量值。