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

时间:2016-04-27 23:19:33

标签: swift sprite-kit game-physics skspritenode

我正在创建一个游戏,并且我一直在为使用操纵杆链接的子弹生成方法获取此错误。我想在操纵杆处于活动状态时重复生成节点这是我如何创建一个触发方法

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
}

第一个子弹按计划启动并且运行良好,但每次第二个子弹启动应用程序崩溃时我都会收到错误。谁能告诉我另一种创造火灾率的方法

1 个答案:

答案 0 :(得分:0)

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

之类的内容

这是因为你需要在每次射击时创建一个新的子弹,你不想每次都移动同一个子弹。