4

I'm currently working on an iOS app written in Swift using the SpriteKit game technology. In this game raindrops fall from the top of the screen down to the bottom. When they hit the bottom, which is represented by a physicsBody they should return to their original position at the top of the screen allowing the animation to continue for the lifetime of the game.

Since I only need a few drops I create them like this:

let drop = SKSpriteNode(imageNamed: "drop")
drop.position = CGPointMake(x,y)
drop.physicsBody = SKPhysicsBody(rectangleOfSize: drop.size)
addChild(drop)

Then I create the bitmasks:

drop.physicsBody!.categoryBitmask = DropCategory
bottom.physicsBody!.categoryBitmask = BottomCategory
drop.physicsBody!.contactTestBitmask = BottomCategory    

I add the didBeginContact function and I do get notified when the drop hits the bottom. Then my console displays "hit ground".

func didBeginContact(contact: SKPhysicsContact) {

    var firstBody: SKPhysicsBody
    var secondBody: SKPhysicsBody

    if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask {
        firstBody = contact.bodyA
        secondBody = contact.bodyB
    } else {
        firstBody = contact.bodyB
        secondBody = contact.bodyA
    }

    if firstBody.categoryBitMask == BlockCategory && secondBody.categoryBitMask == GroundCategory {
        println("hit ground")

        firstBody.node?.removeFromParent()
        let drop = SKSpriteNode(imageNamed: "drop")
        drop.name = "testNode"
        drop.position = CGPointMake(self.frame.width - self.frame.width/3*2, self.frame.height) // At start Position
        drop.physicsBody = SKPhysicsBody(rectangleOfSize: drop.size)
        drop.physicsBody?.categoryBitMask = BlockCategory
        drop.physicsBody?.contactTestBitMask = GroundCategory
        addChild(drop)
    }
}

However, the position of the drop doesn't change. There is no syntax error, so what's my problem?

If I replace this:

firstBody.node!.position = CGPointMake(x, y)

with this:

firstBody.applyImpulse(CGVector)

it gives the drop the impulse. How can I make the first option work as well?

8
  • Have you tried setting the mass for the drops physics body? Feb 8, 2015 at 17:52
  • 2
    Why not use a particle emitter ? There is even a rain template you can choose when creating a new particle file in XCode. Feb 8, 2015 at 18:07
  • Also, even though I think using the physics system is overkill for what you are trying to do, the drops shouldn't need an impulse. The drops would just get affected by gravity. Feb 8, 2015 at 18:14
  • I want to be notified when the drops hit my character so i need the physics system.
    – ecoguy
    Feb 8, 2015 at 18:41
  • Yes why not just use a particle emitter?
    – hamobi
    Feb 8, 2015 at 18:41

1 Answer 1

2

You can try removing the node when it touches the bottom, and adding new node at the top again. Load the texture for reuse in a seperate SKTexture variable. Like this

let dropTexture = SKTexture(imageNamed: "drop.png")

use it like this

let drop = SKSpriteNode(texture: dropTexture)

Then inside didBeginContact

func didBeginContact(contact: SKPhysicsContact) {
    println("collided")
    var bodyA : SKPhysicsBody!
    var bodyB : SKPhysicsBody!

    if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask
    {
        bodyA = contact.bodyA!
        bodyB = contact.bodyB!
    }
    else
    {
        bodyB = contact.bodyA!
        bodyA = contact.bodyB!
    }

    if bodyA.categoryBitMask == bottomCategoryBitMask && bodyB.categoryBitMask == raindropCategoryBitMask
    {
        if bodyB.node?.parent != nil // Added line
        {
            bodyB.node?.removeFromParent()
            let drop = SKSpriteNode(imageNamed: "1.png")
            drop.name = "testNode"
            drop.position = CGPointMake(self.frame.width - self.frame.width/3*2, self.frame.height) // At start Position
            drop.physicsBody = SKPhysicsBody(rectangleOfSize: drop.size)
            drop.physicsBody?.categoryBitMask = raindropCategoryBitMask
            drop.physicsBody?.contactTestBitMask = bottomCategoryBitMask
            addChild(drop)
        }
    }
}
5
  • Every time the drop hits the bottom the number of the drops get multiplied by two so I end up having hundrads of them. If I remove the drop and then tell it to add one new drop, how can it get two.
    – ecoguy
    Feb 10, 2015 at 16:20
  • yes, I think it's clearer when you use an image for the drop. Then it multiplies.
    – ecoguy
    Feb 10, 2015 at 16:37
  • it works even when i tried it with an image. can you show your updated code?
    – rakeshbs
    Feb 10, 2015 at 16:54
  • firstBody.node?.removeFromParent() let block1 = SKSpriteNode(imageNamed: "block") block1.name = "testNode" block1.position = CGPointMake(self.frame.width - self.frame.width/3*2, self.frame.height) // At start Position block1.physicsBody = SKPhysicsBody(rectangleOfSize: block1.size) block1.physicsBody?.categoryBitMask = BlockCategory block1.physicsBody?.contactTestBitMask = GroundCategory addChild(block1)
    – ecoguy
    Feb 10, 2015 at 16:58
  • i updated my code. add the if condition also if firstBody.node?.parent != nil
    – rakeshbs
    Feb 10, 2015 at 17:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.