OGeek|极客世界-中国程序员成长平台

标题: ios - 随机空间游戏 PowerUp 生成 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 20:03
标题: ios - 随机空间游戏 PowerUp 生成

在学习 SpriteKit 和 Swift 的同时,我已经开发了一个月左右的小型太空游戏。除了一件事,我让一切都按照我的意愿工作,无论我尝试什么,我都会得到错误或关闭但不想要的效果。

我的问题是每 XX 秒产生 1 个随机能量。我现在的代码可以工作,但不会产生 1,它会在 30 到 90 秒之间的设定间隔内同时产生 1 或 2 甚至 3 个。

我尝试了几种不同的 arc4random 方法,但我不知道该怎么做。有人可以引导我走向正确的方向。这是我在游戏结束前的最后一道关卡,它已经困扰我好几个星期了。

我使用solar的代码,虽然工作,但不是他想要的效果:

    func startNewLevel(){

    levelNumber += 1

    if self.action(forKey: "spawningEnemies") != nil{
        self.removeAction(forKey: "spawningEnemies")
    }

    if self.action(forKey: "spawningUfos") != nil{
        self.removeAction(forKey: "spawningUfos")
    }

    if self.action(forKey: "spawningLifePowerUps") != nil{
        self.removeAction(forKey: "spawningLifePowerUps")
    }

    if self.action(forKey: "spawningShieldPowerUps") != nil{
        self.removeAction(forKey: "spawningShieldPowerUps")
    }

    if self.action(forKey: "spawningRapidFirePowerUps") != nil{
        self.removeAction(forKey: "spawningRapidFirePowerUps")
    }

    var levelDuration = TimeInterval()
    // Change the levelDuration = X.X to what ever you want to determin your game diffculty. The higher the number the slow the game and vice versa
    switch levelNumber {
    case 1: levelDuration = 3.0 //Easy
    case 2: levelDuration = 2.8
    case 3: levelDuration = 2.6
    case 4: levelDuration = 2.4
    case 5: levelDuration = 2.2
    case 6: levelDuration = 2.0 //Medium
    case 7: levelDuration = 1.8
    case 8: levelDuration = 1.6
    case 9: levelDuration = 1.4
    case 10: levelDuration = 1.2
    case 11: levelDuration = 1.0 //Hard
    case 12: levelDuration = 0.8
    case 13: levelDuration = 0.6
    case 14: levelDuration = 0.4
    case 15: levelDuration = 0.2 //Insane
    default:
        levelDuration = 0.2
        print("Cannot find levelinfo")
    }

    let upperBonuslimit : UInt32 = 90 // Default 120 seconds
    let lowerBonuslimit : UInt32 = 60 // Default 60 seconds

    let upperlimit : UInt32 = 90 // Default 90 seconds
    let lowerlimit : UInt32 = 30 // Default 30 seconds

    let spawn = SKAction.run(spawnEnemy)
    let waitToSpawn = SKAction.wait(forDuration: levelDuration)
    let spawnSequence = SKAction.sequence([waitToSpawn, spawn])
    let spawnForever = SKAction.repeatForever(spawnSequence)
    self.run(spawnForever, withKey: "spawningEnemies")

    let spawnUfoEnemy = SKAction.run(spawnUfo)
    let waitToSpawnUfo = TimeInterval(CGFloat(arc4random() % ((upperBonuslimit - lowerBonuslimit) * 10) + lowerBonuslimit * 10)/10.0)
    let randomWaitUfo = SKAction.wait(forDuration: waitToSpawnUfo)
    let spawnUfoSequence = SKAction.sequence([randomWaitUfo, spawnUfoEnemy])
    let spawnUfoForever = SKAction.repeatForever(spawnUfoSequence)
    self.run(spawnUfoForever, withKey: "spawningUfos")

    let spawnLife = SKAction.run(spawnALifePowerUp)
    let waitToSpawnLife = TimeInterval(CGFloat(arc4random() % ((upperlimit - lowerlimit) * 10) + lowerlimit * 10)/10.0)
    let randomWait = SKAction.wait(forDuration: waitToSpawnLife)
    let spawnSequenceLife = SKAction.sequence([randomWait, spawnLife])
    let spawnForeverLife = SKAction.repeatForever(spawnSequenceLife)
    self.run(spawnForeverLife, withKey: "spawningLifePowerUps")

    let spawnShield = SKAction.run(spawnAShieldPowerUp)
    let waitToSpawnShield = TimeInterval(CGFloat(arc4random() % ((upperlimit - lowerlimit) * 10) + lowerlimit * 10)/10.0)
    let randomWait2 = SKAction.wait(forDuration: waitToSpawnShield)
    let spawnSequenceShield = SKAction.sequence([randomWait2, spawnShield])
    let spawnForeverShield = SKAction.repeatForever(spawnSequenceShield)
    self.run(spawnForeverShield, withKey: "spawningShieldPowerUps")

    let spawnRapidFire = SKAction.run(spawnARapidFirePowerUp)
    let waitToSpawnRapidFire = TimeInterval(CGFloat(arc4random() % ((upperlimit - lowerlimit) * 10) + lowerlimit * 10)/10.0)
    let randomWait3 = SKAction.wait(forDuration: waitToSpawnRapidFire)
    let spawnSequenceRapidFire = SKAction.sequence([randomWait3, spawnRapidFire])
    let spawnForeverRapidFire = SKAction.repeatForever(spawnSequenceRapidFire)
    self.run(spawnForeverRapidFire, withKey: "spawningRapidFirePowerUps")
}

如果有什么我需要更好地解释的,请告诉我。或者如果我需要添加任何代码。

这是 Powerup 功能代码之一,以防它需要膨胀来理解我的问题:

 func spawnALifePowerUp(){
    if livesNumber < 5 {
    let randomXStart = random(min: gameArea.minX, max: gameArea.maxX)
    let randomXEnd = random(min: gameArea.minX, max: gameArea.maxX)

    let startPoint = CGPoint(x: randomXStart, y: self.size.height * 1.2)
    let endPoint = CGPoint(x: randomXEnd, y: -self.size.height * 0.2)

    let lifePower = SKSpriteNode(imageNamed: "lifePowerUp")
    lifePower.name = "LifePu"
    lifePower.setScale(1.2)
    lifePower.position = startPoint
    lifePower.zPosition = 3
    lifePower.physicsBody = SKPhysicsBody(rectangleOf: lifePower.size)
    lifePower.physicsBody!.affectedByGravity = false
    lifePower.physicsBody!.categoryBitMask = PhysicsCategories.LifePu
    lifePower.physicsBody!.collisionBitMask = PhysicsCategories.None
    lifePower.physicsBody!.contactTestBitMask = PhysicsCategories.Player | PhysicsCategories.ShieldActive
    lifePower.physicsBody!.isDynamic = true
    self.addChild(lifePower)

    let lifeRotation:SKAction = SKAction.rotate(byAngle: CGFloat.pi * 2, duration: 5)
    let repeatLifeRotation:SKAction = SKAction.repeatForever(lifeRotation)

    let moveLifeHeart = SKAction.move(to: endPoint, duration: 3.5) // Life Power Up move Speed
    let deleteLifeHeart = SKAction.removeFromParent()
    let lifeHeartSequence = SKAction.sequence([moveLifeHeart, deleteLifeHeart])

    if currentGameState == gameState.inGame{
        lifePower.run(lifeHeartSequence)
        lifePower.run(repeatLifeRotation)
    }
    let dx = endPoint.x - startPoint.x
    let dy = endPoint.y - startPoint.y
    let amoutToRotate = atan2(dy, dx)
    lifePower.zRotation = amoutToRotate
    } else {
        print("Life meter FULL!")
    }
}



Best Answer-推荐答案


假设:您只希望其中一个电源以 30-90 秒之间的随机间隔触发,然后重置间隔并选择不同的电源并重复。

您的代码设置方式存在 2 个问题

  1. 您正在为所有 3 个对象设置随机间隔,它们完全有可能最终具有相同的值并同时触发。
  2. 您正在寻找一个随机间隔并放入一个 SKAction 并重复该操作。该操作获取该值的快照并重复该快照。如果您的随机间隔生成 35 秒。它总是会在 35 秒后触发。它不会在每次 SKAction 重复时重新运行随机化器。

有很多方法可以做到这一点,这里有一个非常基本的示例,可以帮助您上路。它使用递归并以这种方式调用自己,它会永远触发。

//somewhere in your setup code
spawnPowerup()

...

func spawnPowerup() {

    let upperlimit: UInt32 = 90
    let lowerlimit: UInt32 = 30

    let waitToSpawnLife = TimeInterval(CGFloat(arc4random() % ((upperlimit - lowerlimit) * 10) + lowerlimit * 10) / 10.0)
    print("waitToSpawnLife \(waitToSpawnLife)")

    self.run(.wait(forDuration: waitToSpawnLife)) {

        let powerup = Int(arc4random_uniform(UInt32(3)))
        print("powerup \(powerup)")

        var spawnPowerUp: SKAction!

        if powerup == 0 {
            spawnPowerUp = SKAction.run(self.spawnALifePowerUp)
            self.run(spawnPowerUp, withKey: "spawningLifePowerUps")
        }
        else if powerup == 1 {
            spawnPowerUp = SKAction.run(self.spawnAShieldPowerUp)
            self.run(spawnPowerUp, withKey: "spawningShieldPowerUps")
        }
        else if powerup == 2 {
            spawnPowerUp = SKAction.run(self.spawnARapidFirePowerUp)
            self.run(spawnPowerUp, withKey: "spawningRapidFirePowerUps")
        }

        self.spawnPowerup()
    }
}

此代码将选择一个随机间隔等待 30-90 秒,然后触发其中一个电源功能。然后它会调用自己并重新选择一个 30-90 秒之间的新间隔并选择一个新的随机电源来触发并重复,然后重复和重复......

EDIT

与问题无关...

我知道您刚刚开始使用 Spritekit,但您可能正处于应该开始考虑优化某些代码的阶段。开始优化的最简单方法是为多个对象寻找相同的代码,然后多次重新运行并尝试合并它。就像我如何整合您的生成电源操作一样。

例如...

if self.action(forKey: "spawningEnemies") != nil{
    self.removeAction(forKey: "spawningEnemies")
}

if self.action(forKey: "spawningUfos") != nil{
    self.removeAction(forKey: "spawningUfos")
}

if self.action(forKey: "spawningLifePowerUps") != nil{
    self.removeAction(forKey: "spawningLifePowerUps")
}

if self.action(forKey: "spawningShieldPowerUps") != nil{
    self.removeAction(forKey: "spawningShieldPowerUps")
}

if self.action(forKey: "spawningRapidFirePowerUps") != nil{
    self.removeAction(forKey: "spawningRapidFirePowerUps")
}

可能会...

removeRunningActionBy(key: "spawningEnemies")
removeRunningActionBy(key: "spawningUfos")
removeRunningActionBy(key: "spawningLifePowerUps")
removeRunningActionBy(key: "spawningShieldPowerUps")
removeRunningActionBy(key: "spawningRapidFirePowerUps")

func removeRunningActionBy(key: String) {
    if self.action(forKey: key) != nil{
        self.removeAction(forKey: key)
    }
}

关于ios - 随机空间游戏 PowerUp 生成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49883901/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://jike.in/) Powered by Discuz! X3.4