Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在swift sprite套件中防止产卵重叠?_Swift_Sprite Kit - Fatal编程技术网

如何在swift sprite套件中防止产卵重叠?

如何在swift sprite套件中防止产卵重叠?,swift,sprite-kit,Swift,Sprite Kit,如果我试图在屏幕的两个定义区域(顶部和底部,中间有一个不能产卵的区域)产卵,我如何防止它们在顶部或彼此太近的地方产卵 我的精灵在屏幕上相对较小,我在这里找到的唯一建议是创建一个可能的位置数组,每次使用其中一个位置时,都会将其从列表中删除,但首先我不知道它会是什么样子,第二,我有很多可能的位置,因为我和5px高的精灵一起工作,我希望他们能够在该区域清理干净后重生 我选择顶部或底部的方法只是选择一个随机数1或2,根据我有两个函数使它们位于顶部或底部 我只需要两个物体在一个球的直径内相互繁殖。有没有办

如果我试图在屏幕的两个定义区域(顶部和底部,中间有一个不能产卵的区域)产卵,我如何防止它们在顶部或彼此太近的地方产卵

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

我选择顶部或底部的方法只是选择一个随机数1或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)
//创建数组并用潜在的起点填充它
var posArray=Array()
append((CGPoint(x:1.0,y:1.0))
append((CGPoint(x:1.0,y:2.0))
append((CGPoint(x:1.0,y:3.0))
//通过掷骰子和骰子生成敌人
//从队列中删除其起始位置
让randPos=Int(arc4random())%posArray.count
posArray[randPos]
posArray.removatendex(randPos)
...
//玩游戏,等待敌人死亡
//然后用敌人的起始位置重新填充阵列
posArray.append(enemyDude.startPosition)
这是我找到的建议,但这会产生错误“预期分隔符”,我真的不知道如何修复


所以实际上,我必须在X和Y上建立一个巨大的可能位置阵列,覆盖所有区域,或者有更好的方法吗?

使用
intersectsNode(u.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
    }
}
-(void)测试方法{
//包含所有当前节点的数组
NSMUTABLEARRY*myArray=[NSMUTABLEARRY new];
//具有建议繁殖位置的潜在新繁殖节点
SKSpriteNode*mySpawnNode=[SKSpriteNode new];
BOOL-tooClose=否;
//枚举节点数组
for(myArray中的SKSpriteNode*对象){
//获取繁殖节点的绝对x和y位置距离差
//和数组中的当前节点
//使用绝对值,以便以后可以同时检查正值和负值
//在IF语句中
float xPos=fabs(object.position.x-mySpawnNode.position.x);
float yPos=fabs(object.position.y-mySpawnNode.position.y);
//检查相对于x或y的繁殖位置是否小于10
//到数组中的当前节点
如果((xPos<10)| |(yPos<10))
tooClose=是;
}
如果(tooClose==否){
//繁殖节点
}
}

请注意,数组应该是一个属性,而不是在示例中的作用域中声明。

作为一个刚刚起步的人,您提供的“繁殖”解决方案是否只是一个需要处理的怪物?我肯定必须查看如何执行几乎每一步。如果我简化它并说“bot部分有5个Y值,顶部部分有5个Y值,我会让一个家伙在X上随机出现“这会简化解决方案吗?在这一点上,如果每个部分一次只有一个,那么距离就没有那么重要了。如果你不介意的话,我将如何开始这样的工作?太棒了,非常感谢你花时间这么做。非常感谢!@swiftcnt-很乐意帮忙。”。