繁体   English   中英

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

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

我是SpriteKit的新手。 我必须创建一个与此图像相似的视图;

在此处输入图片说明

我正在使用以下代码(由这个问题提供 )绘制树叶;

-(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负担?

在此处输入图片说明

谢谢

这是对另一个问题的解答,但也适用于此...

这不是SK中的现成功能。 您唯一的解决方案是创建一个hack,以执行您想要的操作。 一种可能的方法是使用循环创建一个非常小的节点并将其放置在屏幕上的各个位置。 例如(x,y位置),0,0-> 4,0-> 8,0等等...

您可以使用-(BOOL)intersectsNode:(SKNode *)node查看此节点是否与任何其他节点相交。

另一种方法可能是使用相同种类的逻辑,但改用-(BOOL)containsPoint:(CGPoint)p

两种方法都将占用大量处理器,因此您只能谨慎地运行它们。

暂无
暂无

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

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