2014-09-29 70 views
1

我开始学习SpriteKit并立即遇到问题。我试图用SKShapeNode绘制一个空矩形,但确实出现在屏幕上。如果我给填充属性设置一个颜色矩形出现。我究竟做错了什么 ?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]; 

回答

3

欢迎精灵套件,我学习它也并没有与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,我希望这是一个愉快的经历。