0
votes

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)
2
post the code where you set up the physics bodies for boundary and balloons. Keep in mind that if you want correct collision behavior you must not move the balloons using SKAction moveTo/By - LearnCocos2D
rakeshbs - the balloon class is in a separate swift file so your edit's don't seem to work - macgeezer

2 Answers

0
votes

Create an enum to differentiate the two types of sprites:

    enum ColliderType {
    case sprite1 = 1
    case sprite2 = 2
}

You need to add a contact and collision bit mask like so:

borderShape.physicsBody?.contactTestBitMask = ColliderType.sprite2.rawValue

Also add a collisionBitMask:

borderShape.physicsBody?.collisionBitMask = ColliderType.sprite2.rawValue

Do the same for sprite2 and set it's collisionBitMask and it's contactTestBitMask to be the rawValue of sprite1. Make sure you set this class to be the contactDelegate so you receive a warning though a function that will notify you when two objects have collided:

self.physicsWorld?.contactDelegate = self


func didBeginContact(contact: SKPhysicsContact) {

[handle the contact however you would like to here]

}

I hope this helps!

0
votes

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