gpt4 book ai didi

ios - 未检测到 SpriteKit 碰撞

转载 作者:行者123 更新时间:2023-11-30 14:17:07 25 4
gpt4 key购买 nike

好吧,我已经和这个代码斗争了几天了。我不明白为什么没有检测到碰撞。

我正在尝试沿着逆时针旋转的圆绘制一根绳子,并且我想要一个对撞机圆顺时针绕绳子旋转。

这是我的代码:

class GameScene : SKScene, SKPhysicsContactDelegate {
var circleString:String = "some string to draw in a circle"

private struct ColliderType {
static let Symbol: UInt32 = 0x1 << 0
static let OrbitingBall: UInt32 = 0x1 << 1
}

override func didMoveToView(view: SKView) {
self.physicsWorld.contactDelegate = self
createSpinningText()
}

func createSpinningText() {
//circleNode serves as a container node to simplify placing and rotating the symbolNodes
let circleNode = SKShapeNode(circleOfRadius: self.size.width * 0.40)
circleNode.position = CGPointMake(self.size.width * 0.5, self.size.height * 0.5)
circleNode.strokeColor = UIColor.purpleColor()

self.addChild(circleNode)

//count elements and calculate rotation per character
let l = countElements(symbolString)
let charRotation = -(2.0 * M_PI) / Double(l)

for x in 0..<l {
let character = circleString.substringWithRange(Range<String.Index>(start: advance(circleString.startIndex, x), end: advance(circleString.startIndex, x + 1)))

//charSprite is a container for charNode, to rotate the starting
//position of each character. (because I can't set a frame or anchorPoint
//on a SKLabelNode)
let charSprite = SKSpriteNode(color: UIColor.clearColor(), size: circleNode.frame.size)

let charNode = SKLabelNode(fontNamed: "Arial")
charNode.text = character
charNode.fontSize = 25
charNode.position = CGPoint(x: 0, y: circleNode.frame.size.width / 2.27)
charNode.fontColor = UIColor.purpleColor()

charNode.physicsBody = SKPhysicsBody(rectangleOfSize: charNode.frame.size)
charNode.physicsBody?.dynamic = false
charNode.physicsBody?.categoryBitMask = ColliderType.Symbol
charNode.physicsBody?.contactTestBitMask = ColliderType.OrbitingBall | ColliderType.Symbol
charNode.physicsBody?.usesPreciseCollisionDetection = true
charNode.physicsBody?.collisionBitMask = ColliderType.OrbitingBall | ColliderType.Symbol

let rotate = SKAction.rotateByAngle(CGFloat(Double(x) * charRotation), duration: 0)
charSprite.runAction(rotate)

charSprite.addChild(charNode)
circleNode.addChild(charSprite)
}

let turnSlowlyCCW = SKAction.rotateByAngle(1, duration: 20)
let repeat = SKAction.repeatActionForever(turnSlowlyCCW)

circleNode.runAction(repeat)

let orbitingColliderContainer = SKShapeNode(circleOfRadius: self.size.width * 0.35)
orbitingColliderContainer.strokeColor = UIColor.redColor()

orbitingColliderContainer.position = CGPointZero
let xy = CGPoint(x: orbitingColliderContainer.frame.size.width * 0.5, y: orbitingColliderContainer.frame.size.height * 0.25)

orbitingColliderContainer.physicsBody = SKPhysicsBody(circleOfRadius: 70, center:xy)
orbitingColliderContainer.physicsBody?.dynamic = false
orbitingColliderContainer.physicsBody?.contactTestBitMask = ColliderType.Symbol | ColliderType.OrbitingBall
orbitingColliderContainer.physicsBody?.collisionBitMask = ColliderType.Symbol | ColliderType.OrbitingBall
orbitingColliderContainer.physicsBody?.categoryBitMask = ColliderType.OrbitingBall
orbitingColliderContainer.physicsBody?.usesPreciseCollisionDetection = true
circleNode.addChild(orbitingColliderContainer)

let turnSlowlyCW = SKAction.rotateByAngle(-2, duration: 20)
let repeat2 = SKAction.repeatActionForever(turnSlowlyCW)
orbitingColliderContainer.runAction(repeat2)
}

func didBeginContact(contact: SKPhysicsContact) {
println("Contact between \(contact.bodyA) and \(contact.bodyB) detected")
}
}

这是应用程序运行时的样子(打开 .showPhysics):

enter image description here

一切都按照预期放置和移动,但我没有收到任何 didBeginContact 调用。那么,我在这里做错了什么?

最佳答案

通过设置让它工作

self.physicsWorld.gravity = CGVectorMake(0, 0)

还有...

orbitingColliderContainer.physicsBody?.dynamic = true

然后将 charNode 和 OrbitingColliderContainer 的 CollisionBitMask 设置为 0。

关于ios - 未检测到 SpriteKit 碰撞,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31004873/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com