Search code examples
rotationsprite-kitskphysicsbody

sometimes sprites will not bounce off the physics boundary


I'm beginning a new game and have a weird problem right off the bat. In my didMoveToView function I have the following to put a bounds on my sprites within the frame(whole screen)

self.physicsBody=SKPhysicsBody(edgeLoopFromRect: self.frame)

The following code adds an SKSpriteNode at the touch point and adds a rotatebyangle action with a repeat forever

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
    /* Called when a touch begins */

    for touch: AnyObject in touches {
        let location = touch.locationInNode(balloonWorld)
        let nodeAtPoint = self.nodeAtPoint(location)
        if(nodeAtPoint.name == nil) {
            let location = touch.locationInNode(self)

            let randNum:Int = Int.random(min: 0, max: 7)

            var stringColor:String = (balloonColors.objectAtIndex(randNum) as String)

            stringColor = stringColor.stringByReplacingOccurrencesOfString("face",
                withString :"")

            let sprite = Balloon(theColor:stringColor)


            //let spriteFileName:String = balloonColors.objectAtIndex(randNum) as String
            //let sprite = SKSpriteNode(imageNamed:spriteFileName)


            sprite.xScale = 0.5
            sprite.yScale = 0.5
            sprite.position = location
            sprite.zPosition = SceneLevel.hero.rawValue
            balloonWorld!.addChild(sprite)
            let action = SKAction.rotateByAngle(CGFloat(-M_PI), duration:1)

            sprite.runAction(SKAction.repeatActionForever(action))


        } else {
            nodeAtPoint.removeFromParent()
            println(nodeAtPoint.name)
        }
    }
}

I've setup balloonWorld as follows:

 balloonWorld = SKNode()
 self.addChild(balloonWorld!)

My problem is that sometimes the balloon sprites will not bounce off the edge, but just keep going thru the edge never to be seen again.

Any suggestions?

Thanks, Ken

per request here's the code for setting up the physics bodies

balloon:

class Balloon: SKNode {

    // properties


     var myColor:String?
     var objectSprite:SKSpriteNode?

    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    init(theColor:String){
        super.init()
        self.myColor=theColor

        let imageFileName = "\(theColor)face"
        let objectSprite:SKSpriteNode = SKSpriteNode(imageNamed:imageFileName)
        objectSprite.physicsBody = SKPhysicsBody(circleOfRadius: objectSprite.size.height / 2.0)
        objectSprite.physicsBody?.affectedByGravity = true
        objectSprite.name = theColor + " balloon"
        addChild(objectSprite)
    }
 }

I didn't set up a physics body on the boundary as I though all I needed was the line self.physicsBody = SKPhysicsBody(edgeLoopFrom Rect : self:frame) since the boundary has the same frame.

I tried adding the following, but that did not change the behavior:

let borderShape=SKShapeNode(rect: CGRectMake(self.frame.origin.x+2, self.frame.origin.y+2, self.frame.size.width-4, self.frame.size.height-4))
borderShape.fillColor=SKColor.clearColor()
borderShape.strokeColor=SKColor.blackColor()
borderShape.lineWidth=1
borderShape.physicsBody?.categoryBitMask=BodyType.boundary.rawValue
borderShape.zPosition=SceneLevel.border.rawValue
borderShape.physicsBody=SKPhysicsBody(edgeLoopFromRect: borderShape.frame)

balloonWorld!.addChild(borderShape)

Solution

  • LearnCocos2D(Steffen Itterheim) pointed me in the right direction in his comment. I was using moveTo/moveBy and the physics engine would not properly handle that. Once I simply made the moves based on impulses everything worked fine.

    Thanks Steffen