2017-03-18 18 views
0

我在Swift操场上制作了一个小型的SpriteKit游戏,它是一种小行星防御类型的游戏。基本上,玩家用大炮捍卫宇宙飞船,并且必须摧毁靠近的岩石。下面的图片链接是我的场景,没有添加枪,但基本上我希望岩石从顶部向底部的飞船缓慢移动(对不起,这太大了,无法在此添加)。SKSpriteNode在Swift中受约束的随机向下运动

https://www.dropbox.com/s/pisgazar4jaxra0/Screenshot%202017-03-18%2011.23.17.png?dl=0

岩石将是SKSpriteNodes,我想加速它们对于每个连续的岩石随时间增加难度。

我将如何能够实现这一点。我在其他地方看到过有关使用UIBezierPaths的一些答案,但是我怎样才能使它随机化,但是限制了岩石只能从上面落下?

在此先感谢!

+0

问题太多...尝试专注于一个特定的问题。否则,你的问题可能会被关闭。关于你的问题的提示很少......如果你的岩石会沿着直线走,你不需要更加缓慢的路径。你可以使用动作产卵/移动/移除岩石。 – Whirlwind

回答

1

如果你只需要它们从上到下没有任何特殊的曲线轨迹,那么你不需要处理样条曲线。

让我们先解决渐进加速。你希望他们在开始时缓慢下降,并在比赛进程中加速。一种方法是跟踪级别时间并根据经过的级别时间进行一些持续时间数学计算。一个更容易的方法是减少小行星到达底部的时间。在你的场景声明部分,声明这些:

fileprivate let startingDuration: TimeInterval = 6.0 // Put here whatever value you feel fit for the level start falling speed 
fileprivate let difficultyIncrease: TimeInterval = 0.05 // Put here whatever value you feel fit for how much should each next asteroid accelerate 
fileprivate let collisionPosition = CGPoint(x: 300, y: 200) // Put here the onscreen location where the asteroids should be heading to 
fileprivate var asteroidCounter = 0 // Increase this counter every time you spawn a new asteroid 

现在,我们来看看实际的小行星运动。有不同的方式来处理它。苹果推荐的最简单的方法就是为此采取行动。此代码添加到您的小行星产卵方法:如果你需要随机整数生成功能

// Implement here your spawning code ... 
// It's best to choose random starting points (X axis) a bit higher than the upper screen boundary 

let fallingDuration: TimeInterval = startingDuration - difficultyIncrease * TimeInterval(asteroidCounter) 
let fallingAction = SKAction.move(to: collisionPosition, duration: fallingDuration) 
asteroid.runAction(fallingAction) 

,试试这个:

func RandomInt(min: Int, max: Int) -> Int { 
    return Int(arc4random_uniform(UInt32(max-(min-1)))) + min 
} 
+0

非常感谢!这正是我所期待的。 – QPDev