SpriteKit 中的 SKPhysicsJointFixed 和 Swift
SKPhysicsJointFixed in SpriteKit and Swift
我正在使用 Sprite Kit 制作游戏,Swift 我在屏幕底部有一个 Sprite,从顶部掉落的 Sprite 我想抓住并粘在底部的 Sprite 上,所以我正在尝试使用 SKPhysicsJointFixed 但是当物体发生碰撞而不是坠落的物体粘在底部的那个应该抓住并附着的物体上时,似乎底部的 Sprite 适应了坠落的精灵的物理特性然后从屏幕上掉下来。这是我在 didBeginContact 方法中的代码。而 skewer 是底部精灵的名称,它应该一直在底部并且不会消失。
if contact.bodyA.node!.name == "Skewer"
{
let boundX = skewer.physicsBody?.node?.position.x
let fixedJoint = SKPhysicsJointFixed.jointWithBodyA(contact.bodyA.node!.physicsBody, bodyB: contact.bodyB.node!.physicsBody, anchor: CGPoint(x: boundX!, y: boundY))
physicsWorld.addJoint(fixedJoint)
// contact.bodyB.node!.removeFromParent()
}
else
{
contact.bodyA!.node!.removeFromParent()
}
底部屏幕 Sprite 的物理特性在这里
func makeSkewer()
{
skewer.name = "Skewer"
skewer.position = CGPoint(x: size.width * 0.5, y: size.height * 0.244)
skewer.physicsBody = SKPhysicsBody(rectangleOfSize: skewer.size)
skewer.physicsBody?.affectedByGravity = false
skewer.physicsBody?.categoryBitMask = kSkewerCategory
skewer.physicsBody?.contactTestBitMask = kFoodCategory
skewer.physicsBody?.collisionBitMask = kSceneEdgeCategory
addChild(skewer)
}
坠落精灵的物理特性在这里
func random() ->CGFloat
{
return CGFloat(Float(arc4random()) / 0xFFFFFFFF)
}
func random(#min: CGFloat,max: CGFloat) -> CGFloat
{
return random() * (max - min) + min
}
func addFood()
{
let randomCatchIndex = Int(arc4random_uniform(UInt32(foods.count)))
let food = SKSpriteNode(imageNamed: foods[randomCatchIndex])
let actualX = random(min: food.size.width/2, max: size.width - food.size.width/2)
let actualDuration = random(min: CGFloat(1.5), max: CGFloat(8.0))
let actionMove = SKAction.moveTo(CGPoint(x: actualX, y: -food.size.height/2), duration: NSTimeInterval(actualDuration))
let actionMoveDone = SKAction.removeFromParent()
food.physicsBody = SKPhysicsBody(rectangleOfSize: food.size)
food.position = CGPoint(x: actualX, y: size.height + food.size.height/2)
food.physicsBody?.categoryBitMask = kFoodCategory
food.physicsBody?.contactTestBitMask = kSkewerCategory
food.physicsBody?.collisionBitMask = 0x0
food.physicsBody?.dynamic = true
food.runAction(SKAction.sequence([actionMove, actionMoveDone]))
addChild(food)
}
将串串设置为没有动态物理。你目前的情况是它不受重力影响,一旦它锁定食物(向下移动并有动量),串子就会随之移动。
在串的创建中,运行下面一行:
skewer.physicsBody?.dynamic = false
您现在也可以忽略 affectedByGravity
,因为它只影响动态对象。
我正在使用 Sprite Kit 制作游戏,Swift 我在屏幕底部有一个 Sprite,从顶部掉落的 Sprite 我想抓住并粘在底部的 Sprite 上,所以我正在尝试使用 SKPhysicsJointFixed 但是当物体发生碰撞而不是坠落的物体粘在底部的那个应该抓住并附着的物体上时,似乎底部的 Sprite 适应了坠落的精灵的物理特性然后从屏幕上掉下来。这是我在 didBeginContact 方法中的代码。而 skewer 是底部精灵的名称,它应该一直在底部并且不会消失。
if contact.bodyA.node!.name == "Skewer"
{
let boundX = skewer.physicsBody?.node?.position.x
let fixedJoint = SKPhysicsJointFixed.jointWithBodyA(contact.bodyA.node!.physicsBody, bodyB: contact.bodyB.node!.physicsBody, anchor: CGPoint(x: boundX!, y: boundY))
physicsWorld.addJoint(fixedJoint)
// contact.bodyB.node!.removeFromParent()
}
else
{
contact.bodyA!.node!.removeFromParent()
}
底部屏幕 Sprite 的物理特性在这里
func makeSkewer()
{
skewer.name = "Skewer"
skewer.position = CGPoint(x: size.width * 0.5, y: size.height * 0.244)
skewer.physicsBody = SKPhysicsBody(rectangleOfSize: skewer.size)
skewer.physicsBody?.affectedByGravity = false
skewer.physicsBody?.categoryBitMask = kSkewerCategory
skewer.physicsBody?.contactTestBitMask = kFoodCategory
skewer.physicsBody?.collisionBitMask = kSceneEdgeCategory
addChild(skewer)
}
坠落精灵的物理特性在这里
func random() ->CGFloat
{
return CGFloat(Float(arc4random()) / 0xFFFFFFFF)
}
func random(#min: CGFloat,max: CGFloat) -> CGFloat
{
return random() * (max - min) + min
}
func addFood()
{
let randomCatchIndex = Int(arc4random_uniform(UInt32(foods.count)))
let food = SKSpriteNode(imageNamed: foods[randomCatchIndex])
let actualX = random(min: food.size.width/2, max: size.width - food.size.width/2)
let actualDuration = random(min: CGFloat(1.5), max: CGFloat(8.0))
let actionMove = SKAction.moveTo(CGPoint(x: actualX, y: -food.size.height/2), duration: NSTimeInterval(actualDuration))
let actionMoveDone = SKAction.removeFromParent()
food.physicsBody = SKPhysicsBody(rectangleOfSize: food.size)
food.position = CGPoint(x: actualX, y: size.height + food.size.height/2)
food.physicsBody?.categoryBitMask = kFoodCategory
food.physicsBody?.contactTestBitMask = kSkewerCategory
food.physicsBody?.collisionBitMask = 0x0
food.physicsBody?.dynamic = true
food.runAction(SKAction.sequence([actionMove, actionMoveDone]))
addChild(food)
}
将串串设置为没有动态物理。你目前的情况是它不受重力影响,一旦它锁定食物(向下移动并有动量),串子就会随之移动。
在串的创建中,运行下面一行:
skewer.physicsBody?.dynamic = false
您现在也可以忽略 affectedByGravity
,因为它只影响动态对象。