简体   繁体   English

SpriteKit中的这两个语句有什么区别?

[英]What is the difference between these two statements in SpriteKit?

this part is a SKShapeNode 这部分是SKShapeNode

var rect = CGRect(origin: CGPoint(x: 0, y: 0), size: CGSize(width: 30, height: 30)) 

self.path = CGPathCreateWithRect(rect, nil)

AND

let rect = SKShapeNode(rectOfSize: CGSize(width: 30, height: 30))

They seems to be acting differently in the simulator and I'm wondering if these are different in any way. 它们在模拟器中的行为似乎有所不同,我想知道它们是否在任何方面都不同。

The origin, (0, 0) point, of a SKShapeNode is the center of the node and its path is drawn relative to that point. SKShapeNode的原点(0,0)是节点的中心,并且相对于该点绘制其路径。 Therefore, a shape node created with the following CGRect 因此,使用以下CGRect创建的形状节点

CGRect(origin: CGPoint(x: 0, y: 0), size: CGSize(width: 30, height: 30))

will be drawn with its bottom-left corner in the center of the shape node. 将以其左下角在形状节点的中心进行绘制。

The convenience method SKShapeNode(rectOfSize...) centers the rect in the shape node by offsetting the origin argument of the CGRect by half of the rectangle's width and height. 便捷方法SKShapeNode(rectOfSize...)通过将CGRectorigin参数偏移矩形宽度和高度的一半,将rect在形状节点中CGRect In this case, the rect's origin is (-15, -15). 在这种情况下,rect的origin是(-15,-15)。

You can create a CGRect that is centered in an SKShapeNode by offsetting its origin: 您可以通过偏移其原点来创建以CGRect为中心的SKShapeNode

let height = CGFloat(30)
let width = CGFloat(30)
let anchorPoint = CGPoint(x:0.5, y:0.5)

CGRect(origin: CGPoint(x: -width*anchorPoint.x, y: -height*anchorPoint.y), size: CGSize(width: width, height: height))

where anchorPoint defines the relative "point in the sprite that corresponds to the node's position." 其中anchorPoint定义相对的“精灵中与节点位置相对应的点”。

You can also use SKShapeNode(path:path, centered:true) which will center the path using its bounding box. 您也可以使用SKShapeNode(path:path, centered:true) ,它将使用其边界框将SKShapeNode(path:path, centered:true)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM