取阵列选择出一个周期 [英] Taking an array option out for one cycle

查看:99
本文介绍了取阵列选择出一个周期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好了,所以我有这个code其中产卵位置放入数组,然后其中一个位置是随机选出与此code:

 让randx = spawnLocations [INT(arc4random_uniform(UInt32的(spawnLocations.count)))]
            obstacle.position = CGPoint(X:randx,Y:0)

对象产卵code:

  VAR spawnLocations:CGFloat的] = []FUNC getObjectSpawnLocation(){//创建5个可能地点产卵
让numberOfNodes = 5    //节点之间的间距会改变,如果:1)的节点的数目改变时,2)屏幕宽度被改变,3)节点的大小被改变。
    因为我在0 ... numberOfNodes - 1 {        //间距用于空间了根据框架(与屏幕的宽度改变)节点
        VAR xPosition位置=(frame.maxX / * - * thePlayer.size.width /)/ CGFloat的((numberOfNodes - 1))* CGFloat的(I)        //默认情况下,添加一个半球员的宽度,因为节点的锚点(0.5,0.5)
        xPosition位置+ = thePlayer.size.width / 2        //我不知道这是什么一样,但它的工作原理。
        xPosition位置 - = frame.maxX / 1.6
        spawnLocations.append(xPosition位置)    }
  ()
}

但我有一个问题,因为有时比赛派生的对象,如下面的图片,它不会让我的球员提前任何进一步的没有他们死去,所以我的问题是:

反正我可以从这样阻止它?

也许采取产卵的地点之一出数组暂时的?

我也应该注意到,每一个对象(头骨)都产生了一个接一个的不是一次后头骨可以在任何的5个水平位置产卵。

在这里输入的形象描述


解决方案

玩家只能被困在头骨和屏幕的边缘之间。你应该跟踪你是否正在播放或不是楔入,例如:

  //保持实例变量来跟踪你的当前状态
BOOL lineFromEdge; //是,如果你目前正在绘制一个头骨的线从边缘
BOOL leftEdge; //如果是如果从右边线从左侧边缘起源,NO
INT previousIndex;

然后确定值如下:

   - (INT){randomIndex    INT randIndex = INT(arc4random_uniform(UInt32的(spawnLocations.count)));    //这个前pression告诉你,如果你目前的指数处于一个边缘
    如果(randIndex == 0 || randIndex ==(spawnLocations.count - 1)){
        lineFromEdge = YES;
        leftEdge = randIndex == 0;
    }    //检查你留下的空白
    BOOL didLeaveGap = ABS(randIndex - previousIndex)GT; 1
    如果(didLeaveGap)lineFromEdge = NO;    如果((lineFromEdge&安培;&安培; leftEdge和放大器;&安培; randomIndex == spawnLocations.count)||
        (lineFromEdge&安培;&安培;!leftEdge和放大器;&安培; randomIndex == 0)){        //您已绘制的线从一个边缘到另一个而不留间隙;
        //计算另一个索引并再次执行相同的检查
        返回[个体经营randomIndex]    }    //你的指数是有效的
    previousIndex = randIndex;
    返回randIndex;
}


  

请注意:你的算法必须返回 0 spawnLocations.count 非常第一指数为这个工作。否则你的头骨可以在中心的启动和楔仍然玩家没有你意识到这一点。


Ok so I have this code where spawn locations are put into an array and then one of the locations is picked at random with this code:

let randx = spawnLocations[Int(arc4random_uniform(UInt32(spawnLocations.count)))]
            obstacle.position = CGPoint(x: randx, y: 0)

Object Spawning code:

var spawnLocations:[CGFloat] = [] 

func getObjectSpawnLocation() {

//Create 5 possible spawn locations
let numberOfNodes = 5

    // Spacing between nodes will change if: 1) number of nodes is changed, 2) screen width is changed, 3) node's size is changed.
    for i in 0...numberOfNodes - 1 {

        // spacing used to space out the nodes according to frame (which changes with screen width)
        var xPosition = (frame.maxX /*- thePlayer.size.width*/) / CGFloat((numberOfNodes - 1)) * CGFloat(i)

        //add a half of a player's width because node's anchor point is (0.5, 0.5) by default
        xPosition += thePlayer.size.width/2

        //I have no idea what this does but it works.
        xPosition -= frame.maxX/1.6
        spawnLocations.append( xPosition )

    }
  ()
}

But I have a problem because sometimes the game spawns the objects like in the picture below and it does not let my player advance any further without them dying and so my question is:

Is there anyway I can stop it from doing this?

maybe take one of the spawning locations out of the array temporally?

I should also note that each of the objects (Skulls) are spawned one after the other not all at once and the skulls can spawn at any of the 5 horizontal locations.

解决方案

The player can only be trapped between the skulls and the screen's edge. You should keep track whether or not you are currently "wedging" the player in or not, for example:

//Keep instance variables to track your current state
BOOL lineFromEdge; //YES if you're currently drawing a "line" of skulls from an edge
BOOL leftEdge; //YES if the line originates from the left edge, NO if from the right
int previousIndex;

Then you determine the value as follows:

- (int) randomIndex {

    int randIndex = Int(arc4random_uniform(UInt32(spawnLocations.count)));

    // This expression tells you if your current index is at an edge
    if (randIndex == 0 || randIndex == (spawnLocations.count - 1)) {
        lineFromEdge = YES;
        leftEdge = randIndex == 0;
    }

    //Check if you left a gap
    BOOL didLeaveGap = abs(randIndex - previousIndex) > 1
    if (didLeaveGap) lineFromEdge = NO;

    if ((lineFromEdge && leftEdge && randomIndex == spawnLocations.count) ||
        (lineFromEdge && !leftEdge && randomIndex == 0)) {

        //You have drawn a line from one edge to the other without leaving a gap;
        //Calculate another index and perform the same checks again
        return [self randomIndex];

    }

    //Your index is valid
    previousIndex = randIndex;
    return randIndex;
}

Note: Your algorithm must return 0 or spawnLocations.count as very first index for this to work. Else your skulls may start at the center and still wedge the player in without you realizing it.

这篇关于取阵列选择出一个周期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆