gpt4 book ai didi

xcode - 快速生成节点无限

转载 作者:行者123 更新时间:2023-11-30 10:03:05 30 4
gpt4 key购买 nike

我试图让我的节点在屏幕上移动并在计时器上无限生成。我也想同时在屏幕上显示多个节点。这是迄今为止我的稳定代码。我尝试了多种不同的方法,要么卡住,要么崩溃。

 let bunnyTexture = SKTexture(imageNamed: "oval 1.png")
bunny = SKSpriteNode(texture: bunnyTexture)
bunny.position = CGPoint(x: (size.width / 3), y: 750 + bunny.frame.height)
self.addChild(bunny)
bunny.physicsBody = SKPhysicsBody(circleOfRadius: bunnyTexture.size().height/2)
bunny.physicsBody!.dynamic = true
let spawnBunny = SKAction.moveByX(0, y: -self.frame.size.height, duration: 4)
let despawnBunny = SKAction.removeFromParent()
let spawnNdespawn2 = SKAction.sequence([spawnBunny, despawnBunny])
bunny.runAction(spawnNdespawn2)

let gobblinTexture = SKTexture(imageNamed: "oval 2.png")
gobblin = SKSpriteNode(texture: gobblinTexture)
gobblin.position = CGPoint(x: 150 + gobblin.frame.width, y: (size.height / 3))
self.addChild(gobblin)
let randomGob = arc4random() % UInt32(self.frame.size.height / 2)
let spawnGob = SKAction.moveByX(+self.frame.size.width, y: 0, duration: 4)
let despawnGob = SKAction.removeFromParent()
let spawnNdespawn = SKAction.sequence([spawnGob, despawnGob])
gobblin.runAction(spawnNdespawn)

let ground = SKNode()
ground.position = CGPointMake(0, 0)
ground.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(self.frame.size.width, -50))
ground.physicsBody!.dynamic = false
self.addChild(ground)

最佳答案

您可以使用SKAction.waitForDuration(interval)创建循环计时器。

使用新函数loopAction扩展SKNode。这需要一个 SKAction 、一个 NSTimeInterval 和一个 () -> Bool 函数。

SKAction 是在每个时间间隔执行的函数。() -> Bool 函数将用于停止否则的无限循环。

请记住,这会捕获 SKNodeSKAction。在循环停止之前,两者都不会释放。

可以创建一个带有标志 (Bool) 的对象来保存所有这些信息,并在需要停止时将标志设置为 false。我只是更喜欢这种方式。

LoopActionManager 是如何实现loopAction 的示例。您需要一个 SKNode 和一个 SKAction。您不是在 SKNode 上调用 runAction,而是调用 loopAction 并传递间隔和控制何时停止的函数。 您可以将此代码放置在您有权访问相关节点的位置。

停止函数也可以作为尾随闭包实现

将代码粘贴到 Playground 中以查看其工作原理。

import UIKit
import SpriteKit
import XCPlayground



extension SKNode {

func loopAction(action:SKAction,interval:NSTimeInterval,continueLoop:() -> Bool) {

guard continueLoop() else { return }

runAction(SKAction.waitForDuration(interval)) {

if continueLoop() {
self.runAction(action)
self.loopAction(action, interval: interval, continueLoop: continueLoop)
}
}
}
}

// example
class LoopActionManager {

let node = SKSpriteNode(color: UIColor.whiteColor(), size: CGSize(width: 20, height: 20))
let action = SKAction.moveBy(CGVector(dx: 10,dy: 0), duration: 0.5)

func continueMoveLoop() -> Bool {
return (node.position.x + node.size.width) < node.scene?.size.width
}

func start() {
node.loopAction(action, interval: 1, continueLoop: continueMoveLoop)
}

}

let view = SKView(frame: CGRect(x: 0, y: 0, width: 200, height: 200))
let scene = SKScene(size: view.frame.size)
view.presentScene(scene)

let example = LoopActionManager()
scene.addChild(example.node)
example.start()



XCPlaygroundPage.currentPage.liveView = view
<小时/>

正如下面的评论中所述,也可以使用repeatAction。您可以使用static func 扩展SKAction,以根据要重复的操作和间隔生成repeatAction。与第一个解决方案相比,这个解决方案更简单并且更符合SDK。您确实会丢失每个间隔的完成处理程序。

import UIKit
import SpriteKit
import XCPlayground


extension SKAction {

static func repeatAction(action:SKAction,interval:NSTimeInterval) -> SKAction {

// diff between interval and action.duration will be the wait time. This makes interval the interval between action starts. Max of 0 and diff to make sure it isn't smaller than 0
let waitAction = SKAction.waitForDuration(max(0,interval - action.duration))
let sequenceAction = SKAction.sequence([waitAction,action])
let repeatAction = SKAction.repeatActionForever(sequenceAction)

return repeatAction

}
}


let view = SKView(frame: CGRect(x: 0, y: 0, width: 200, height: 200))
let scene = SKScene(size: view.frame.size)
view.presentScene(scene)

let node = SKSpriteNode(color: UIColor.whiteColor(), size: CGSize(width: 20, height: 20))
let action = SKAction.moveBy(CGVector(dx: 10,dy: 0), duration: 0.5)

scene.addChild(node)
node.runAction(SKAction.repeatAction(action, interval: 1.0))



XCPlaygroundPage.currentPage.liveView = view

关于xcode - 快速生成节点无限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37397833/

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