我正在SpriteKit制作游戏,在这个游戏中我有平台产生,我想制作它,以便宝石或硬币等物体在产生时有机会在平台上产生
我需要它是随机的,也可以选择它产生的东西(无论是宝石还是硬币)
有什么想法吗?
答案 0 :(得分:1)
要正确使用堆栈溢出,您应该总是发布一些代码。如果你只是希望他们为你工作,人们就无济于事。
最好的方法是将您的平台子类化,这是它背后的基本思想。
enum PlatformObject: Int {
case Gem = 0
case Coin
}
class Platform: SKSpriteNode {
init (size: CGSize, color: SKColor, objectType: PlatformObject, spawnObjectRandomly: Bool) { // create your own init for your needs
super.init (texture: nil, color: color, size: size)
// set up platform properties
// Than spawn object
if spawnObjectRandomly {
spawnRandomObject()
} else if objectType == .Coin {
spawnCoin()
} else if objectType == .Gem {
spawnGem()
}
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func spawnRandomObject() {
let randomNumber = Int(arc4random() % 2) // 0 and 1
if randomNumber == 0 {
// spawn gem
spawnGem()
} else {
// spawn coin
spawnCoin()
}
}
func spawnCoin() {
let coin = SKSpriteNode(...
}
func spawnGem() {
let gem = SKSpriteNode(...
}
}
在你的场景中,你会产生像这样的平台
class GameScene: SKScene {
let size = // set your size
let color = // set color
let platform1 = Platform(size: size, color: color, objectType: .Gem, spawnObjectRandomly: false)
...
// if false will spawn selected objectType ("Gem" in this example)
let platform2 = Platform(size: size, color: color, objectType: .Gem, spawnObjectRandomly: true)
// if true will spawn random object regardless of objectType settings
...
}
希望这有帮助