本文介绍了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.

下面是一段简单的代码:

Here is a simple snippet of code:

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 不遵守节点的锚点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 03:02