所以我有一个简单的目标。我只是想有两个skspritenode走向彼此,好像由一个橡皮筋连接。我认为,一个SKSpringJoint将是完美的,因为文档说,它有类似的属性,但这不是太好为我工作!
我能够在两个精灵之间进行接触,但是没有"像弹簧一样"的事情发生,相反,它们在整个时间内保持相同的距离,就好像它是一根将它们连接在一起的棍子,而不是可以扩展和收缩的东西。
你能看看我的代码,告诉我我做错了什么吗?模拟很简单,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日:我只是尝试在模拟中对其中一个球应用物理脉冲。还是没有运气。这两个球似乎只是用一根棍子连接在一起,没有弹簧作用:(
只是想发布我的最终代码,为那些想要类似效果的人,或者只是一个如何使用springjoint的好例子。它基本上是一个2d蜘蛛侠模拟。代码是非常可重用的,你所需要做的就是把draw函数放到更新循环中,然后简单地调用函数,传递一个sprint和一个点。享受吧!
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
}
}
试一下:
it.frequency = 1.0
it.damping = 0.0
self.physicsWorld.addJoint(it)
我认为你的频率太高了,看不到任何东西,默认值是0.0。