2015-12-31 78 views
1

我很想在重新定位某個東西之後添加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)) 
    } 

} 
+0

無法準確顯示這裏發生了什麼 – hamobi

+0

我說的問題或我的代碼? – Albert

+0

platformrespawn令人困惑..並且您的問題令人困惑 – hamobi

回答

0

所以你每一幀上產卵敵人..多數民衆贊成將是太多的敵人..我還沒有完全按照你的遊戲的確切結構,但這可能會幫助你。

得到增量時間,看看有多少時間已經過去了..和每10秒(或其他)調用而不是調用它的每一幀

// time values 
var delta = NSTimeInterval(0) 
var last_update_time = NSTimeInterval(0) 

let spawnTimerMax = NSTimeInterval(10) 
var spawnTimer = NSTimeInterval(0) 

override func update(currentTime: NSTimeInterval) { 
    if last_update_time == 0.0 { 
     delta = 0 
    } else { 
     delta = currentTime - last_update_time 
    } 

    last_update_time = currentTime 

    // ten seconds elapsed, spawn enemy and restart. 
    spawnTimer += delta 
    if spawnTimer >= spawnTimerMax { 
     // your spawn code goes here 
     spawnTimer = 0 
    } 
} 
+0

,因此您試圖在新平臺上產生敵人當它進入視野? – hamobi

+0

爲了更好的可視化,我編輯了我的代碼,也沒有真正添加新的平臺節點,我只是在更新方法中更改它的位置。每當位置從底部變到頂部時,位於平臺頂部的敵人節點都消失了,所以我需要再次調用該方法來添加敵人。 – Albert

1

你的問題是你沒有創建您的產卵敵人代碼任何檢查,移動平臺已經催生了一個敵人。當然,你移動平臺,但直到你的玩家移動,它會不斷產生敵人。我沒有看到這種情況的發生,但是當你移動平臺,而不是您的更新,你應該產卵敵人

編輯:
哎呀,現在我明白了,這是一些醜陋的代碼笑,在所有這些IFS那移動平臺,你應該產生敵人。但是,這需要合作,也檢查以前的平臺是目前下方,而不是上方

編輯++:
經進一步審查,並添加細節,嘗試這樣的事情,而不是:

首先,給所有平臺一個名爲「平臺」

然後

override func update(currentTime: CFTimeInterval) { 
    for node in scene.children 
    { 
     if (node.name == "Platform" && !node.intersectsNode(scene) && node.position.y < player.y - (self.frame.size.height/4.5)) 
     { 
      let platform = node as! SKSpriteNode 
      platform.position.y += (7 * (self.frame.size.height/4.5)) 
      platform.color = SKColor.redColor() 
      SpawnEnemy(CGPoint(x: self.frame.size.width/2, y: platform.position.y + 30)) 

     } 
    } 
} 

問:請問這個連工作,我只想檢查,如果一個平臺打在現場的某一點,回收它,而不是爲h以檢查每一個平臺,並移動它的前一個

+0

我也想到了這一點,但問題是我沒有「真正」移動平臺,我寧願有一個照相機節點,一直關注玩家,而玩家「跳」照相機隨玩家移動。因此我需要在更新方法中重新定位平臺。 – Albert

+0

如果你正在使用相機,那麼你不能只使用'if(!platform.intersectsNode(cameranode)和platform.y Knight0fDragon

+0

我很抱歉,但我似乎不明白什麼意義代碼將使。 我在代碼中添加了我的相機。 – Albert