如何阻止精灵重叠?

时间:2016-03-15 00:43:36

标签: ios swift sprite-kit

我有两种不同类型的精灵从屏幕顶部下来。然而,有时它们同时下降,并且在相同的位置,一个与另一个重叠。如何解决这个问题,以便它们在下降时不会重叠?我听说过使用intersectNode,但我不确定如何实际实现它。

以下是我的代码到目前为止的样子:

// function to spawn enemies
func SpawnEnemies(){
    var Enemy = SKSpriteNode (imageNamed: "brick1")
    Enemy.physicsBody = SKPhysicsBody(rectangleOfSize: Enemy.size)
    Enemy.physicsBody?.categoryBitMask = PhysicsCatagory.brick
    Enemy.physicsBody?.collisionBitMask = PhysicsCatagory.hero
    Enemy.physicsBody?.contactTestBitMask = PhysicsCatagory.hero
    Enemy.physicsBody?.affectedByGravity = false
    Enemy.physicsBody?.dynamic = false

    var MinValue: CGFloat = 0.0
    var MaxValue: CGFloat = self.size.width
    var SpawnPoint = UInt32(MaxValue - MinValue)
    Enemy.position = CGPoint(x: CGFloat(arc4random_uniform(SpawnPoint)), y: self.size.height)
    let ActionDone = SKAction.removeFromParent()
    let EnemyAction = SKAction.moveToY(-100 , duration: 7.0)
    Enemy.runAction(SKAction.sequence([EnemyAction, ActionDone]))

    self.addChild(Enemy)
}

func SpawnEnemies2(){
    var Enemy2 = SKSpriteNode (imageNamed: "brick2")
    Enemy2.physicsBody = SKPhysicsBody(rectangleOfSize: Enemy2.size)
    Enemy2.physicsBody?.categoryBitMask = PhysicsCatagory.brick
    Enemy2.physicsBody?.collisionBitMask = PhysicsCatagory.hero
    Enemy2.physicsBody?.contactTestBitMask = PhysicsCatagory.hero
    Enemy2.physicsBody?.affectedByGravity = false
    Enemy2.physicsBody?.dynamic = false

    var MinValue: CGFloat = 0.0
    var MaxValue: CGFloat = self.size.width
    var SpawnPoint = UInt32(MaxValue - MinValue)
    Enemy2.position = CGPoint(x: CGFloat(arc4random_uniform(SpawnPoint)), y: self.size.height)
    let ActionDone = SKAction.removeFromParent()
    let EnemyAction = SKAction.moveToY(-100 , duration: 7.0)
    Enemy2.runAction(SKAction.sequence([EnemyAction, ActionDone]))

    self.addChild(Enemy2)
}

0 个答案:

没有答案