gpt4 book ai didi

swift - 如何防止 swift Sprite 套件中的产卵重叠?

转载 作者:搜寻专家 更新时间:2023-10-31 22:49:51 25 4
gpt4 key购买 nike

如果我试图在屏幕的 2 个定义区域中生成敌人(顶部和底部中间有一个它们无法生成的区域),我该如何防止它们生成在每个区域的顶部或太靠近每个区域其他 。

我的 sprite 在屏幕上相对较小,我在这里找到的唯一建议是创建一个可能的位置数组,每次使用其中一个位置时将其从列表中删除,但首先我不知道那会是什么样子,其次,我有很多可能的位置,因为我正在使用 5px 高 Sprite ,我希望他们能够在该区域清除后重生。

我选择顶部或底部的方法只是选择一个随机数 1 或 2,这取决于我有 2 个函数使它们位于顶部或底部。

我只需要没有 2 个对象在一个球的直径范围内生成。有什么想法可以将其整合到我的产卵中吗?

编辑:

  //Create your array and populate it with potential starting points
var posArray = Array<CGPoint>()
posArray.append((CGPoint(x: 1.0, y: 1.0))
posArray.append((CGPoint(x: 1.0, y: 2.0))
posArray.append((CGPoint(x: 1.0, y: 3.0))

//Generate an enemy by rolling the dice and
//remove its start position from our queue
let randPos = Int(arc4random()) % posArray.count
posArray[randPos]
posArray.removeAtIndex(randPos)

...

//Play game and wait for enemy to die
//Then repopulate the array with that enemy's start position
posArray.append(enemyDude.startPosition)

这是我找到的建议,但这会给出错误“预期的分隔符”,我真的不知道如何修复。

因此,实际上,我必须在 X 和 Y 上创建一个涵盖所有区域的大量可能位置,或者是否有更好的方法来做到这一点?

最佳答案

通过使用 intersectsNode(_ node: SKNode) -> Bool,不在另一个节点之上生成一个节点就足够了。

至于不要产卵太近,那就是另一回事了。你能做到这一点的唯一方法是将所有当前节点都放在一个数组中,枚举数组并检查每个节点的位置与生成节点的位置。根据您的参数,您可以生成或不生成。


我不精通 Swift,所以你必须自己翻译代码。

-(void)testMethod {

// an array with all your current nodes
NSMutableArray *myArray = [NSMutableArray new];

// the potential new spawn node with the proposed spawn position
SKSpriteNode *mySpawnNode = [SKSpriteNode new];

BOOL tooClose = NO;

// enumerate your node array
for(SKSpriteNode *object in myArray) {

// get the absoulte x and y position distance differences of the spawn node
// and the current node in the array
// using absolute so you can check both positive and negative values later
// in the IF statement
float xPos = fabs(object.position.x - mySpawnNode.position.x);
float yPos = fabs(object.position.y - mySpawnNode.position.y);

// check if the spawn position is less than 10 for the x or y in relation
// to the current node in the array
if((xPos < 10) || (yPos < 10))
tooClose = YES;
}

if(tooClose == NO) {
// spawn node
}
}

请注意,数组应该是一个属性,而不是在示例中的范围内声明。

关于swift - 如何防止 swift Sprite 套件中的产卵重叠?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31090897/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com