我使用swift,我得到了这个功能。
func spawnTwo() - > SKSpriteNode {
let two = SKSpriteNode(imageNamed: "landPic1")
two.anchorPoint = CGPoint(x: 0.5, y: 0.5)
two.name = "Obs"
two.zPosition = 5;
two.anchorPoint = CGPoint(x: 0.5 , y: 0.5);
two.position.x = 0
two.position.y = 0
self.scene?.addChild(two)
return two
}
现在我想,每当我调用该函数时,1280都会添加position.x。我该怎么做?
答案 0 :(得分:1)
只需将变量保留在函数之外:
var n = 0
func spawnTwo() -> SKSpriteNode {
//...
}
在函数中,您将1280乘以n
并增加n
:
let two = SKSpriteNode(imageNamed: "landPic1")
two.anchorPoint = CGPoint(x: 0.5, y: 0.5)
two.name = "\(n)bs" // you might want to change the name each time too
two.zPosition = 5;
two.anchorPoint = CGPoint(x: 0.5 , y: 0.5);
two.position.x = 1280 * n
two.position.y = 0
self.scene?.addChild(two)
n += 1
return two