【发布时间】:2016-05-26 17:02:25
【问题描述】:
说明
我正在构建一个我面临一些问题的游戏。我有两个按照相同规则运行的对象(A 和 B):从上到下,它们产生、向下移动并在到达边缘时被移除。它们中的每一个在生成之前都有其特定的持续时间,这样,有时它们会被覆盖(如下所示)。
问题
问题很简单:有没有办法让 A 和 B 产生不同的持续时间,但仍然是随机的?
代码
如果你愿意,可以下载这个项目here。
import SpriteKit
class GameScene: SKScene {
//Declarations
var A = SKSpriteNode()
var B = SKSpriteNode()
var durationA = CGFloat()
var durationB = CGFloat()
//Setup
func setupA(){
A = SKSpriteNode(imageNamed: "A")
A.position = CGPoint(x: self.frame.width / 2, y: self.frame.height)
}
func setupB(){
B = SKSpriteNode(imageNamed: "B")
B.position = CGPoint(x: self.frame.width / 2, y: self.frame.height)
}
//Actions
func actionsA(){
//Start spawning, moving and removing
let spawnA = SKAction.runBlock({
() in
//Spawn A
self.setupA()
self.addChild(self.A)
//Move left and remove when go off screenk
let frameHeight = CGFloat(self.frame.height)
let moveA = SKAction.moveByX(0, y: -frameHeight, duration: NSTimeInterval(0.0028 * frameHeight)) //duration: faster or slower
let removeA = SKAction.removeFromParent()
let moveAndRemoveA = SKAction.sequence([moveA, removeA])
self.A.runAction(moveAndRemoveA)
})
//Spawn A each 1.75~2.25 seconds
durationA = (CGFloat(arc4random_uniform(51)) + 175.0) / 100.0
let spawnAfterDurationA = SKAction.repeatActionForever(SKAction.sequence([SKAction.waitForDuration(Double(durationA)), spawnA]))
runAction(spawnAfterDurationA)
}
func actionsB(){
//Start spawning, moving and removing
let spawnB = SKAction.runBlock({
() in
//Spawn B
self.setupB()
self.addChild(self.B)
//Move left and remove when go off screen
let frameHeight = CGFloat(self.frame.height)
let moveB = SKAction.moveByX(0, y: -frameHeight, duration: NSTimeInterval(0.0028 * frameHeight)) //duration: faster or slower
let removeB = SKAction.removeFromParent()
let moveAndRemoveB = SKAction.sequence([moveB, removeB])
self.B.runAction(moveAndRemoveB)
})
//Spawn B each 0.5~1.0 seconds
durationB = (CGFloat(arc4random_uniform(51)) + 50.0) / 100.0
let spawnAfterDurationB = SKAction.repeatActionForever(SKAction.sequence([SKAction.waitForDuration(Double(durationB)), spawnB]))
runAction(spawnAfterDurationB)
}
override func didMoveToView(view: SKView) {
/* Setup your scene here */
actionsA()
actionsB()
}
}
蒂莫西·史密斯的尝试
如果需要,可以here下载。
import SpriteKit
class GameScene: SKScene {
//Declarations
var A = SKSpriteNode()
var B = SKSpriteNode()
//Setup
func setupA(){
A = SKSpriteNode(imageNamed: "A")
A.position = CGPoint(x: self.frame.width / 2, y: self.frame.height)
}
func setupB(){
B = SKSpriteNode(imageNamed: "B")
B.position = CGPoint(x: self.frame.width / 2, y: self.frame.height)
}
//Actions
func actionsA(){
//Spawn, move and remove
let spawnA = SKAction.runBlock({
() in
//Spawn A
self.setupA()
self.addChild(self.A)
//Move left and remove when go off screenk
let frameHeight = CGFloat(self.frame.height)
let moveA = SKAction.moveByX(0, y: -frameHeight, duration: NSTimeInterval(0.0028 * frameHeight)) //duration: faster or slower
let removeA = SKAction.removeFromParent()
let moveAndRemoveA = SKAction.sequence([moveA, removeA])
self.A.runAction(moveAndRemoveA)
})
runAction(spawnA)
}
func actionsB(){
//Spawn, move and remove
let spawnB = SKAction.runBlock({
() in
//Spawn B
self.setupB()
self.addChild(self.B)
//Move left and remove when go off screen
let frameHeight = CGFloat(self.frame.height)
let moveB = SKAction.moveByX(0, y: -frameHeight, duration: NSTimeInterval(0.0028 * frameHeight)) //duration: faster or slower
let removeB = SKAction.removeFromParent()
let moveAndRemoveB = SKAction.sequence([moveB, removeB])
self.B.runAction(moveAndRemoveB)
})
runAction(spawnB)
}
//Spawn
func spawn(){
let random = Int(arc4random_uniform(10)+1) //pick a number from 1 to 10
if random <= 8{
actionsB()
}
else{
actionsA()
}
}
override func didMoveToView(view: SKView) {
/* Setup your scene here */
runAction(SKAction.repeatActionForever(SKAction.sequence([SKAction.waitForDuration(0.5, withRange: 0.25), SKAction.runBlock(self.spawn)])))
}
}
在此先感谢,
路易斯。
【问题讨论】:
标签: ios swift sprite-kit