影响随机产卵

时间:2018-03-11 21:13:31

标签: ios swift

所以我得到了我的游戏,它有4个车道,从下到上,其中头盔在很长一段时间内永远产生。
我想检查产生的4个头盔中是否至少有一个包含我给他的物体 这是我在Lane.swift中产生泳道的代码:

private func spawnRandomHelmet(){
        let helmet = HelmetNode(fruit: .random())
        helmet.delegate = self
        helmet.position = CGPoint(x: 0, y: 0 - helmet.size.height)
        addChild(helmet)

        let moveHelmetUp = SKAction.moveTo(y: self.size.height - 285 - (helmet.size.height - 100), duration: self.config.helmetSpeed)
        helmet.run(moveHelmetUp) {
            let moveHelmetToScreenEnd = SKAction.moveTo(y: self.size.height + helmet.size.height, duration: 0.5)
            let fadeOutHelmet = SKAction.fadeOut(withDuration: 0.5)
            let moveAndFadeOut = SKAction.group([moveHelmetToScreenEnd, fadeOutHelmet])
            helmet.run(moveAndFadeOut) {
                helmet.removeFromParent()
                self.delegate?.removed(helmet.fruit)
            }
        }
    }

    public func startSpawning() {
        let wait = SKAction.wait(forDuration: config.delayDuration, withRange: config.delayVariance)
        let spawnHelmet = SKAction.run(spawnRandomHelmet)
        let spawnSequence = SKAction.sequence([wait, spawnHelmet])
        let spawnForever = SKAction.repeatForever(spawnSequence)
        run(spawnForever)
    }

我在我的类GameScene.swift中调用该函数:

private func setUpLanes() {
    //Remove lanes from latest round
    for lane in [firstLane, secondLane, thirdLane, fourthLane] {
        lane.removeFromParent()
    }
    //Add lanes for current round
    for lane in [firstLane, secondLane, thirdLane, fourthLane] {
        lane.delegate = self
        addChild(lane)
        lane.startSpawning()
    }
}

如果一个头盔包含我正在寻找的对象,我不知道如何检查。每个产卵都有一个随机给头盔的物体 这就是我将对象添加到头盔的方式:

class HelmetNode: SKSpriteNode {
weak var delegate: HelmetNodeDelegate?
let fruit: Fruit

init(fruit: Fruit) {
    self.fruit = fruit
    let texture = SKTexture(imageNamed: "helmet \(fruit.name)")
    let size = CGSize(width: 150, height: 250)
    super.init(texture: texture, color: .clear, size: size)
    isUserInteractionEnabled = true
    zPosition = 2
    anchorPoint = CGPoint(x: 0, y: 0)
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

static func random(count: Int = 4) -> [HelmetNode] {
    var helmets: [HelmetNode] = []
    for _ in 1...count  {
        helmets.append(HelmetNode(fruit: .random()))
    }
    return helmets
}

1 个答案:

答案 0 :(得分:0)

如果要将对象添加为头盔的子项,请为对象添加名称

object.name = “special object”

然后运行:

for child in self.children {
    print(child.name)
}

当头盔产生时(但应在添加物体后)