我很想在重新定位某個東西之後添加SKSpriteNode,因此我想通過Swift Sprite Kit中的更新方法添加節點。通過更新方法添加節點
有對象(平臺)向下移動y軸,一旦它們不在屏幕上,它們又會出現在頂端(如無限循環)。我在更新方法中將其可視化。請注意,這些平臺不會真正移動,而是我將相機放在我的播放器節點上向上移動。
在重新定位平臺節點之後,我想再次在平臺之上添加Enemy,但是因爲我嘗試通過更新方法,它有時會在其上添加超過1個節點。
我不能像平臺一樣重新定位敵人,因爲它應該是一個隨機敵人節點。
任何方式來調用更新方法中的SpawnEnemy方法,並檢查它是否只被調用一次?
我的代碼:
override func didMoveToView(view: SKView) {
/* Setup your scene here */
self.World = SKNode()
self.World.name = "World"
addChild(World)
self.WorldCamera = SKNode()
self.WorldCamera.name = "Camera"
self.World.addChild(WorldCamera)
....
SpawnPlatforms()
SpawnEnemy(CGPoint(x: self.frame.size.width/2, y: Platform2.position.y + 30))
SpawnEnemy(CGPoint(x: self.frame.size.width/2, y: Platform3.position.y + 30))
SpawnEnemy(CGPoint(x: self.frame.size.width/2, y: Platform4.position.y + 30))
SpawnEnemy(CGPoint(x: self.frame.size.width/2, y: Platform5.position.y + 30))
SpawnEnemy(CGPoint(x: self.frame.size.width/2, y: Platform6.position.y + 30))
SpawnEnemy(CGPoint(x: self.frame.size.width/2, y: Platform7.position.y + 30))
}
func SpawnPlatforms() {
Platform0 = SKSpriteNode (color: SKColor.greenColor(), size: CGSize(width: self.frame.size.width * 2 , height: 25))
Platform0.position = CGPoint(x: self.frame.size.width/2, y: -36)
Platform0.zPosition = 1
Platform0.physicsBody = SKPhysicsBody(rectangleOfSize:Platform0.size)
Platform0.physicsBody?.dynamic = false
Platform0.physicsBody?.allowsRotation = false
Platform0.physicsBody?.restitution = 0.0
Platform0.physicsBody?.usesPreciseCollisionDetection = true
Platform0.physicsBody?.velocity = CGVectorMake(0, 0)
Platform0.physicsBody?.categoryBitMask = Platform0Category
Platform0.physicsBody?.collisionBitMask = PlayerCategory | EnemyCategory
Platform0.physicsBody?.contactTestBitMask = PlayerCategory | EnemyCategory
World.addChild(Platform0)
Platform1 = SKSpriteNode (color: SKColor.redColor(), size: CGSize(width: self.frame.size.width * 2 , height: 25))
Platform1.position = CGPoint(x: self.frame.size.width/2, y: Platform0.position.y + self.frame.size.height/4.5)
Platform1.zPosition = 1
Platform1.physicsBody = SKPhysicsBody(rectangleOfSize:Platform1.size)
Platform1.physicsBody?.dynamic = false
Platform1.physicsBody?.allowsRotation = false
Platform1.physicsBody?.restitution = 0
Platform1.physicsBody?.usesPreciseCollisionDetection = true
Platform1.physicsBody?.categoryBitMask = Platform1Category
Platform1.physicsBody?.collisionBitMask = PlayerCategory | EnemyCategory
Platform1.physicsBody?.contactTestBitMask = PlayerCategory | EnemyCategory
World.addChild(Platform1)
....a.s.o. for the other 6 platform nodes
}
func SpawnEnemy(position: CGPoint!){
let random = arc4random_uniform(4)
switch (random) {
case 0:
let node1 = SpawnNode1(position)
self.addChild(node1)
break
case 1:
let node2 = SpawnNode2(position)
self.addChild(node2)
break
case 2:
let node3 = SpawnNode3(position)
self.addChild(node3)
break
case 3:
break
default:
break
}
}
override func didSimulatePhysics() {
WorldCamera.position = CGPoint(x: Player.position.x, y: Player.position.y)
self.centerOnNode(WorldCamera!)
}
func centerOnNode(node: SKNode) {
let cameraPositionInScene: CGPoint = WorldCamera.scene!.convertPoint(WorldCamera.position, fromNode: World)
World.runAction(SKAction.moveTo(CGPoint(x:World.position.x , y:World.position.y - cameraPositionInScene.y), duration: 1.0))
}
override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
if(Platform1.position.y < (Player.position.y - self.frame.size.height/2)){
Platform1.position = CGPointMake(self.frame.size.width/2, Platform7.position.y + self.frame.size.height/4.5)
Platform1.color = SKColor.redColor()
SpawnEnemy(CGPoint(x: self.frame.size.width/2, y: Platform1.position.y + 30))
}
if(Platform2.position.y < (Player.position.y - self.frame.size.height/2)){
Platform2.position = CGPointMake(self.frame.size.width/2, Platform1.position.y + self.frame.size.height/4.5)
Platform2.color = SKColor.redColor()
SpawnEnemy(CGPoint(x: self.frame.size.width/2, y: Platform2.position.y + 30))
}
if(Platform3.position.y < (Player.position.y - self.frame.size.height/2)){
Platform3.position = CGPointMake(self.frame.size.width/2, Platform2.position.y + self.frame.size.height/4.5)
Platform3.color = SKColor.redColor()
SpawnEnemy(CGPoint(x: self.frame.size.width/2, y: Platform3.position.y + 30))
}
if(Platform4.position.y < (Player.position.y - self.frame.size.height/2)){
Platform4.position = CGPointMake(self.frame.size.width/2, Platform3.position.y + self.frame.size.height/4.5)
Platform4.color = SKColor.redColor()
SpawnEnemy(CGPoint(x: self.frame.size.width/2, y: Platform4.position.y + 30))
}
if(Platform5.position.y < (Player.position.y - self.frame.size.height/2)){
Platform5.position = CGPointMake(self.frame.size.width/2, Platform4.position.y + self.frame.size.height/4.5)
Platform5.color = SKColor.redColor()
SpawnEnemy(CGPoint(x: self.frame.size.width/2, y: Platform5.position.y + 30))
}
if(Platform6.position.y < (Player.position.y - self.frame.size.height/2)){
Platform6.position = CGPointMake(self.frame.size.width/2, Platform5.position.y + self.frame.size.height/4.5)
Platform6.color = SKColor.redColor()
SpawnEnemy(CGPoint(x: self.frame.size.width/2, y: Platform6.position.y + 30))
}
if(Platform7.position.y < (Player.position.y - self.frame.size.height/2)){
Platform7.position = CGPointMake(self.frame.size.width/2, Platform6.position.y + self.frame.size.height/4.5)
Platform7.color = SKColor.redColor()
SpawnEnemy(CGPoint(x: self.frame.size.width/2, y: Platform7.position.y + 30))
}
}
無法準確顯示這裏發生了什麼 – hamobi
我說的問題或我的代碼? – Albert
platformrespawn令人困惑..並且您的問題令人困惑 – hamobi