有时精灵不会从物理边界反弹

时间:2015-02-02 12:29:30

标签: rotation sprite-kit skphysicsbody

我正在开始一款新游戏并立刻遇到一个奇怪的问题。在我的didMoveToView函数中,我有以下内容在框架内(整个屏幕)放置我的精灵边界

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

以下代码在触摸点添加SKSpriteNode,并添加一个带有永久重复的rotatebyangle动作

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)
        }
    }
}

我按照以下方式设置了balloonWorld:

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

我的问题是,有时气球精灵不会从边缘反弹,而只是继续通过边缘永远不会再被看到。

有什么建议吗?

谢谢, 肯

根据请求,这里是设置物理机构的代码

气球:

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)
    }
 }

我没有在边界上设置物理主体,因为我需要的只是行self.physicsBody = SKPhysicsBody(edgeLoopFrom Rect : self:frame),因为边界具有相同的帧。

我尝试添加以下内容,但这并没有改变行为:

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 个答案:

答案 0 :(得分:0)

创建一个枚举来区分两种类型的精灵:

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

您需要添加一个接触和碰撞位掩码,如下所示:

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

还要添加collisionBitMask:

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

对sprite2执行相同的操作并将其设置为collisionBitMask,并将它的contactTestBitMask设置为sprite1的rawValue。确保将此类设置为contactDelegate,以便通过一个在两个对象发生碰撞时通知您的函数收到警告:

self.physicsWorld?.contactDelegate = self


func didBeginContact(contact: SKPhysicsContact) {

[handle the contact however you would like to here]

}

我希望这有帮助!

答案 1 :(得分:0)

LearnCocos2D(Steffen Itterheim)在他的评论中指出了正确的方向。我使用的是moveTo / moveBy,物理引擎无法正确处理。一旦我根据冲动做出简单的动作,一切都运转正常。

谢谢Steffen