我在一个侧面游戏中工作,我需要知道一个区域中的节点是否能够实现“视线”之类的东西.现在我正在尝试使用enumerateBodyiesInRect()但是它正在检测来自评估的rect的20px或更多的物体,我无法弄清楚为什么它如此不精确.
这就是我现在正在尝试的事情:
import SpriteKit import CoreMotion class GameScene: SKScene,SKPhysicsContactDelegate { var player = SKShapeNode() var world = SKShapeNode() var rShape = SKShapeNode() override func didMovetoView(view: SKView) { self.physicsWorld.contactDelegate = self self.scaleMode = SKScenescaleMode.AspectFit self.size = view.bounds.size // Add world world = SKShapeNode(rectOfSize: view.bounds.size) world.physicsBody = SKPhysicsBody(edgeLoopFromPath: world.path) world.position = CGPointMake(self.frame.size.width/2,self.frame.size.height/2) // Move camera self.addChild(world) // Add player player = SKShapeNode(rectOfSize: CGSize(width: 25,height: 25)) player.physicsBody = SKPhysicsBody(rectangleOfSize: player.frame.size) player.physicsBody.dynamic = false player.strokeColor = SKColor.blueColor() player.fillColor = SKColor.blueColor() player.position = CGPointMake(90,-50) world.addChild(player) } override func update(currentTime: CFTimeInterval) { // Define rect position and size (area that will be evaluated for bodies) var r : CGRect = CGRect(x: 200,y: 200,width: 25,height: 25) // Show rect for debug rShape.removeFromParent() rShape = SKShapeNode(rect: r) rShape.strokeColor = SKColor.redColor() self.addChild(rShape) // Evaluate rect rShape.fillColor = SKColor.clearColor() self.physicsWorld.enumerateBodiesInRect(r) { (body: SKPhysicsBody!,stop: UnsafePointer<ObjCBool>) in self.rShape.fillColor = SKColor.redColor() // Paint the area blue if it detects a node } } }
此代码应在屏幕上显示已评估的rect和ray(用于调试目的),如果它们与播放器节点联系,则将其绘制为红色.但是你可以在屏幕截图中看到当玩家离它25px或更远时它变成红色,就像绘图有点偏离,或者小于被评估的实际区域.您可以将其粘贴到项目中以复制问题.
这可能是因为这只是测试版,还是我做错了什么?