SKPhysicsBody texture not working

Hi guys, i've been searching about this problem very long but couldn't find anywhere, currently im working on a game project which i need to implement SKPhysicsBody with texture method. Everyone uses circleOfRadius: or rectangleOf: methods but i need something more spesific physics body than that because i use triangles or so.


let sprite = SKSpriteNode(imageNamed: "Spaceship") sprite.physicsBody = SKPhysicsBody(texture: sprite.texture!, size: sprite.texture!.size())


This one is not working also.


Thanks,

Replies

The SpriteKit section is probably a more appropriate place for this kind of question, if you want to move this thread there.

https://forums.developer.apple.com/community/graphics-and-games/spritekit



There is an example in the SKPhysicsBody documentation for a physics body based on the alpha channel of the texture, which uses the sprite's size rather than the texture's size :


https://developer.apple.com/documentation/spritekit/skphysicsbody

let texturedSpaceShip = SKSpriteNode(texture: spaceShipTexture)
texturedSpaceShip.physicsBody = SKPhysicsBody(texture: spaceShipTexture, size: CGSize(width: circularSpaceShip.size.width, height: circularSpaceShip.size.height))


If that doesn't fix the problem, please explain what isn't working.

Hi there, sorry for my late respond but it gives an error to;

let texturedSpaceShip = SKSpriteNode(texture: spaceShipTexture) 
texturedSpaceShip.physicsBody = SKPhysicsBody(texture: spaceShipTexture, size: CGSize(width: circularSpaceShip.size.width, height: circularSpaceShip.size.height))


Use of unresolved identifier "spaceShipTexture" error.

I am really beginner at Swift and my mind couldn't handle with this, i couldn't find it anywhere, everyone posting same code whenever i ask my question, but there is no video or working code on web.

Those are just the lines in the sample code at the link, which show the same step.


The equivalent in your code would be this:

let sprite = SKSpriteNode(imageNamed: "Spaceship")
sprite.physicsBody = SKPhysicsBody(texture: sprite.texture!, size: sprite.size)


The change from the code you posted is just to make it match the Apple sample code, in case the size from the texture is a different scale and causing the physics body to be the wrong size for your sprite.


Does it still have the same problem as your original code? If it does (or if it acts differently but still doesn't work right), please explain (with details) everything that happens and what the problem is.