Seung Hun Han Seung Hun Han - 7 days ago 5
iOS Question

ios sprite kit bodyWithBodies not working

SKSpriteNode * test = [[SKSpriteNode alloc] initWithColor:[UIColor redColor] size:CGSizeMake(100, 100)];
[self addChild:test];
[test setPosition:CGPointMake(300, 300)];

// bottom
CGSize textureSize = CGSizeMake(100, 100);
SKPhysicsBody *physicsBottom = [SKPhysicsBody bodyWithRectangleOfSize:textureSize];
physicsBottom.categoryBitMask = ColliderBottom;
physicsBottom.contactTestBitMask = ColliderBottom | ColliderObstacle;

// working...
// test.physicsBody = physicsBottom;

// not working... this line is problem.
test.physicsBody = [SKPhysicsBody bodyWithBodies:@[physicsBottom]];


didEndContact not working by this code.

test.physicsBody = [SKPhysicsBody bodyWithBodies:@[physicsBottom]];


what's problem????

typedef enum {
ColliderObstacle = 1 << 0,
ColliderCharacter = 1 << 2,
ColliderLeft = 1 << 3,
ColliderRight = 1 << 4,
ColliderBottom = 1 << 5,}ColliderType;


// not wokring... this line... I don't understand.
Why didEndContact event not working???????

Answer

I tried the way to set the physicsBody in my project and couldn't reproduce your problem. However,by compared the code between yours and mine, I think your problem is that you create ONE physicsBody and set it to EVERY "collider" you create, which is related to the copying issues. Perhaps this can be fixed by adding copy method:

renderComponent.node.physicsBody = [SKPhysicsBody bodyWithBodies:@[[physicsComponentBottom.physicsBody copy]
                                                                   ]];

I read the documentation later and find the restriction of the method,that it only support volume-based physicsBody.This might be your problem.

Sprite Kit supports two kinds of physics bodies, volume-based bodies and edge-based bodies. When you create a physics body, its kind, size, and shape are determined by the constructor method you call. An edge-based body does not have mass or volume and is unaffected by forces or impulses in the system. Edge-based bodies are used to represent volume-less boundaries or hollow spaces in your physics simulation. In contrast, volume-based bodies are used to represent objects with mass and volume.