我在SpriteKit中是全新的。我必须创建一个类似于这个图像的视图;
我使用以下代码(由this question提供)来绘制树叶;
-(void)createObjects {
// Create random start point
float randomYPoint = arc4random_uniform(self.view.bounds.size.height);
float randomXPoint = arc4random_uniform(self.view.bounds.size.width);
CGPoint startPoint = CGPointMake(randomXPoint, randomYPoint);
// Create random object and add to scene
SKTexture* objectTexture;
switch (arc4random_uniform(8)) {
case 0:
objectTexture = [SKTexture textureWithImageNamed:@"leafOne"];
break;
case 1:
objectTexture = [SKTexture textureWithImageNamed:@"leafTwo"];
break;
case 2:
objectTexture = [SKTexture textureWithImageNamed:@"leafThree"];
break;
case 3:
objectTexture = [SKTexture textureWithImageNamed:@"leafFour"];
break;
case 4:
objectTexture = [SKTexture textureWithImageNamed:@"leafFive"];
break;
case 5:
objectTexture = [SKTexture textureWithImageNamed:@"leafSix"];
break;
case 6:
objectTexture = [SKTexture textureWithImageNamed:@"leafSeven"];
break;
case 7:
objectTexture = [SKTexture textureWithImageNamed:@"leafEight"];
break;
default:
break;
}
SKSpriteNode *object = [SKSpriteNode spriteNodeWithTexture:objectTexture];
object.position = startPoint;
object.name = @"object";
[self addChild: object];
}
目前我反复调用此方法(循环约300次)用叶子填充整个屏幕,这是一种不好的方法。
1)我如何知道整个视图充满了叶子,以便我停止循环?
2)这种方法对CPU和内存很重,如何使其高效,减少CPU占用?
由于
答案 0 :(得分:0)
这是另一个问题的答案,但它也适用于此......
这不是SK的现成功能。你唯一的解决方案是创建一个hack来做你想做的事。一种可能的方法可能是使用循环创建一个非常小的节点并将其放置在屏幕上的不同位置。例如(x,y位置),0,0 - > 4,0 - > 8,0等等......
您可以使用-(BOOL)intersectsNode:(SKNode *)node
查看此节点是否与任何其他节点相交。
另一种方法可能是使用相同类型的逻辑,但改为使用-(BOOL)containsPoint:(CGPoint)p
。
这两种方法都是处理器密集型的,所以你应该只是谨慎地运行它们。