产生的敌人在spritekit / swift中效果不佳

时间:2016-01-27 10:31:09

标签: ios swift sprite-kit swift2 sprite

我想顺序产生敌人。所以为此编写了一个代码,但无法理解为什么只有两个第一个敌人出现在正确的位置,而其余的则出现在错误的位置。这是代码:

func addDoor() {
    for i in 0...1 {
        let doorSprite = SKSpriteNode(imageNamed: "Door")
        doorSprite.name = "door\(i)"
        if i == 0 {
            doorSprite.position = CGPointMake(self.frame.size.width / 2.0 - 40.0 - doorSprite.frame.size.width, self.frame.size.height - doorSprite.frame.size.height / 2.0)
        } else {
            doorSprite.position = CGPointMake(self.frame.size.width / 2.0 + 40.0 + doorSprite.frame.size.width, self.frame.size.height - doorSprite.frame.size.height / 2.0)
        }
        gameLayer.addChild(doorSprite)
    }
}

func enemySpawn() {
    for i in 0..<15 {
        let sprite = Enemy(imageNamed: "Enemy")
        sprite.name = "enemy\(i)"

        let randDoor = Int(arc4random_uniform(2))

            if randDoor == 0 {
                if let door = gameLayer.childNodeWithName("door\(randDoor)") {
                    sprite.position = CGPointMake((door.position.x - door.frame.size.width / 2.0) + spawnPoint(UInt32(door.frame.size.width - sprite.frame.size.width)) + sprite.frame.size.width / 2.0, door.position.y)
                }
            } else {
                if let door = homeLayer.childNodeWithName("door\(randDoor)") {
                    sprite.position = CGPointMake((door.position.x - door.frame.size.width / 2.0) + spawnPoint(UInt32(door.frame.size.width - sprite.frame.size.width)) + sprite.frame.size.width / 2.0, door.position.y)
                }
            }
        }

        enemiesLayer.runAction(SKAction.waitForDuration(NSTimeInterval(i)), completion: {
            self.enemiesLayer.addChild(sprite)
        })

    }
}

func spawnPoint(num: UInt32) -> CGFloat {
    let randomSpawn: CGFloat = CGFloat(arc4random_uniform(num))
    return randomSpawn
}

1 个答案:

答案 0 :(得分:0)

首先,ifelse块中的逻辑完全相同。

我不太确定你在这里尝试做什么或者randDoor变量应该如何使用,但是整个if - else块没有做任何事情。