gpt4 book ai didi

xcode - 如何在可以打开和关闭的循环中添加 SKnode

转载 作者:行者123 更新时间:2023-11-28 06:44:24 25 4
gpt4 key购买 nike

我正在创建一个游戏,但我的与操纵杆相关联的子弹生成方法一直出现此错误。我想在操纵杆处于事件状态时重复生成项目符号节点。这是我如何创建一个触发方法

class GameScene: SKScene, SKPhysicsContactDelegate {

let bullet1 = SKSpriteNode(imageNamed: "bullet.png")

override func didMoveToView(view: SKView) {

if fireWeapon == true {

NSTimer.scheduledTimerWithTimeInterval(0.25, target: self,
selector: Selector ("spawnBullet1"), userInfo: nil, repeats: true)
}
}
func spawnBullet1(){

self.addChild(bullet1)

bullet1.position = CGPoint (x: hero.position.x , y:hero.position.y)
bullet1.xScale = 0.5
bullet1.yScale = 0.5
bullet1.physicsBody = SKPhysicsBody(rectangleOfSize: bullet1.size)
bullet1.physicsBody?.categoryBitMask = PhysicsCategory.bullet1
bullet1.physicsBody?.contactTestBitMask = PhysicsCategory.enemy1
bullet1.physicsBody?.affectedByGravity = false
bullet1.physicsBody?.dynamic = false
}
override func touchesBegan(touches: Set<UITouch>, withEvent
event:UIEvent?) {

for touch in touches {
let location = touch.locationInNode(self)

let node = nodeAtPoint(location)
if (CGRectContainsPoint(joystick.frame, location)) {
stickActive = true

if stickActive == true {

fireWeapon = true
}

}
override func touchesEnded(touches: Set<UITouch>, withEvent event:
UIEvent?) {

fireWeapon = false
}

第一个项目符号按计划启动并且运行良好,但是,每次第二个项目符号启动时应用程序崩溃并且我收到此错误“试图添加一个已经有父级的 SKNode”。谁能告诉我另一种方法

最佳答案

您的问题与错误所说的完全一样,您需要先从其父项中删除项目符号,然后再重新添加它,或者将 bullet1 设为 spawnBullet 中的本地属性函数,这样每次您调用该函数时,都会创建一个新项目符号并将其作为子项添加到场景中,而不是尝试重新添加相同的项目符号。

关于xcode - 如何在可以打开和关闭的循环中添加 SKnode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36927902/

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