Physicsbody不遵守节点的锚点 [英] Physicsbody doesn't adhere to node's anchor point

查看:228
本文介绍了Physicsbody不遵守节点的锚点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的场景中有一堆矩形,其物理体与矩形的大小相同。我喜欢将我的所有物体锚定到CGPointZero,但是我注意到当我这样做时,物理体仍然固定在中间。换句话说,我的物理主体的位置就像在视觉表示的左下方100个像素。

My scene has a bunch of rectangles with physics bodies that are the same size as the rectangle. I like to anchor all of my objects to CGPointZero, however I've noticed when I do that the physicsbody remains anchored in the middle. In other words, the position of my physics body is like 100 pixels lower and to the left of the visual representation.

这是一段简单的代码:

SKSpriteNode* square = [SKSpriteNode spriteNodeWithColor:[SKColor blackColor] size:CGSizeMake(width, height)];
square.anchorPoint = CGPointZero; //position based on bottom-left corner
square.position = CGPointMake(x, y);

square.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(width, height)];

任何有关解决此问题的建议或建议都将不胜感激。例如,如果我可以想象物理机构,这可能有所帮助,但我不知道如何。

Any ideas or advice to solving this problem would be appreciated. For example, if I could visualize the physics bodies, that might help, but I'm not sure how to.

更新:所以我已经通过简单解决了问题没有设置锚点并重新定位我的矩形。所以问题仍然存在,但我已经有了解决方案并且工作正常。

UPDATE: So I've solved the problem by simply not setting the anchor point and repositioning my rectangles. So the problem still exists, but I have a work around in place and the work around is working well.

推荐答案

我写了这个解决Apple缺乏的问题:

I wrote this to fix Apple's lack thereof:

使用 pathForRectangleOfSize:withAnchorPoint:将您的呼叫替换为 bodyWithRectangleOfSize:其简要文档告诉我们问题:创建一个以拥有节点的原点为中心的矩形物理体。

use pathForRectangleOfSize:withAnchorPoint: to replace your call to bodyWithRectangleOfSize: whose brief documentation tells us the problem: "Creates a rectangular physics body centered on the owning node’s origin."

@implementation SKPhysicsBody (CWAdditions)

+ (CGPathRef)pathForRectangleOfSize:(CGSize)size withAnchorPoint:(CGPoint)anchor {
  CGPathRef path = CGPathCreateWithRect( CGRectMake(-size.width * anchor.x, -size.height * anchor.y,
                                                    size.width,   size.height), nil);
  return path;
}

+ (SKPhysicsBody *)bodyWithRectangleOfSize:(CGSize)size withAnchorPoint:(CGPoint)anchor {
  CGPathRef path = [self pathForRectangleOfSize:size withAnchorPoint:anchor];
  return [self bodyWithPolygonFromPath:path];
}

@end

编辑:7.1中有一个新的API来提供这种疏忽。

Edit: There is a new API in 7.1 to provide for this oversight.

+ (SKPhysicsBody *)bodyWithRectangleOfSize:(CGSize)s center:(CGPoint)center

这篇关于Physicsbody不遵守节点的锚点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆