用不同的位置显示更多障碍物 - 斯威夫特

时间:2016-01-17 21:26:30

标签: ios swift sprite-kit

我有更多的障碍,但报告说它是重复的。 我需要以某种方式简化,但问题是每个障碍必须明确给出位置。 你不知道怎么办?

这是代码:

func createObstacle()
    {
        obstacle.position = CGPoint(x: 250, y: 620)
        obstacle.zPosition = 5;
        obstacle.name = "obstacle"
        obstacle.physicsBody = SKPhysicsBody(rectangleOfSize: obstacle.size)
        obstacle.physicsBody!.dynamic = false
        obstacle.physicsBody!.contactTestBitMask = collisionMask
        self.addChild(obstacle)
        obstacle2.position = CGPoint(x: 100, y: 400)
        obstacle2.zPosition = 5;
        obstacle2.name = "obstacle"
        obstacle2.physicsBody = SKPhysicsBody(rectangleOfSize: obstacle2.size)
        obstacle2.physicsBody!.dynamic = false
        obstacle2.physicsBody!.contactTestBitMask = collisionMask
        self.addChild(obstacle2)
    }

由于

1 个答案:

答案 0 :(得分:0)

好吧,你似乎在重复自己,就像你说的那样。

所以我会创建一个可以设置给定障碍的函数。它接受给定的障碍以及位置输入并返回该位置的障碍以及常数。

// if the variables are not pointers
swap(&x, &y);

// if variables are passed via pointers
swap(x_ptr, y_ptr);

然后,您可以在func setupObstacle(obstacle:SKSpriteNode, position:CGPoint) -> SKSpriteNode // You don't say, but I assume it is an SKSpriteNode.. { obstacle.position = position; obstacle.zPosition = 5; obstacle.name = "obstacle" obstacle.physicsBody = SKPhysicsBody(rectangleOfSize: obstacle.size) obstacle.physicsBody!.dynamic = false obstacle.physicsBody!.contactTestBitMask = collisionMask self.addChild(obstacle) } 功能...

中调用此功能
createObstacle()

我对斯威夫特并不熟悉,但希望这是对的。无论如何,我确定你明白了这个想法!