1

I am creating a game and i keep getting this error for my bullet spawn method linked with a joystick. I want to repetitively spawn the bullet node while the joystick is active. Here is how i am creating a firing method

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
}

the first bullet launches as planned and works great however, every time the second bullet launches the app crashes and i get this error "Attemped to add a SKNode which already has a parent". can someone tell me an alternative method

gkolman
  • 189
  • 10

1 Answers1

1

Your problem is exactly as the error says, you need to first remove the bullet from its parent before adding it again, or Make bullet1 a local property inside the spawnBullet function, so that each time you call the function a new bullet gets created and added as child to the scene instead of trying to re-add the same one.

Abdou023
  • 1,654
  • 2
  • 24
  • 45
  • but what if i want more than 1 bullet to show at once what if i wanted a stream not just one at a time @abdou023 – gkolman Apr 29 '16 at 04:09
  • 1
    What I said above is regarding your current error that you are having. First, you need to resolve it and make sure your code to spawn the bullet is right before moving on to the next thing. – Abdou023 Apr 29 '16 at 04:34
  • and what do you suggest is the correct way to sawn the bullet? @abdou023 – gkolman Apr 29 '16 at 17:21
  • What you are doing is fine, there are several ways of doing the same thing in programming, only you who can determine which is the most suitable way, it would be difficult for someone else to tell you how you should write your own code, but we are here to help if you run into any issues or errors. – Abdou023 Apr 29 '16 at 17:48
  • well i obviously knew why the error occurred i was asking for an alternative solution that would not cause an error/crash @abdou023 – gkolman Apr 29 '16 at 18:20
  • As I said your code is fine considering that you already solved the problem causing the error. – Abdou023 Apr 29 '16 at 19:19
  • but based off your previous answer you said i need to remove the node before adding another. by that method i can not have 2 off the same bullet present at once @abdou023 – gkolman Apr 29 '16 at 19:25
  • Read my answer again, I gave you 2 solutions to fix the error, have you even fixed it yet?! – Abdou023 Apr 29 '16 at 19:34
  • @abdo023 thank you for your help i appreciate it. But if i i declare the bullet 1 a local property within the spawn bullet method how will i call bullet1 in other methods. i have to code bullet movement i other methods as well – gkolman Apr 29 '16 at 19:43
  • In your code shown above you are not calling bullet1 outside the spawnBullet function, that's what I based my answer on, but anyway you get the idea. – Abdou023 Apr 29 '16 at 19:53
  • I updated my question, do you see how i have to call bullet 1 in different methods? Can you think of an alternative way to repetitively spawn the bullet since i have to declare the direction of the bullet in my joystick method? @abdou023 – gkolman Apr 29 '16 at 20:47
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/110681/discussion-between-gkolman-and-abdou023). – gkolman Apr 29 '16 at 22:38