Sprite kit Can';t在网格中精确定位SKShapeNode(矩形)

Sprite kit Can';t在网格中精确定位SKShapeNode(矩形),sprite-kit,skshapenode,Sprite Kit,Skshapenode,由于不熟悉obj-c语言和Sprite工具包,我在网格中定位矩形时遇到了困难 在我创建的矩形中有一个偏移-如果我设法让代码在所选字段中定位一个矩形,另一个矩形将被偏移 我已经尝试了几种不同的方法,我可以使用JavaFX使其工作。我做错了什么 下面的照片清楚地显示了我的问题 我的代码非常简单,可以在这里看到: #import "MyScene.h" @implementation MyScene const int ROWS = 10; -(id)initWithSize:(CGSize)

由于不熟悉obj-c语言和Sprite工具包,我在网格中定位矩形时遇到了困难

在我创建的矩形中有一个偏移-如果我设法让代码在所选字段中定位一个矩形,另一个矩形将被偏移

我已经尝试了几种不同的方法,我可以使用JavaFX使其工作。我做错了什么

下面的照片清楚地显示了我的问题

我的代码非常简单,可以在这里看到:

#import "MyScene.h"

@implementation MyScene

const int ROWS = 10;

-(id)initWithSize:(CGSize)size {    
if (self = [super initWithSize:size]) {
    /* Setup your scene here */

    self.backgroundColor = [SKColor darkGrayColor];

    [self createBoardWithRows:ROWS];

    [self createBoxPositionX:1 positionY:1];
    [self createBoxPositionX:3 positionY:3];
    [self createBoxPositionX:5 positionY:5];

}
return self;
}

-(void) createBoardWithRows: (int) rows{

for (int i = 1; i < rows; i++){

    //Horisontal lines
    int yPos = self.size.height/rows * i;

    SKShapeNode *lineH = [SKShapeNode node];
    CGMutablePathRef pathToDraw = CGPathCreateMutable();

    CGPathMoveToPoint(pathToDraw, NULL, 0, yPos);
    CGPathAddLineToPoint(pathToDraw, NULL, self.size.width, yPos);

    lineH.path = pathToDraw;
    lineH.lineWidth = 1.0;
    [lineH setStrokeColor:[UIColor blackColor]];


    //Vertical Lines
    int xPos = self.size.width/rows * i;

    SKShapeNode *lineV = [SKShapeNode node];

    CGPathMoveToPoint(pathToDraw, NULL, xPos, 0);
    CGPathAddLineToPoint(pathToDraw, NULL, xPos, self.size.height);

    lineV.path = pathToDraw;
    lineV.lineWidth = 1.0;
    [lineV setStrokeColor:[UIColor blackColor]];

    //Add lines
    [self addChild:lineH];
    [self addChild:lineV];
}
}

-(void) createBoxPositionX:(int) fieldIndexX positionY:(int) fieldIndexY{

int width = self.size.width/ROWS;
int height = self.size.height/ROWS;

int x = (width * fieldIndexX);
int y = (height * fieldIndexY);

CGRect box = CGRectMake(x, y, width, height);

SKShapeNode *shapeNode = [[SKShapeNode alloc] init];
shapeNode.path = [UIBezierPath bezierPathWithRect:box].CGPath;
shapeNode.fillColor = SKColor.yellowColor;
//Stroke settings
shapeNode.strokeColor = [SKColor clearColor];
shapeNode.lineWidth = 0;
[self addChild:shapeNode];

//Alternative rectangle
//SKSpriteNode spriteNodeWithColor:CGSize:
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
/* Called when a touch begins */
}

-(void)update:(CFTimeInterval)currentTime {
/* Called before each frame is rendered */
}
#导入“MyScene.h”
@MyScene的实现
const int ROWS=10;
-(id)initWithSize:(CGSize)size{
if(self=[super initWithSize:size]){
/*在这里设置场景*/
self.backgroundColor=[SKColor darkGrayColor];
[self-createBoardWithRows:ROWS];
[self-createBoxPositionX:1 positionY:1];
[self-createBoxPositionX:3 positionY:3];
[self-createBoxPositionX:5 positionY:5];
}
回归自我;
}
-(void)createBoardWithRows:(int)行{
对于(int i=1;i

@结束

似乎您没有考虑每个框中边框的线宽-因此,当您创建黄色框时,它们会被替换为与“边框”数量相等的点数

要解决此问题,请更改以下两行:

int x = (width * fieldIndexX);
int y = (height * fieldIndexY);
致:


若你们在任何方向上都有1个像素的偏移,那个么这可能是定位点问题。 默认情况下,它是0.5,0.5-位于精灵的中心。 因此,如果您的精灵具有偶数长度,例如4,则中间可以落在2或3上,您将从定位中获得1个像素

解决方法很清楚,将节点的定位点设置为0,0,如下所示:

node.anchorPoint = CGPointMake(0,0);
这样做的目的是将精灵固定到左下角而不是中心的父对象上。由于左下角始终从0,0像素开始,因此无论节点的像素数如何,都将具有正确的位置

但您必须调整节点位置以适应此更改


希望这能有所帮助。

看起来您几乎没有考虑每个框中边框的线宽-因此,当您创建黄色框时,它们会被与“边框”数量相等的点数所取代。这也是我最初的想法,但我仍然会遇到此解决方案的问题。看起来只有我在Y轴上。您可以在此处看到结果:
node.anchorPoint = CGPointMake(0,0);