确定何时在矩形SpriteNode上挂起SpriteNode

时间:2015-04-23 00:36:38

标签: xcode swift sprite-kit

我正在制作一个有云移动的游戏,我想要它,这样当角色落在它上面时,云会消失。然而,当我放置代码时,如果角色绕过它并且当云仍在下降时击中云的底部或侧面,它就会消失。这是我用于检测角色和云何时命中的代码。

无论如何都要确定角色何时降落在云端,这样如果它在落下时从底部或侧面撞击它,它就不会褪去云层?

以下是声明对象的代码:

    Person.physicsBody?.usesPreciseCollisionDetection = true
    Person.size = CGSizeMake(self.frame.size.width / 25, self.frame.size.height / 16.25)
    Person.physicsBody = SKPhysicsBody(rectangleOfSize: Person.size)
    Person.physicsBody?.restitution = 0
    Person.physicsBody?.friction = 0
    Person.physicsBody?.allowsRotation = false
    Person.physicsBody?.affectedByGravity = true
    Person.physicsBody?.dynamic = true
    Person.physicsBody?.linearDamping = 0
    Person.zPosition = 5
    Person.physicsBody?.categoryBitMask = BodyType.PersonCategory.rawValue
    Person.physicsBody?.contactTestBitMask = BodyType.CloudCategory.rawValue
    Person.position = CGPoint(x: CGRectGetMidX(self.frame), y: CGRectGetMidY(self.frame) * 1.7)
    self.addChild(Person)


    Cloud = SKSpriteNode(texture: NormalCloudTexture)
    Cloud.zPosition = 7
    Cloud.physicsBody?.usesPreciseCollisionDetection = true
    Cloud.physicsBody?.affectedByGravity = false
    Cloud.physicsBody?.allowsRotation = false
    Cloud.size = CGSizeMake(self.frame.size.width / 8.05, self.frame.size.height / 40)
    Cloud.physicsBody = SKPhysicsBody(rectangleOfSize: Cloud.size)
    Cloud.physicsBody?.friction = 0
    Cloud.physicsBody?.restitution = 0
    Cloud.physicsBody?.dynamic = false
    Cloud.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame) / 7.60)
    addChild(Cloud)

以下是对象命中时间的代码:

func didBeginContact(contact: SKPhysicsContact)
{


    let contactMask = contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask

    switch(contactMask)
    {
    case BodyType.PersonCategory.rawValue | BodyType.CloudCategory.rawValue:
        JumpContact = true
        let CheckDelay = delay(0.055)
            {

                    //cloud fades away here

        }

2 个答案:

答案 0 :(得分:1)

我还没有这样做,但我有一个想法。 :)

我认为你应该这样做: 使用 contactPoint 属性跟踪联系人位置,然后检查它是否未触及距离顶部几个点的云的x位置。

我希望它可以帮到你。 :)

答案 1 :(得分:0)

感谢您的推荐!我决定愚弄,这就是你们做的人!

//put like a few millisecond delay here, forgot it

 if (Person.physicsBody?.velocity.dy == 0){//this means the person has stopped moving since it landed
                    //cloud fades away
                }