gpt4 book ai didi

ios - 如何使用 SpriteKit 的 SKAction 为 SKSpriteNode 创建时间间隔?

转载 作者:搜寻专家 更新时间:2023-11-01 05:53:39 24 4
gpt4 key购买 nike

我有一个函数,篮球。我希望能够以固定的时间间隔在屏幕顶部的 CGPoint(515,700) 处生成我的 basketBall 节点。我知道唯一可以完成等待几秒钟的方法是 sleep();然而,sleep() 显然不允许 SpriteNodes 保留在屏幕上,所以我需要一个替代方案。

我发现了 NSTimeInterval,但我宁愿避免导入 Foundation。我认为 SKAction 允许时间通过 waitForDuration(),但我很困惑它是如何工作的。有人可以阐明 SpriteKit 的 SKAction 吗?

override func didMoveToView(view: SKView) {

let basketBall = SKSpriteNode(imageNamed: "basketball")
let waitForObjects = SKAction.waitForDuration(3)
let basketBallFalls = SKAction.runBlock({
self.Basketball()
})
let action = SKAction.sequence([waitForObjects, basketBallFalls])


SKAction.runAction(action, onChildWithName: "basketball")
SKAction.repeatActionForever(action)
//Basketball()
}

func Basketball(){

basketBall.position = CGPointMake(515, 700)
basketBall.size = CGSizeMake(50, 50)
basketBall.size = CGSize(width: 50.0, height: 50.0)
basketBall.zPosition = 10
basketBall.physicsBody = SKPhysicsBody(circleOfRadius: 25.0)
basketBall.physicsBody?.mass = 0.8
basketBall.physicsBody?.restitution = 0.6
basketBall.physicsBody?.dynamic = true


self.addChild(basketBall)
}

最佳答案

尝试使用SKAction.waitForDuration(_:) .

例。像这样:

let waitDuration = NSTimeInterval(arc4random_uniform(20))

let waitAction = SKAction.waitForDuration(waitDuration)
let ballAction = SKAction.runBlock(self.Basketball) //call your function

runAction(SKAction.sequence([waitAction, ballAction]))

关于ios - 如何使用 SpriteKit 的 SKAction 为 SKSpriteNode 创建时间间隔?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36140703/

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