1

我开始学习 SpriteKit 并立即遇到了问题。我正在尝试使用 SKShapeNode 绘制一个空矩形,但确实出现在屏幕上。如果我为填充属性设置颜色,则会出现矩形。我究竟做错了什么 ?

    CGRect box = CGRectMake(0, 0, self.frame.size.width/2, self.frame.size.height/2);

    SKShapeNode *shapeNode = [[SKShapeNode alloc] init];
    shapeNode.path = [UIBezierPath bezierPathWithRect:box].CGPath;
    shapeNode.fillColor = nil;
    shapeNode.strokeColor = SKColor.redColor;
    shapeNode.lineWidth = 3;

    [self addChild:shapeNode];
4

1 回答 1

3

欢迎使用 sprite 工具包,我也在学习它,并且对 shapeNodes 没有太多经验,但这是我的建议:

//If you want the shape to be that of a rectangle I would suggest using a simpler allocation method such as the following:
SKShapeNode *shapeNode = [SKShapeNode shapeNodeWithRectOfSize:CGSizeMake(self.frame.size.width/2, self.frame.size.height/2))];

/*shapeNodeWithRectOfSize is a built in allocation method for SKShapeNodes that handles
allocation and initialization for you, and will also create the rectangle shape for you.
The CGSizeMake method will return a CGSizeMake object for you*/

/*a CGSizeMake object is an object with two properties: width, and height. It is used to hold
the dimensions of objects. self.frame.size is a CGSize object*/

/*You do not need to set the fill color to nil. This is because the default is [SKColor clearColor]
which is an empty color already*/

//Make sure that you use an initializer method when setting the colour as below
shapeNode.strokeColor = [SKColor redColor];
shapeNode.lineWidth = 3;

[self addChild:shapeNode];

如果您想参考 SKShapeNode 对象的详细信息,那么我建议您看这里:Apple - SKShapeNode Reference

如果您想要优质教程的来源,我建议您看这里:在此处输入链接描述

我还没有测试代码,因为我目前无法测试,所以如果它不起作用,请告诉我,我会看看我能做些什么来帮助你。再次欢迎使用 Sprite-Kit,希望这是一次愉快的体验。

于 2014-09-30T06:58:41.507 回答