SpriteKit PhysicsBody: Could not create physics body

此生再无相见时 提交于 2019-12-06 15:36:37

thanks 0x141E, i found out that my texture had some white shit around it, after I've deleted it, everything started to work. Now i am wondering how to hide stroke around my texture which represents my physical object.

You shouldn't change the texture and physics body of player at the update rate (up to 60 times a second). You should change them only when needed. Here's an example of how to do that:

if (self.player.physicsBody.velocity.dy > 30) {
    // Change texture/body only if not already flying
    if (!isFlying) {
        self.player.texture = [SKTexture textureWithImageNamed:@"player_flying"];
        self.player.physicsBody = [SKPhysicsBody bodyWithTexture:self.player.texture
                                                        size:self.player.size];
        isFlying = YES;
    }      
}
else
{
    // Change texture/body only if not already falling
    if (isFlying) {
        self.player.texture = [SKTexture textureWithImageNamed:@"player_falling"];
        self.player.physicsBody = [SKPhysicsBody bodyWithTexture:self.player.texture
                                                        size:self.player.size];
        isFlying = NO;
   }
}

Check the image for stray pixels away from the main image. This can cause the 'could not create physics body' error.

Clean Image

Image is not clean - note extra pixel island to the bottom right

Typically this extra pixel island can be a very light / low alpha block that looks similar to the background or is just really difficult to spot.

I ran a loop through all of my images testing them as follows:

                    let t = SKTexture(imageNamed: filename)
                    let physics = SKPhysicsBody(texture: t, size: t.size())
                    if physics.area == 0.0 {
                        print("File \(filename) - failed")
                    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!