OLZ1
OLZ1

Reputation: 185

iOS Sprite Kit- SKSpriteNode seems to participate in simulation without physics body

I am working on an iOS game using Sprite Kit.

I recently added an SKSpriteNode into my scene and did NOT create a physics body for it. However, when I build and run, when the player's character(which does have a physics body) moves into the SKSpriteNode, it spins and moves away, like it has a physics body -but I didn't create one for the SKSpriteNode.

Even if I type

sprite.physicsBody = nil;

it still behaves like it's part of the simulation.

I wondered if a physics body is created automatically when you make an SKSpriteNode, but I looked at the documentation and searched it on Google and couldn't find anything to suggest that is the case.

Any help or suggestions would be much appreciated.

This is the code I used to create the sprite (the one that should not be affected by the simulation)

-(void)addSprite
{
SKSpriteNode *sprite = [SKSpriteNode spriteNodeWithImageNamed:@"image"];
sprite.position = CGPointMake(40, 30);
sprite.zPosition = 30;
sprite.name = @"spriteName";

[self addChild:sprite];
 }

In another part of the project I have

[self addSprite];

to call the method.

Upvotes: 0

Views: 871

Answers (1)

Ivan Lesko
Ivan Lesko

Reputation: 1304

Yes every SKSpriteNode comes with a physics body. Its best to change the physics body's properties instead of getting rid of it altogether. If you want your sprite to be on screen and not interact with any other sprite nodes, do the following:

sprite.physicsBody.dynamic = NO;
sprite.physicsBody.collisionBitMask = 0x0;
sprite.physicsBody.contactTestBitMask = 0x0;

Upvotes: 2

Related Questions