[英]Spritekit: Separating randomly placed non-dynamic “block” nodes by a set X and Y value?
my script below does exactly what i need it to do, add blocks to a scene in any random order in the view. 我的下面脚本完全满足我的需要,在视图中以任意随机顺序将块添加到场景中。 the only problem is, as i increase the amount of "block" nodes, they tend to overlap on one another and clump up, I'm wondering if there is a way i can add a "barrier" around each block node so that they cannot overlap but still give a random feel?
唯一的问题是,当我增加“块”节点的数量时,它们往往会相互重叠并聚集在一起,我想知道是否有办法在每个块节点周围添加“障碍”,以便它们不能重叠但仍然给人一种随意的感觉? My current code is below:
我当前的代码如下:
-(void) addBlocks:(int) count {
for (int i = 0; i< count; i++) {
SKSpriteNode *blocks = [SKSpriteNode spriteNodeWithImageNamed:@"Ball"];
blocks.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:(blocks.size.width/2)];
blocks.physicsBody.dynamic = NO;
blocks.position = CGPointMake(self.frame.size.width/2, self.frame.size.height/2);
[self addChild:blocks];
int blockRandomPositionX = arc4random() % 290;
int blockRandomPositionY = arc4random() % 532;
blockRandomPositionY = blockRandomPositionY + 15;
blockRandomPositionX = blockRandomPositionX + 15;
blocks.position = CGPointMake(blockRandomPositionX, blockRandomPositionY);
}
}
Any help highly appreciated, thanks! 任何帮助,高度赞赏,谢谢!
To prevent two nodes from overlapping, you should check the newly created node's random position with intersectsNode:
to see if it overlaps any other nodes. 为了防止两个节点重叠,您应该使用
intersectsNode:
检查新创建的节点的随机位置,以查看它是否与任何其他节点重叠。 You also have to add each successfully added node into an array against which you run the intersectsNode:
check. 您还必须将每个成功添加的节点添加到要运行
intersectsNode:
check的数组中。
Look at the SKNode Class Reference for detailed information. 有关详细信息,请参阅《 SKNode类参考 》。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.