弹簧精灵套件出现问题(它就像一根棍子)

时间:2015-09-17 01:08:13

标签: sprite-kit skphysicsjoint

所以我有一个简单的目标。我只是想让两个skspritenodes走向彼此,好像是用松紧带连接起来的。我认为SKSpringJoint是完美的,因为文档说它有类似的属性,但是这对我来说不是很好!

我能够在两个精灵之间建立联系,但是没有"像#34;事情正在发生,相反,它们在整个时间内保持相同的距离,好像它是将它们连接在一起的一根棍子,而不是可以扩展和收缩的东西。

你能看一下我的代码并告诉我我做错了什么吗?模拟很简单,有2个球和一些墙。当轻敲屏幕时,两个球之间的连接就会建立起来。

import SpriteKit

class GameScene: SKScene {
    var it1 = SKSpriteNode()
    var it2 = SKSpriteNode()
    override func didMoveToView(view: SKView) {
        /* Setup your scene here */
        it1 = SKSpriteNode(texture: SKTexture(imageNamed: "Spaceship"))
        it1.position.x = self.frame.size.width / 2
        it1.position.y = self.frame.size.height / 2
        it1.size.width = self.frame.size.height / 10
        it1.size.height = self.frame.size.height / 10

        it1.physicsBody = SKPhysicsBody(circleOfRadius: it1.size.width / 2)


        it2 = SKSpriteNode(texture: SKTexture(imageNamed: "Spaceship"))
        it2.position.x = self.frame.size.width / 2 + 2
        it2.position.y = self.frame.size.height
        it2.size.width = self.frame.size.height / 10
        it2.size.height = self.frame.size.height / 10

        it2.physicsBody = SKPhysicsBody(circleOfRadius: it2.size.width / 2)

        self.addChild(it1)
        self.addChild(it2)

        var ground = SKNode() // just an object
        ground.position = CGPointMake(0, 0) //borrom left
        ground.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(self.frame.size.width * 2, 1))
        ground.physicsBody!.dynamic = false
        self.addChild(ground)

        var ground2 = SKNode() // just an object
        ground2.position = CGPointMake(0, 0) //borrom left
        ground2.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(1, self.frame.size.height * 2))
        ground2.physicsBody!.dynamic = false
        self.addChild(ground2)

        var ground3 = SKNode() // just an object
        ground3.position = CGPointMake(self.frame.size.width, 0) //borrom left
        ground3.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(1, self.frame.size.height * 2))
        ground3.physicsBody!.dynamic = false
        self.addChild(ground3)

    }
    var it = SKPhysicsJointSpring()
    override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
        /* Called when a touch begins */

        var poso = CGPointMake(0, 0)
        var posm = CGPointMake(0.5, 0.5)
        var p1s = it1.position
        var p2s = it2.position
        var p1 = p1s
        var p2 = p2s
        it = SKPhysicsJointSpring.jointWithBodyA(it1.physicsBody, bodyB: it2.physicsBody, anchorA: p1, anchorB: p2)
        //it.frequency = 2349.0
        //it.damping = 0.0
        self.physicsWorld.addJoint(it)
        //Make the tether
    }

}

如果您有任何想法,请告诉我们!

9月17日: 我只是尝试将物理冲动应用于模拟中的一个球。仍然没有运气。这两个球似乎只是用棍子连在一起,没有弹簧动作:(

想要发布我的最终代码,以便那些想要在某一天获得类似效果的人,或者只是一个如何使用SpringJoints的好例子。它基本上是一个2d蜘蛛侠模拟。代码非常可重用,您需要做的就是将绘制函数放在更新循环中,然后将项目中的函数调用,然后简单地调用函数传递一个sprinted和point。享受!

import SpriteKit

class GameScene: SKScene {
    var it1 = SKSpriteNode()
    var it2 = SKSpriteNode()
    override func didMoveToView(view: SKView) {
        /* Setup your scene here */
        it1 = SKSpriteNode(texture: SKTexture(imageNamed: "Spaceship"))
        it1.position.x = 0

        it1.size.width = self.frame.size.height / 10
        it1.size.height = self.frame.size.height / 10
        it1.position.y = 0 + (it1.size.width / 2)

        it1.physicsBody = SKPhysicsBody(circleOfRadius: it1.size.width / 2)



        it2 = SKSpriteNode(texture: SKTexture(imageNamed: "Spaceship"))
        it2.position.x = self.frame.size.width / 2 + 2
        it2.position.y = self.frame.size.height
        it2.size.width = self.frame.size.height / 10
        it2.size.height = self.frame.size.height / 10
        it2.position.y = 0 + (it1.size.width / 2)

        it2.physicsBody = SKPhysicsBody(circleOfRadius: it2.size.width / 2)
        it2.physicsBody?.dynamic = false

        self.addChild(it1)
        //self.addChild(it2)

        let ground = SKNode() // just an object
        ground.position = CGPointMake(0, 0) //borrom left
        ground.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(self.frame.size.width * 2, 1))
        ground.physicsBody!.dynamic = false
        self.addChild(ground)

        let ground2 = SKNode() // just an object
        ground2.position = CGPointMake(0, 0) //borrom left
        ground2.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(1, self.frame.size.height * 2))
        ground2.physicsBody!.dynamic = false
        self.addChild(ground2)

        let ground3 = SKNode() // just an object
        ground3.position = CGPointMake(self.frame.size.width, 0) //borrom left
        ground3.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(1, self.frame.size.height * 2))
        ground3.physicsBody!.dynamic = false
        self.addChild(ground3)

        self.physicsWorld.gravity = CGVectorMake(-1, self.physicsWorld.gravity.dy)

        //drawmeclose(it1, point: it2.position)


    }
    var it = SKPhysicsJointSpring()
    override  func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {


       // it1.physicsBody?.applyImpulse(CGVectorMake(0, -200))

        for touch in (touches )
        {
            let location:CGPoint = touch.locationInNode(self)
            if (havespring == false)
            {
                //drawmeclose(it1, point: it2.position)
                drawmeclose(it1, point: location)
            }
            else
            {
                undrawme()
            }
        }





    }
    var sd  = false
    override func update(currentTime: NSTimeInterval) {


        drawtether()
    }

    var sp = CGPointMake(0, 0)
    var spring = SKPhysicsJointSpring()
    var string = SKShapeNode()
    var havespring = false
    var haveline = false
    var bA = SKSpriteNode()
    func drawmeclose(bodyA: SKSpriteNode, point: CGPoint)
    {
        sp = bodyA.position

        let tetherp = SKSpriteNode()
        tetherp.position = point
        tetherp.physicsBody = SKPhysicsBody()
        tetherp.physicsBody?.dynamic = false
        self.addChild(tetherp)

        bodyA.position = tetherp.position

        spring = SKPhysicsJointSpring.jointWithBodyA(bodyA.physicsBody!, bodyB: tetherp.physicsBody!, anchorA: bodyA.position, anchorB: tetherp.position)
        spring.frequency = 0.8
        spring.damping = 0.5
        self.physicsWorld.addJoint(spring)

        bodyA.position = sp
        havespring = true

        bA = bodyA

        sp = point


    }
    func drawtether()
    {
        if (haveline == true)
        {
            string.removeFromParent()
            haveline = false
        }

        if (havespring == true)
        {
            let path = CGPathCreateMutable()
            CGPathMoveToPoint(path, nil, bA.position.x, bA.position.y)
            CGPathAddLineToPoint(path, nil, sp.x, sp.y)
            CGPathCloseSubpath(path);
            string = SKShapeNode(path: path )
            self.addChild(string)
            haveline = true
        }
    }
    func undrawme()
    {
        self.physicsWorld.removeJoint(spring)
        havespring = false
    }

}

1 个答案:

答案 0 :(得分:1)

试试这个:

it.frequency = 1.0
it.damping = 0.0
self.physicsWorld.addJoint(it)

我认为您的频率太高而无法看到任何内容,默认值为0.0。