SpriteKit PhysicsBody: Could not create physics bo

2020-03-31 03:05发布

I have a player in my game, it has two states flying, falling. Each of them has an image: player_flying, player_falling correspondingly. I am also using a physical bodies to detect collision. It is completely normally functioning when I use one texture. But when I am trying to use both in different conditions using different textures, it shows me an error in the log. I am trying it like that:

if (self.player.physicsBody.velocity.dy > 30) {
    self.player.texture = [SKTexture textureWithImageNamed:@"player_flying"];
    self.player.physicsBody = [SKPhysicsBody bodyWithTexture:self.player.texture
                                                        size:self.player.size];        
}
else
{
    self.player.texture = [SKTexture textureWithImageNamed:@"player_falling"];
    self.player.physicsBody = [SKPhysicsBody bodyWithTexture:self.player.texture
                                                        size:self.player.size];
}

the error is:

2014-08-30 12:55:47.515 kalabaska[1569:50535] PhysicsBody: Could not create physics body.

3条回答
够拽才男人
2楼-- · 2020-03-31 03:36

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;
   }
}
查看更多
聊天终结者
3楼-- · 2020-03-31 03:50

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.

查看更多
劳资没心,怎么记你
4楼-- · 2020-03-31 03:55

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

Clean Image Clean Image Image is not clean - note extra pixel island to the bottom right Image is not clean - 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")
                    }
查看更多
登录 后发表回答