我有一个小型的太空游戏,我在学习SpriteKit和Swift时已经工作了一个月左右。我把所有的东西都按照我想要的方式工作,除了一件事,无论我尝试什么,我都会得到错误或接近但不希望的效果。
我的问题是每XX秒产生1个随机电源。我现在拥有的代码可以使用,但不会产生1,它会在30到90秒的设定间隔内同时产生1或2甚至3个。
我尝试了几种不同的arc4随机方式,但我无法弄清楚如何做到这一点。有人可以指导我走向正确的方向。这是我在比赛结束前的最后一道障碍,现在已经困扰了我几个星期。
我工作时使用太阳能的代码,但不是他想要的效果:
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!")
}
}
答案 0 :(得分:0)
假设:你只希望其中一个电源按照30-90秒之间的随机间隔发射,然后重置间隔并选择不同的电源并重复。
如何设置代码存在2个问题
有很多方法可以实现这一目标,这是一个非常基本的例子,可以帮助你。它使用递归并以永远触发的方式调用自身。
//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秒之间的新间隔,然后选择一个新的随机电源来重复并重复,并重复和重复...
编辑
与问题无关......
我意识到你刚开始使用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)
}
}