我正在构建一个我面临一些问题的游戏。我有两个按照相同规则运行的对象(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()
}
}
<2>蒂莫西史密斯的尝试
如果需要,可以下载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)])))
}
}
提前谢谢,
路易斯。
答案 0 :(得分:1)
一种可能的解决方案是使用单个函数spawn
,每0.25到1.00秒随机调用一次。在函数内部,随机选择A或B,并生成它。看起来你想要B比A更频繁出现,所以你可以使用加权随机性(从0.45到1选择一个数字并舍入到最接近的整数)。
这是否有效取决于B分离是否超过1秒是否可以接受。
答案 1 :(得分:0)
每次运行(arc4random_uniform(50)+51)/100
时,它都会为您提供0.5到1.0秒之间的随机持续时间。您可以使用以下代码生成A
&amp;的随机延迟B
durationA = (arc4random_uniform(50)+51)/100
durationB = (arc4random_uniform(50)+51)/100
此外,您可以运行for循环以将durationB与durationA进行比较,并且仅在durationB
与durationA
不同时才接受$(function(){
var hasBeenTrigged = false;
$(window).scroll(function() {
if ($(this).scrollTop() >= 100) { // if scroll is greater/equal then 100 and hasBeenTrigged is set to false.
$("#sticky_nav").show();
hasBeenTrigged = true;
} else {
$("#sticky_nav").hide();
}
});
})
。