随机化 Space 游戏 PowerUp 生成
Randomize Space Game PowerUp spawns
我有一个 Space 小游戏,我在学习 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!")
}
}
假设:您只希望其中一个电源以 30-90 秒之间的随机间隔启动,然后重置间隔并选择不同的电源并重复。
您的代码设置方式存在 2 个问题
- 您正在为所有 3 个对象设置随机间隔,它们完全有可能以相同的值结束并同时触发。
- 您正在寻找一个随机间隔并放入 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)
}
}
我有一个 Space 小游戏,我在学习 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!")
}
}
假设:您只希望其中一个电源以 30-90 秒之间的随机间隔启动,然后重置间隔并选择不同的电源并重复。
您的代码设置方式存在 2 个问题
- 您正在为所有 3 个对象设置随机间隔,它们完全有可能以相同的值结束并同时触发。
- 您正在寻找一个随机间隔并放入 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)
}
}