[ios]SKShapeNode 空矩形

标签: Xcode sprite-kit ios
发布时间: 2017/3/28 21:28:50
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

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

欢迎来到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 对象的详细信息的引用,那么我建议看这里︰苹果-SKShapeNode 参考

如果你想要的优质教程来源建议去看这里︰链接在此输入说明

因为我不能时刻,所以让我知道是否它不工作,我会看到我可以做什么来帮助你,我没有测试过的代码。再一次欢迎来到Sprite工具包,我希望这是一个令人愉快的经验。

赞助商