简体   繁体   中英

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). You need a body with diameter of 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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