我得到了我的游戏,我的四个车道从屏幕底部向上移动到顶部。我得到了一个在我的场景中显示的正确的对象,我在一个产生的波中产生随机对象,每个通道1个,所以每次4个,我想检查4个对象中至少有一个是正确的对象,如果没有,删除并生成新的4个对象,直到你有一个权限。
有时你需要等待10-20秒,直到出现一个无聊的正确物体并让你离开游戏流程
以下代码生成头盔:
private func spawnRandomHelmet() {
let helmet = HelmetNode(fruit: .random())
helmet.delegate = self
helmet.position = CGPoint(x: 0, y: 0 - helmet.size.height)
addChild(helmet)
let endOfFieldForHelmet = self.size.height * 0.7864 - (helmet.size.height - screenWidth / 7.5)
let helmetOutOfScreen = self.size.height + helmet.size.height
let moveHelmetUp = SKAction.moveTo(y: endOfFieldForHelmet, duration: self.config.helmetSpeed)
helmet.run(moveHelmetUp) {
let moveHelmetOutOfScreen = SKAction.moveTo(y: helmetOutOfScreen, duration: 0.5)
let fadeOutHelmet = SKAction.fadeOut(withDuration: 0.5)
let moveAndFadeOut = SKAction.group([moveHelmetOutOfScreen, fadeOutHelmet])
helmet.run(moveAndFadeOut) {
helmet.removeFromParent()
self.delegate?.removed(helmet.fruit)
}
}
}
以下代码来自HelmetNode.swift:
class HelmetNode: SKSpriteNode {
weak var delegate: HelmetNodeDelegate?
let fruit: Fruit
init(fruit: Fruit) {
self.fruit = fruit
let texture = SKTexture(imageNamed: "helmet \(fruit.name)")
let size = CGSize(width: laneWidth, height: screenWidth / 3)
super.init(texture: texture, color: .clear, size: size)
isUserInteractionEnabled = true
zPosition = 2
anchorPoint = CGPoint(x: 0, y: 0)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
static func random(count: Int = 4) -> [HelmetNode] {
var helmets: [HelmetNode] = []
for _ in 1...count {
helmets.append(HelmetNode(fruit: .random()))
}
return helmets
}
}
以下代码来自Fruit.swift:
enum Fruit: Int {
case grapes = 0
case orange
case pear
case cherry
case pineapple
static var count: UInt32 = 5
var name: String {
switch self {
case .grapes:
return "grapes"
case .cherry:
return "cherry"
case .orange:
return "orange"
case .pear:
return "pear"
case .pineapple:
return "pineapple"
}
}
static func random() -> Fruit {
let randomNumber = Int(arc4random_uniform(count))
return Fruit(rawValue: randomNumber)!
}
}
正确的水果在我的GameScene.swift中初始化为fruit
答案 0 :(得分:0)
有太多未知变量可以准确地告诉您如何布局代码,例如HelemtNode代码中的内容,fruit(enum,class,struct)的类型,random()代码中的内容。< / p>
所以这只是如何做到这一点的指导原则,但必须进行调整以更好地适应您的代码。
//func used to create all 4 helmets
func spawnHelmets() {
//find the correct type first
let correctType = HelmetNode.random()
//spawn the correct type
spawnRandomHelmetOf(type: correctType)
//spawn the remaining 3 but as types other than correct
for x in 0...3 {
let randomType = randomNotOf(type: correctType)
spawnRandomHelmetOf(type: randomType )
}
}
//recursive function to find random type NOT of a certain type
func randomNotOf(type: FruitType) -> FruitType {
//you'll have to figure out your own random variables here these are unknown to me
let random = Int(arc4random_uniform(UInt32(maxTypes)))
if random != type {
//doesn't match the type so we can return it
return random as! FruitType
}
else {
//using recursion will run the loop until it finds a match that is not of the same type
return randomNotOf(type: type)
}
}
private func spawnRandomHelmetOf(type: FruitType) {
let helmet = HelmetNode(fruit: type)
helmet.delegate = self
helmet.position = CGPoint(x: 0, y: 0 - helmet.size.height)
addChild(helmet)
//add your positioning code
}