SpriteKit碰撞检测中SKSpriteNodes之间的间隙 [英] Gap between SKSpriteNodes in SpriteKit collision detection

查看:89
本文介绍了SpriteKit碰撞检测中SKSpriteNodes之间的间隙的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试了好一阵子了-我有一款游戏具有简单的平台物理,玩家会掉到一块上,阻止他掉下来.这是可行的,但是播放器停止的位置与实际对象/spritenode的位置之间存在明显的差距.这是一个屏幕截图,应该是不言自明的:

I've been trying to figure this out for quite a while now -- I have a game with simple platformer physics where a player falls onto a block, which stops him from falling. This works, however there is a noticeable gap between where the player stops, and where the actual object/spritenode is. Here is a screenshot, it should be self-explanatory:

class GameScene: SKScene {

override init(){
    super.init(size: UIScreen.mainScreen().bounds.size)
    self.physicsWorld.gravity = CGVector(dx: 0, dy: -9.8)
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}
func genBlock(iteration: CGFloat){
    let block = SKSpriteNode(color: UIColor.blackColor(), size: CGSizeMake(100,50))
    block.position = CGPointMake((iteration*50)-block.size.width/2,0)
    block.physicsBody = SKPhysicsBody(edgeLoopFromRect: CGRectMake(0,0,block.size.width,block.size.height))
    self.addChild(block)
}
func genPlayer(){
    let char = SKSpriteNode(color: UIColor.redColor(), size: CGSizeMake(100,100))
    char.position = CGPointMake(0,500)
    char.physicsBody = SKPhysicsBody(rectangleOfSize: char.size)
    self.addChild(char)
}
override func didMoveToView(view: SKView) {
    /* Setup your scene here */
    view.backgroundColor = UIColor.whiteColor()
    self.view?.backgroundColor = UIColor.whiteColor()
    genBlock(0)
    genBlock(1)
    genBlock(2)
    genBlock(3)
    genBlock(4)
    genPlayer()
}
}

我觉得这不是SpriteKit的问题,而是我的代码的问题.任何有关如何消除节点之间的间隙的建议将不胜感激.

I feel as though this isn't an issue with SpriteKit and is instead an issue with my code. Any advice on how to remove this gap between the nodes would be greatly appreciated.

我已经将其提交给了Apple的错误报告者,但我不希望收到任何答复.如果有人在此发布后很久才来这里,最好在这里提交您的建议.

I've submitted this to Apple's bug reporter, but I don't expect to receive anything in reply. If anyone is coming here long after this was posted, it would be great if you could submit your suggestions here.

推荐答案

我认为这与从CGPath构建的physicsBodies有关.例如,使用下面的代码创建6个正方形:

I believe it has to do with physicsBodies built from a CGPath. For instance, using the code below to create 6 squares:

override func didMoveToView(view: SKView) {
    self.physicsBody = SKPhysicsBody(edgeLoopFromRect: self.frame)
    self.physicsWorld.gravity = CGVector(dx: 0, dy: -9.8)
    self.backgroundColor = UIColor.whiteColor()

    let square1 = SKSpriteNode(color: UIColor.blackColor(), size: CGSize(width: 50, height: 50))
    square1.physicsBody = SKPhysicsBody(edgeLoopFromRect: CGRect(x: -25, y: -25, width: square1.size.width, height: square1.size.height))
    square1.position = CGPoint(x: self.size.width/2 - 75, y: 25)
    self.addChild(square1)

    let square2 = SKSpriteNode(color: UIColor.greenColor(), size: CGSize(width: 50, height: 50))
    square2.physicsBody = SKPhysicsBody(rectangleOfSize: square2.size)
    square2.position = CGPoint(x: self.size.width/2 - 75, y: 200)
    self.addChild(square2)

    let square3 = SKSpriteNode(color: UIColor.blueColor(), size: CGSize(width: 50, height: 50))
    square3.physicsBody = SKPhysicsBody(rectangleOfSize: square3.size)
    square3.position = CGPoint(x: self.size.width/2 + 75, y: 200)
    self.addChild(square3)

    let squarePath = getSquarePath()
    let square4 = SKSpriteNode(color: UIColor.redColor(), size: CGSize(width: 50, height: 50))
    square4.physicsBody = SKPhysicsBody(polygonFromPath: squarePath)
    square4.position = CGPoint(x: self.size.width/2 + 75, y: 400)
    self.addChild(square4)

    let square5 = SKSpriteNode(color: UIColor.orangeColor(), size: CGSize(width: 50, height: 50))
    square5.physicsBody = SKPhysicsBody(rectangleOfSize: square5.size)
    square5.position = CGPoint(x: self.size.width/2, y: 200)
    self.addChild(square5)

    let square6 = SKSpriteNode(color: UIColor.purpleColor(), size: CGSize(width: 50, height: 50))
    square6.physicsBody = SKPhysicsBody(rectangleOfSize: square6.size)
    square6.position = CGPoint(x: self.size.width/2, y: 400)
    self.addChild(square6)
}

func getSquarePath() -> CGPath {
    let path = CGPathCreateMutable()
    CGPathMoveToPoint(path, nil, -25, -25)
    CGPathAddLineToPoint(path, nil, -25, 25)
    CGPathAddLineToPoint(path, nil, 25, 25)
    CGPathAddLineToPoint(path, nil, 25, -25)
    CGPathCloseSubpath(path)

    return path
}

您可以看到从CGPath创建的带有physicsBodies的正方形(包括edgeLoopF​​romRect)存在间隙问题.甚至场景边缘循环的physicsBody都存在此问题.当我使用texture构造函数构建PhysicsBody时,这对我来说也很明显,该构造函数似乎是在引擎盖下构建CGPath的.

you can see that the squares with physicsBodies created from CGPath (this includes edgeLoopFromRect) have that gap issue. Even the physicsBody for the scene's edge loop has this issue. This also shows up for me when I build the physicsBody using the texture constructors which appears to build a CGPath under the hood.

除了不使用这些类型的physicsBodies进行长期(可见)碰撞外,我没有找到其他解决方法.

I haven't found a fix for it other than to not use those types of physicsBodies for long term (visible) collisions.

这篇关于SpriteKit碰撞检测中SKSpriteNodes之间的间隙的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆