SKShapeNode空矩形

时间:2014-09-29 11:07:16

标签: ios sprite-kit skspritenode skshapenode

我开始学习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];

1 个答案:

答案 0 :(得分: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

如果您想获得优质教程的资源,我建议您查看:enter link description here

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