Sprite-Kit Sprites在我的if语句中没有向右移动

时间:2016-07-15 15:17:01

标签: ios sprite-kit sprite

在xcode中搞乱,我在我的游戏机器人上看起来像小块。我让所有的块都在游戏项目文件附带的更新功能中向左移动。但是,一旦其中一块瓷砖离开屏幕,我会将它重新定位到另一侧,试图让它继续运行,但它变得静止,不会移动到这里是我的代码。

override func update(currentTime: CFTimeInterval) {


    tileStone1.position = CGPoint(x: tileStone1.position.x - 4, y: tileStone1.position.y)
    tileStone2.position = CGPoint(x: tileStone2.position.x - 4, y: tileStone2.position.y)
    tileStone3.position = CGPoint(x: tileStone3.position.x - 4, y: tileStone3.position.y)
    tileStone4.position = CGPoint(x: tileStone4.position.x - 4, y: tileStone4.position.y)
    tileStone5.position = CGPoint(x: tileStone5.position.x - 4, y: tileStone5.position.y)
    tileStone6.position = CGPoint(x: tileStone6.position.x - 4, y: tileStone6.position.y)
    tileStone7.position = CGPoint(x: tileStone7.position.x - 4, y: tileStone7.position.y)
    tileStone8.position = CGPoint(x: tileStone8.position.x - 4, y: tileStone8.position.y)
    tileStone9.position = CGPoint(x: tileStone9.position.x - 4, y: tileStone9.position.y)
    tileStone10.position = CGPoint(x: tileStone10.position.x - 4, y: tileStone10.position.y)
    tileStone11.position = CGPoint(x: tileStone11.position.x - 4, y: tileStone11.position.y)
    tileStone12.position = CGPoint(x: tileStone12.position.x - 4, y: tileStone12.position.y)



    if (tileStone1.position.x < self.frame.size.width){

        tileStone1.anchorPoint = CGPointZero

        tileStone1.position = CGPoint(x: 930 - 4, y: tileStone1.position.y)




    }

1 个答案:

答案 0 :(得分:2)

正如Putz1103建议的那样,你真的应该使用数组。然后,正如我在评论中提出质疑的那样,您是否希望使用self.frame.minX代替self.frame.size.width? (此外,将来,您可以使用self.frame.width代替self.frame.size.width

它不移动的原因是因为位置总是小于宽度,这当然意味着每一帧都重置位置。

要绝对清楚:.width获取屏幕的宽度,例如1024,而.minX获取屏幕的最左侧位置,通常为0.

override func update(currentTime: CFTimeInterval) {

    tileStone1.position = CGPoint(x: tileStone1.position.x - 4, y: tileStone1.position.y)
    tileStone2.position = CGPoint(x: tileStone2.position.x - 4, y: tileStone2.position.y)
    tileStone3.position = CGPoint(x: tileStone3.position.x - 4, y: tileStone3.position.y)
    tileStone4.position = CGPoint(x: tileStone4.position.x - 4, y: tileStone4.position.y)
    tileStone5.position = CGPoint(x: tileStone5.position.x - 4, y: tileStone5.position.y)
    tileStone6.position = CGPoint(x: tileStone6.position.x - 4, y: tileStone6.position.y)
    tileStone7.position = CGPoint(x: tileStone7.position.x - 4, y: tileStone7.position.y)
    tileStone8.position = CGPoint(x: tileStone8.position.x - 4, y: tileStone8.position.y)
    tileStone9.position = CGPoint(x: tileStone9.position.x - 4, y: tileStone9.position.y)
    tileStone10.position = CGPoint(x: tileStone10.position.x - 4, y: tileStone10.position.y)
    tileStone11.position = CGPoint(x: tileStone11.position.x - 4, y: tileStone11.position.y)
    tileStone12.position = CGPoint(x: tileStone12.position.x - 4, y: tileStone12.position.y)



    if (tileStone1.position.x < self.frame.minX){

        tileStone1.anchorPoint = CGPointZero

        tileStone1.position = CGPoint(x: 930 - 4, y: tileStone1.position.y)
    }
}

数组版本(你必须初始化一个数组。这个版本更好,因为它可以在数组中使用很多tileStones):

override func update(currentTime: CFTimeInterval) {

    for tileStone in tileStonesArray {

        tileStone.position = CGPoint(x: tileStone.position.x - 4, y: tileStone.position.y)




        if (tileStone.position.x < self.frame.minX){

            tileStone.anchorPoint = CGPointZero

            tileStone.position = CGPoint(x: 930 - 4, y: tileStone.position.y)
        }
    }
}

此外(尽管我并不完全确定它是否可能因为某些属性是只读的 - 你必须亲自看看,虽然我认为它适用于Swift),也许可以直接改变tileStone.position.x

换句话说,您可以改用此行:

tileStone.position.x -= 4

//or later

tileStone.position.x = 926

比以下更简单,更简单:

tileStone.position = CGPoint(x: tileStone.position.x - 4, y: tileStone.position.y)

//or later

tileStone.position = CGPoint(x: 930 - 4, y: tileStone1.position.y)