如何将SKPhysicsBody添加到对象

时间:2016-01-06 17:30:25

标签: ios swift sprite-kit

我有圆圈,角色和三角形的场景。 我的角色旋转到我的圆圈内外,她穿过障碍物,但她需要远离它们。

我认为我的三角形物理问题有问题,我试图解决它,但它没有用。

我看到了三角形的物理特性,我看到我的尺寸三角形(40,40)与我的轮廓三角形物理不匹配。

That's how it looks now

我们可以看到我的角色的物理身体与你的身体和我的三角形不匹配。

守则:

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

    Triangle = SKSpriteNode(imageNamed: "Triangle.png")

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


    let path = CGPathCreateMutable()

    CGPathMoveToPoint(path, nil,298,2)
    CGPathAddLineToPoint(path, nil,149,298)
    CGPathAddLineToPoint(path, nil,0,0)
    CGPathAddLineToPoint(path, nil,298,2)

    CGPathCloseSubpath(path)

    Triangle.physicsBody = SKPhysicsBody(polygonFromPath: path)


    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.physicsBody = SKPhysicsBody(texture:TriangelSKT, size: CGSize(width: 40, height: 40))
    Triangle.physicsBody?.categoryBitMask = triangleCategory
    Triangle.physicsBody?.contactTestBitMask = heroCategory
    Triangle.physicsBody?.collisionBitMask = heroCategory
    Triangle.physicsBody?.allowsRotation = false
    // Set Dynamic to false
    Triangle.physicsBody?.dynamic = false
    Circle.addChild(Triangle);
}

0 个答案:

没有答案