Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Swift - Adding PhysicsBody removes SpriteNode from scene

mattdryermattdryer 94538Posts: 5New Users Noob
edited February 2015 in iOS SDK Development
I am creating my first iOS app. I have birds appearing on both sides of the screen at different y values and moving to the other side of the screen. I decided to add a physics body to the birds so that when they are hit by a ball a collision is detected. Without a physics body, the birds can be seen and move the way they should. The moment I add a physics body I can no longer see them when I run the app. Can someone please help? Thank you.

gravity is set to 0 in case that is your idea.

 func createBird() {
        
        let randomSpawn = Int(arc4random_uniform(2))
        
        if randomSpawn == 1 {
        let newX = Int(-20)
        let newY = Int(arc4random_uniform(500) + 300)
        var p = CGPoint(x: newX, y: newY)
        var destination = CGPoint(x:Int(self.frame.width+21), y:newY)
        
        var moverSpeed = Int(arc4random_uniform(5) + 3)
        
        let birdRight = SKSpriteNode (imageNamed: "bird-right1")
        birdRight.xScale = 0.6
        birdRight.yScale = 0.6
        birdRight.position = p
        birdRight.zPosition = 2
        
        //birdRight.physicsBody = SKPhysicsBody(rectangleOfSize: birdRight.size)
        birdRight.physicsBody?.dynamic = true
        birdRight.physicsBody?.categoryBitMask = birdCategory
        birdRight.physicsBody?.contactTestBitMask = ballCategory
        birdRight.physicsBody?.collisionBitMask = 0
        
        let birdMoveRight = SKAction.animateWithTextures([
        SKTexture(imageNamed: "bird-right1"),
        SKTexture(imageNamed: "bird-right2"),
        SKTexture(imageNamed: "bird-right3"),
        SKTexture(imageNamed: "bird-right4"),
        SKTexture(imageNamed: "bird-right5"),
        SKTexture(imageNamed: "bird-right6"),
        SKTexture(imageNamed: "bird-right7"),
        SKTexture(imageNamed: "bird-right8"),
        SKTexture(imageNamed: "bird-right9"),
        SKTexture(imageNamed: "bird-right10")
        ], timePerFrame: 0.05)
        
        let foreverAction = SKAction.repeatActionForever(birdMoveRight)
        
        birdRight.runAction(foreverAction)
        
        let duration = NSTimeInterval(moverSpeed)
        
        let action = SKAction.moveTo(destination, duration: duration)
        
        birdRight.runAction(action)
        
        addChild(birdRight)
        
        
    }
        
        else {
        
        let newX = Int(self.frame.width+20)
        let newY = Int(arc4random_uniform(500) + 300)
        var p = CGPoint(x: newX, y: newY)
        var destination = CGPoint(x:-21, y:newY)
        
        var moverSpeed = Int(arc4random_uniform(5) + 3)
        
        let birdLeft = SKSpriteNode(imageNamed: "bird-left1")
        birdLeft.xScale = 0.6
        birdLeft.yScale = 0.6
        birdLeft.position = p
        birdLeft.zPosition = 2
        
        let birdMoveLeft = SKAction.animateWithTextures([
        SKTexture(imageNamed: "bird-left1"),
        SKTexture(imageNamed: "bird-left2"),
        SKTexture(imageNamed: "bird-left3"),
        SKTexture(imageNamed: "bird-left4"),
        SKTexture(imageNamed: "bird-left5"),
        SKTexture(imageNamed: "bird-left6"),
        SKTexture(imageNamed: "bird-left7"),
        SKTexture(imageNamed: "bird-left8"),
        SKTexture(imageNamed: "bird-left9"),
        SKTexture(imageNamed: "bird-left10")
        ], timePerFrame: 0.05)
        
        birdLeft.physicsBody = SKPhysicsBody(rectangleOfSize: birdLeft.size)
        birdLeft.physicsBody?.dynamic = true
        birdLeft.physicsBody?.categoryBitMask = birdCategory
        birdLeft.physicsBody?.contactTestBitMask = ballCategory
        birdLeft.physicsBody?.collisionBitMask = 0
        birdLeft.physicsBody?.applyForce(CGVectorMake(-400, 0))
        let foreverAction = SKAction.repeatActionForever(birdMoveLeft)
        
        birdLeft.runAction(foreverAction)
        
        let duration = NSTimeInterval(moverSpeed)
        
        let action = SKAction.moveTo(destination, duration: duration)
        
        birdLeft.runAction(action)
        addChild(birdLeft)
        
        }
    }

Replies

  • UIUsUIUs Posts: 165Moderators Admin
    Please do not post multiple posts asking the exact same question. It just clutters up the board. And will not get you help any faster.

    I have removed your duplicate post.
  • mattdryermattdryer 94538Posts: 5New Users Noob
    My apologies. I wasn't sure what forum to post it in.
Sign In or Register to comment.