Chapter 9 catBodyTexture

Hey guys!

I’m following the tutorial to get the catNode’s physicsBody to match the SKtexture(imageNamed: “cat_body_outline”. But instead of showing the image texture, it’s just a regular square.

let catBodyTexture = SKTexture(imageNamed: “cat_body_outline”)
parent!.physicsBody = SKPhysicsBody(texture: catBodyTexture,
size: catBodyTexture.size())

I’ve taken a look at the final project, provided with the books resources, but there it also shows as a square. Anyone know how to fix this?

1 Like

I think it is a bug in some simulators

I have the same issue when building with iOS 13.5, targetting a simulator iPhone XS with iOS 13.5
However, it is working as expected when I build the same code targeting my real device iPhone XS also with iOS 13.5
And it is also working as expected in simulator when I build iOS 12.0 targeting a simulator iPad Air 2 with iOS 12.1

I end up using SKPhysicsBody(plogygonFrom:)

import SpriteKit

class CatNode: SKSpriteNode, EventListenerNode {

func didMoveToScene()
{
 // 2020-05-28 12:03:13.442486+0200 Cat nap[2376:114822] PhysicsBody: Could not create physics body.
 // let texture = SKTexture(imageNamed: "cat_body_outline")
 // parent!.physicsBody = SKPhysicsBody(texture: texture, size:texture.size())

 let path = CGMutablePath();
 path.move(to: CGPoint(x:-77.0, y:-198.0))
 path.addLine(to: CGPoint(x:-87.0, y:-129.0))
 path.addLine(to: CGPoint(x:-14.0, y:-18.0))
 path.addLine(to: CGPoint(x:-82.0, y:67.0))
 path.addLine(to: CGPoint(x:-13.0, y:179.0))
 path.addLine(to: CGPoint(x:85.0, y:177.0))
 path.addLine(to: CGPoint(x:169.0, y:81.0))
 path.addLine(to: CGPoint(x:99.0, y:3.0))
 path.addLine(to: CGPoint(x:137.0, y:-127.0))
 path.addLine(to: CGPoint(x:117.0, y:-198.0))

 parent!.physicsBody = SKPhysicsBody(polygonFrom: path)
}

}

@mokhet Thank you for sharing your solution - much appreciated! :]

This topic was automatically closed after 166 days. New replies are no longer allowed.