gpt4 book ai didi

ios - 在不叠加的情况下以随机持续时间生成对象

转载 作者:可可西里 更新时间:2023-11-01 02:16:50 27 4
gpt4 key购买 nike

解释

我正在制作一款游戏,但遇到了一些问题。我有两个按相同规则运行的对象(A 和 B):从上到下,它们生成、向下移动并在到达边缘时被移除。它们中的每一个在生成之前都有其特定的持续时间,这样,有时它们会被覆盖(如下所示)。

enter image description here


问题

问题很简单:有没有办法让 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()
}
}

Timothy Smith 的尝试

喜欢的可以下载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)])))
}
}


提前致谢,
路易斯。

最佳答案

一种可能的解决方案是使用单个函数 spawn,每 0.25 到 1.00 秒随机调用一次。在函数内部,随机选择 A 或 B,并生成它。您似乎希望 B 比 A 出现得更频繁,因此您可以使用加权随机性(选择 0.45 到 1 之间的数字并四舍五入到最接近的整数)。

这是否有效取决于超过 1 秒的 B 间隔是否可以接受。

关于ios - 在不叠加的情况下以随机持续时间生成对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37467019/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com