SKScene:找到SKSpriteNode的最大位置

时间:2023-01-22 23:03:42

I'm writing an App for iPad and iOS 10. My App uses only one SKScene (size is 1300 by 1000). I also use one camera defined like this:

我正在为iPad和iOS 10编写应用程序。我的应用程序只使用一个SKScene(大小为1300 x 1000)。我也使用一个像这样定义的相机:

myCamera=[[SKCameraNode alloc] init];
myCamera.position=CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));
self.camera=myCamera;
[self addChild:myCamera];

I use many SKSpriteNodes that the user can drag around (even beyond the size of the SKScene).

我使用了许多用户可以拖动的SKSpriteNodes(甚至超出了SKScene的大小)。

I also use:

我也用:

[myCamera setScale:newScale];

to deal with pinch to zoom.

处理捏缩放。

I need to know if there is a way to get the max X position of the node that is most distant from position 0,0 in the SKScene on the right hand side without having to read all node positions one by one. And then do the same for the other three sides (up, left, down).

我需要知道是否有办法获得距离右侧SKScene中距离位置0,0最远的节点的最大X位置,而不必逐个读取所有节点位置。然后对其他三个方面(向上,向左,向下)做同样的事情。

1 个解决方案

#1


1  

Keep four floats minX, minY, maxX, maxY. While the user is dragging a node, check if its position exceeds either max or is less than either min. If so, update the corresponding value, e.g.

保持四个浮点数minX,minY,maxX,maxY。当用户拖动节点时,检查其位置是否超过max或小于min。如果是,请更新相应的值,例如

EDIT use calculateAccumulatedFrame to get the full extent of the node's descendants.

编辑使用calculateAccumulatedFrame来获取节点后代的完整范围。

// assuming you have code like this already
skNodeThatIAmDragging.position = currentDragPosition;
CGRect frame = [skNodeThatIAmDragging calculateAccumulatedFrame];

if (self.maxX < frame.origin.x) {
    self.maxX = frame.origin.x;
}
if (self.minX > CGRectGetMaxX(frame)) {
    self.minX = CGRectGetMaxX(frame);
}
// ... and so on

#1


1  

Keep four floats minX, minY, maxX, maxY. While the user is dragging a node, check if its position exceeds either max or is less than either min. If so, update the corresponding value, e.g.

保持四个浮点数minX,minY,maxX,maxY。当用户拖动节点时,检查其位置是否超过max或小于min。如果是,请更新相应的值,例如

EDIT use calculateAccumulatedFrame to get the full extent of the node's descendants.

编辑使用calculateAccumulatedFrame来获取节点后代的完整范围。

// assuming you have code like this already
skNodeThatIAmDragging.position = currentDragPosition;
CGRect frame = [skNodeThatIAmDragging calculateAccumulatedFrame];

if (self.maxX < frame.origin.x) {
    self.maxX = frame.origin.x;
}
if (self.minX > CGRectGetMaxX(frame)) {
    self.minX = CGRectGetMaxX(frame);
}
// ... and so on