Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

SKPhysicsBody assigning wrong size to SKSpriteNode

So I'm making this game and I came across something weird.

I've made all my nodes, and have no reason to think any of the other ones are having this problem.

In my GamePlayScene I am initializing my floor node with this code:

FloorNode *floor = [FloorNode floorWithSize:CGSizeMake(self.view.frame.size.width, 150.0)];
// Frame is 375 as per iPhone 6 resolution.

I add it to the scene.

This is the initializer:

+(instancetype) floorWithSize:(CGSize) size {
    FloorNode *floor = [self spriteNodeWithColor:[SKColor grayColor] size:size];
    //FloorNode *floor = [self spriteNodeWithImageNamed:@"floor"];
    floor.size = size;
    floor.position = CGPointMake(0.0, 0.0);
    floor.anchorPoint = CGPointMake(0.0, 0.0);
    floor.zPosition = 199;
    floor.name = @"Floor";

    [floor setupPhysicsBody];

    return floor;
}

This shows me a block of the size of the frame and a height of 150 on the screen, on my phone, everywhere. Exactly what I need.

I had some objects falling down to test my collision/contact code, etc.

It all worked fine, until I noticed that objects that were falling on the right half side of the block were not getting caught by the collision code.

I tried running this down, trying to figure out what's going on.

I turned down opacity, and noticed the collision code was also not running until it reached half the shape.

I dug into the setupPhysicsBody of my floor.

self.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(self.frame.size.width, self.frame.size.height )];
self.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:self.frame.size)];

Tried both of these options.

I'm logging both width and height values and they are listing 375 and 150 (the sizes of the "floor node"). It's showing up fine on screen with those values, filling up the screen accordingly.

I'm getting the right size values, but exactly HALF the collision body. When I x2 the size in the physicsbody, it becomes the size I want it to be for collision and does not change the visual look at 375,150.

This doesn't make sense, how do I get this to work how it should be? I don't want to use some cheaty workaround.

I didn't have this problem on my GroundNode in SpaceCat.

Please help,

Thanks.

Jerome

So I have the solution here, don't understand why though...

Apparently if I assign the anchor point to 0.0 (for easy placement), the physicsbody messes up.

    floor.position = CGPointMake(0.0, 0.0);
    floor.anchorPoint = CGPointMake(0.0, 0.0);

So I changed the anchor point back to default and positioned it accordingly.

    floor.position = CGPointMake(375 / 2, 75);
    floor.anchorPoint = CGPointMake(0.5, 0.5);

Now i works fine...

Isn't this strange?

Jerome