简体   繁体   English

如何在两个SpriteKit对象之间创建分隔

[英]How to create a separation between two SpriteKit objects

I have a circle and triangles inside and outside. 我的内外都有一个圆形和三角形。 I was looking through my character if it is not in a circle outside the circle touching triangles and vice versa. 我一直在寻找我的角色,如果它不在与三角形接触的圆圈之外,反之亦然。

It does not matter if my character out of my circle it still happens. 我的角色是否离开圈子也没关系,它仍然会发生。 In fact if I'm in my circle and triangles outside my circle so my character should not touch them. 事实上,如果我在圆中,而三角形不在圆中,那么我的角色就​​不要碰它们。 How do I solve this? 我该如何解决?

Does anyone have an idea? 有人有主意吗?

My Code : 我的代码:

 func AddCharacter() {

        BooCharacter.size = CGSize(width: 30, height: 30)
        BooCharacter.anchorPoint.y = 0
        BooCharacter.zRotation = CGFloat(-M_PI_2)
        //BooCharacter.position.y += circleRadius
        BooCharacter.position = CGPoint(x:0.0, y:circleRadius)
        BooCharacter.physicsBody = SKPhysicsBody(texture:BooCharacterSKT, size: CGSize(width: 30, height: 30))
        BooCharacter.physicsBody = SKPhysicsBody(circleOfRadius: 15);
        BooCharacter.physicsBody?.categoryBitMask = heroCategory
        BooCharacter.physicsBody?.contactTestBitMask = triangleCategory
        BooCharacter.physicsBody?.collisionBitMask = triangleCategory;
    }


    func AddCircle() {

        Circle = SKShapeNode(circleOfRadius: circleRadius)
        Circle.position = CGPoint(x: self.size.width/2, y: self.size.height/2)
        Circle.strokeColor = UIColor.whiteColor()
        self.addChild(Circle)
        Circle.addChild(BooCharacter)

        self.AddTriangleToCircle(Circle, Location: CGFloat(random(1...90)), Inside: true)
        self.AddTriangleToCircle(Circle, Location: CGFloat(random(90...280)), Inside: false)
        self.AddTriangleToCircle(Circle, Location: CGFloat(random(280...360)), Inside: false)
        //self.AddTriangleToCircle(Circle, Location: 90)
        //self.AddTriangleToCircle(Circle, Location: 180)
        //self.AddTriangleToCircle(Circle, Location: 240)
        //self.AddTriangleToCircle(Circle, Location: 300)
    }

    func AddPointsLable() {

        pointsLabel = PointsLabel(num: 0)
        pointsLabel.position = CGPoint(x: self.size.width/2, y: self.size.height/2 - 35)
        pointsLabel.name = "pointsLabel"
        addChild(pointsLabel)
    }

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {

        BooCharacter.zRotation += CGFloat(M_PI)



         //for touch in touches {
         //let location = touch.locationInNode(self)

            let TapLable = childNodeWithName("tap")
            TapLable?.removeFromParent()

        //}



    }



    override func update(currentTime: NSTimeInterval) {
        angleRelatedToCircle -= rotationSpeed
        //BooCharacter.zRotation -= rotationSpeed

        let newLocation = CGPointMake(circleRadius * cos(angleRelatedToCircle), circleRadius * sin(angleRelatedToCircle));
//      BooCharacter.position.x = circleRadius * cos(angleRelatedToCircle)
//      BooCharacter.position.y = circleRadius * sin(angleRelatedToCircle)

        BooCharacter.runAction(SKAction.rotateByAngle(-rotationSpeed, duration: 0));
        BooCharacter.runAction(SKAction.moveTo(newLocation, duration: 0));

        //SKAction.moveTo(CGP, duration: <#T##NSTimeInterval#>)

        //NSLog("x = %f, y = %f, r = %f",BooCharacter.position.x,BooCharacter.position.y,BooCharacter.zRotation);
    }

    func AddTriangleToCircle(Circle: SKShapeNode, Location: CGFloat, Inside: Bool) {

        let Triangle: SKSpriteNode = SKSpriteNode(imageNamed: "Triangle")

        Triangle.size = CGSize(width: 30, height: 30)
        Triangle.anchorPoint.y = 0


        if Inside == true {
            // Inside Triangle
            Triangle.zRotation = CGFloat(M_PI_2)
        } else {
            // Outside Triangle
            Triangle.zRotation = CGFloat(-M_PI_2)
        }

        Triangle.position = CGPoint(x:0.0, y:circleRadius)

        let rotationSpeed1 = rotationSpeed + Location;
        var angleRelatedToCircle1 = angleRelatedToCircle;

        angleRelatedToCircle1 -= rotationSpeed1
        Triangle.zRotation -= rotationSpeed1

        Triangle.position.x = circleRadius * cos(angleRelatedToCircle1)
        Triangle.position.y = circleRadius * sin(angleRelatedToCircle1)

        //Triangle.name = "Triangle";
        Triangle.physicsBody = SKPhysicsBody(texture:TriangelSKT, size: CGSize(width: 30, height: 30))
        //TODO:  Make this a polygon body
        Triangle.physicsBody?.categoryBitMask = triangleCategory
        Triangle.physicsBody?.contactTestBitMask = heroCategory
        Triangle.physicsBody?.collisionBitMask = heroCategory
        Circle.addChild(Triangle);
    }

    func didBeginContact(contact: SKPhysicsContact) {
        // ---------------------------------------------
        // Hero Hit Triangle
        // ---------------------------------------------
        if (contact.bodyA.categoryBitMask == triangleCategory) {

            // Play Sound Effect

            // Remove Triangle
            //contact.bodyA.node?.removeFromParent();

            // Update Score

            NSLog("Hero hit Triangle");
        }
    }

    func random(range: Range<Int> ) -> Int
    {
        var offset = 0

        if range.startIndex < 0   // allow negative ranges
        {
            offset = abs(range.startIndex)
        }

        let mini = UInt32(range.startIndex + offset)
        let maxi = UInt32(range.endIndex   + offset)

        return Int(mini + arc4random_uniform(maxi - mini)) - offset
    }

}

在此处输入图片说明

What you probably want is to set character's physics body like this: 您可能想要的是这样设置角色的物理身体:

BooCharacter.physicsBody = SKPhysicsBody(circleOfRadius: 15);

because currently, character size is a 30x30 rectangle and its body is a circle with a diameter of 60 (d=2r). 因为目前,字符大小是30x30的矩形,其主体是直径为60(d = 2r)的圆。 You need a body with diameter of 30. 您需要一个直径为30的物体。

Also, you are changing characters anchor point but keep in mind that physics body is not affected with that action . 另外,您正在更改角色的锚点,但请记住, 物理身体不会受到该动作的影响 It stays centered on node's position... Read more in this example . 它停留中心节点的位置...阅读更多在这个例子

And about triangle node. 关于三角形节点。 Currently it has a physics body with a rectangular shape even if it's a triangle. 目前,它的物理主体为矩形,即使它是三角形也是如此。 Not sure if you want that, but it may cause the problems you are experiencing. 不确定是否要这样做,但这可能会导致遇到问题。 You have a few options to solve this: 您可以通过几种方法解决此问题:

  1. Create physics body manually, like pointed in one of your previous questions , or 手动创建物理物体,如您先前的问题之一指出的那样 ,或者

  2. Create a physics body from a texture . 从纹理创建物理物体 Keep in mind that this might be performance intensive, but it will probably be fine for your game because you don't have many objects on the scene. 请记住,这可能会提高性能,但对您的游戏来说可能会很好,因为场景中没有太多对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM