使用SpriteKit设置Ground节点?

时间:2015-09-11 14:49:15

标签: ios swift sprite-kit skphysicsbody

我一直在设计一款游戏,我希望在球与地面接触后立即停止游戏。我的下面的功能是为了建立基础。

class GameScene: SKScene, SKPhysicsContactDelegate {

     var ground = SKNode() 

     let groundCategory: UInt32 = 0x1 << 0
     let ballCategory: UInt32 = 0x1 << 1

     //Generic Anchor coordinate points
     let anchorX: CGFloat = 0.5
     let anchorY: CGFloat = 0.5
     /*Sets the background and ball to be in the correct dimensions*/

     override init(size: CGSize) {
         super.init(size: size)

         //Create the Phyiscs of the game
         setUpPhysics()  

         setUpGround()   

         setUpBall()
    }

    func setUpPhysics() -> Void {
        self.physicsWorld.gravity = CGVectorMake( 0.0, -5.0 )
        self.physicsWorld.contactDelegate = self
    }

     func setUpGround() -> Void {
        self.ground.position = CGPointMake(0, self.frame.size.height)
        self.ground.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(self.frame.size.width, self.frame.size.height)) //Experiment with this
        self.ground.physicsBody?.dynamic = false

        self.ground.physicsBody?.categoryBitMask = groundCategory    //Assigns the bit mask category for ground
        self.ball.physicsBody?.contactTestBitMask = ballCategory  //Assigns the contacts that we care about for the ground

        /*Added in*/
        self.ground.physicsBody?.dynamic = true

        self.ground.physicsBody?.affectedByGravity = false
        self.ground.physicsBody?.allowsRotation = false
        /**/

        self.addChild(self.ground)
    }

    func setUpBall() -> Void {

        ball.anchorPoint = CGPointMake(anchorX, anchorY)

        self.ball.position = CGPointMake(self.frame.size.width/2, self.frame.size.height/2)

        self.ball.name = "ball"
        self.ball.userInteractionEnabled = false

        ball.physicsBody?.usesPreciseCollisionDetection = true

        self.ball.physicsBody?.categoryBitMask = ballCategory    //Assigns the bit mask category for ball
        self.ball.physicsBody?.collisionBitMask = wallCategory | ceilingCategory //Assigns the collisions that the ball can have
        self.ball.physicsBody?.contactTestBitMask = groundCategory   //Assigns the contacts that we care about for the ball

        addChild(self.ball)  //Add ball to the display list
    }

我注意到的问题是,当我运行iOS模拟器时,未检测到Ground节点。在这种情况下,我做错了什么?

3 个答案:

答案 0 :(得分:2)

你的ballCategory应该分配给self.ground.contactBitMask而不是球。

答案 1 :(得分:2)

您的地面节点根本没有显示出来吗?我相信你的理由应该是SKSpriteNode。将SKSpriteNode添加为您创建的SKNode对象的子项。 SKNode是一个空节点,SKSpriteNode具有实际的可视化表示。而不是在单独的函数中执行它,如果你在DidMoveToView本身中执行它会更简单。

var objects = SKNode()
addChild(objects)
let ground = SKNode()
ground.position = .......
let colorsprite1 = SKSpriteNode(imageNamed: "yourimageoftheground")
ground.addChild(colorsprite1)
ground.physicsBody?.dynamic = false

是的,你将地面的物理定义设置为非动态是正确的。我不知道为什么别人会告诉你。你不希望它在与你的球物接触时移动,或者你在场景中移动的任何东西。

添加您需要的所有其他物理定义。考虑使用ContactTestBitMask,因为您希望SpriteKit在发生与地面的碰撞时通知您,而不是SpriteKit自己处理它。有关该掩码的详细信息可以通过示例在StackOverFlow中轻松获得。

objects.addChild(ground)

希望这有帮助

答案 2 :(得分:0)

您需要将dynamic属性设置为true才能使联系人检测正常工作。

同时,您可以设置以下属性。

ground.affectedByGravity = false
ground.allowsRotation = false