我想知道是否有人可以告诉我如何制作它以便我可以产生随机图像导弹。现在我正在使用一个名为" meteor"的图像,我还有一些我希望显示和随机化的图像。我知道我需要将它们放在一个数组中并随机创建一个弧。我已经完成了声音,但我不确定如何为图像做到这一点。到目前为止这是我的代码。
var lastMissileAdded : NSTimeInterval = 0.0
let missileVelocity : CGFloat = 4.0
func addMissile() {
// Initializing missile node
var missile = SKSpriteNode(imageNamed: "meteor")
missile.setScale(0.44)
// Adding SpriteKit physics body for collision detection
missile.physicsBody = SKPhysicsBody(rectangleOfSize: missile.size)
missile.physicsBody?.categoryBitMask = UInt32(obstacleCategory)
missile.physicsBody?.dynamic = true
missile.physicsBody?.contactTestBitMask = UInt32(shipCategory)
missile.physicsBody?.collisionBitMask = 0
missile.physicsBody?.usesPreciseCollisionDetection = true
missile.name = "missile"
// Selecting random y position for missile
var random : CGFloat = CGFloat(arc4random_uniform(300))
missile.position = CGPointMake(self.frame.size.width + 20, random - 20)
self.addChild(missile)
}
func moveObstacle() {
self.enumerateChildNodesWithName("missile", usingBlock: { (node, stop) -> Void in
if let obstacle = node as? SKSpriteNode {
obstacle.position = CGPoint(x: obstacle.position.x - self.missileVelocity, y: obstacle.position.y)
if obstacle.position.x < 0 {
obstacle.removeFromParent()
}
}
})
}
答案 0 :(得分:1)
您需要做的就是将它们命名为meteor0,meteor1和meteor2,并使用String Interpolation以随机图像创建节点:
var missile = SKSpriteNode(imageNamed: "meteor\(arc4random_uniform(3))")