简体   繁体   English

SpriteKit-如何添加节点,使其覆盖整个屏幕?

[英]SpriteKit - How to add the nodes so that they cover the whole screen?

I'm completely new in SpriteKit. 我是SpriteKit的新手。 I have to create a view similar to this image; 我必须创建一个与此图像相似的视图;

在此处输入图片说明

I'm using following code (courtesy of: this question ) to draw the leaves; 我正在使用以下代码(由这个问题提供 )绘制树叶;

-(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];
}

Currently I repeatedly call this method (around 300 times in loop) to fill the whole screen with leaves which is a bad approach. 目前,我反复调用此方法(循环约300次)以叶子覆盖整个屏幕,这是一种不好的方法。

1) How do I know that the whole view is filled with leaves so that I stop the loop? 1)我怎么知道整个视图都充满了叶子,所以我停止了循环?

2) This approach is heavy on CPU and memory, how can I make it efficient and less CPU hungry? 2)这种方法占用大量CPU和内存,如何提高效率和减少CPU负担?

在此处输入图片说明

Thanks 谢谢

This is an answer to another question but it also applies here... 这是对另一个问题的解答,但也适用于此...

This is not an off the shelf kind of function in SK. 这不是SK中的现成功能。 Your only solution would be to create a hack to do what you want. 您唯一的解决方案是创建一个hack,以执行您想要的操作。 One possible approach might be to create a very small node and place it in various positions on the screen, using a loop. 一种可能的方法是使用循环创建一个非常小的节点并将其放置在屏幕上的各个位置。 For example (x,y position), 0,0 -> 4,0 -> 8,0 and so on... 例如(x,y位置),0,0-> 4,0-> 8,0等等...

You can use the -(BOOL)intersectsNode:(SKNode *)node to see if this node intersects any other node. 您可以使用-(BOOL)intersectsNode:(SKNode *)node查看此节点是否与任何其他节点相交。

Another approach might be to use the same kind of logic but use -(BOOL)containsPoint:(CGPoint)p instead. 另一种方法可能是使用相同种类的逻辑,但改用-(BOOL)containsPoint:(CGPoint)p

Both approaches would be processor intensive so you should only run them sparingly. 两种方法都将占用大量处理器,因此您只能谨慎地运行它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM