Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何在swift spritekit中创建重复段_Ios_Swift_Sprite Kit_Nodes_Skaction - Fatal编程技术网

Ios 如何在swift spritekit中创建重复段

Ios 如何在swift spritekit中创建重复段,ios,swift,sprite-kit,nodes,skaction,Ios,Swift,Sprite Kit,Nodes,Skaction,正如您在下面的代码中所看到的,我试图在屏幕的左下角建立一个节点,然后将其中的二十个节点排成一行,这样它们就可以直接相邻,而不是堆叠在一起。但是,这段代码不起作用,并且没有显示我试图放入的图像。如果有人能告诉我为什么这不起作用,我会很感激的 是的,这个函数只是在代码的不同部分被调用 let obstacleSquare = SKSpriteNode(imageNamed: "obstaclesquare") func createBottomBound() { obstacleSquare

正如您在下面的代码中所看到的,我试图在屏幕的左下角建立一个节点,然后将其中的二十个节点排成一行,这样它们就可以直接相邻,而不是堆叠在一起。但是,这段代码不起作用,并且没有显示我试图放入的图像。如果有人能告诉我为什么这不起作用,我会很感激的

是的,这个函数只是在代码的不同部分被调用

let obstacleSquare = SKSpriteNode(imageNamed: "obstaclesquare")
func createBottomBound() {
    obstacleSquare.position = CGPoint(x: 0 + frame.size.width/40, y: 0 + frame.size.width/40)
    obstacleSquare.size = CGSize(width: frame.size.width/20, height: frame.size.width/20)
    var i = 0
    while (i<20) {
        obstacleSquare.removeFromParent()
        addChild(obstacleSquare)
        obstacleSquare.run(SKAction.moveBy(x: obstacleSquare.size.width*CGFloat(i), y: 0, duration: 0))
        i += 1
    }
}
让obstacleSquare=SKSpriteNode(图像名为:“obstacleSquare”)
func createbottomboundbound(){
obstacleSquare.position=CGPoint(x:0+frame.size.width/40,y:0+frame.size.width/40)
obstacleSquare.size=CGSize(宽度:frame.size.width/20,高度:frame.size.width/20)
变量i=0

(iHi)看看下面的差异

func createBottomBound() {
    var i = 0
    while (i<20) {
        //Create a new instance here
        let obstacleSquare = SKSpriteNode(imageNamed: "obstaclesquare")

        /*I'm surprised your code ran as this line should of made it crash, you are asking it to remove the obstacle before you even added it. Any way you do not need this*/
        //obstacleSquare.removeFromParent()

        /*You don't want the objects stacked so you can change the position in the loop*/
        obstacleSquare.position = CGPoint(x: 0 + frame.size.width/(40 - i*2), y: 0 +     frame.size.width/(40 - i*2))
        obstacleSquare.size = CGSize(width: frame.size.width/20, height: frame.size.width/20)
        addChild(obstacleSquare)
        obstacleSquare.run(SKAction.moveBy(x: obstacleSquare.size.width*CGFloat(i), y: 0, duration: 0))
        i += 1
    }
}
func createBottomBound(){
变量i=0

而(I)你只实例化一次SKSpriteNode类来创建一个对象。你对同一个人重复添加过程20次,而不是20个人。这很有效,我意识到了我的错误。谢谢。