试图添加一个已经有一个父级 swift 的 SKNode



我正在创建一个游戏,但我的子弹生成方法与操纵杆链接,不断收到此错误。我想在操纵杆处于活动状态时重复生成节点 这是我创建触发方法的方式

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
}

第一颗子弹按计划启动并且效果很好,但是,每次第二颗子弹启动应用程序时,应用程序都会崩溃并且我收到错误。 有人可以告诉我创建射速的替代方法

而不是self.addChild(bullet1)你需要类似self.addChild(SKSpriteNode(imageNamed: "bullet.png"))

这是因为您每次发射时都需要创建一个新子弹,因此您不希望每次都移动相同的子弹。

相关内容

最新更新