Out-of-range array error to delete an item in Quick mode

So I have a platform that spawns a mine and coins on it so first i spawn the mine using a bunch of spawn locations created basd on the size of the screen width and player like so:

var spawnLocations:[CGFloat] = []

func getObjectSpawnLocation() {

    spawnLocations.removeAll()

    //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) (player width go up for closer nodes down for bigger space)
        var xPosition = (frame.maxX + thePlayer.size.width / 0.57) / 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 //2

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

    }

  ()
}

func spawnMine() {

  if (theType == LevelType.normal && imageName == "WallObjectFarLeft") {

     for _ in 0...0 {

        getObjectSpawnLocation()

        let obstacle:Object = Object()
        obstacle.theType = LevelType.normal
        obstacle.createObject()
        obstacle.zPosition = 100

        addChild(obstacle)
        let randx = spawnLocations[0]
        obstacle.position = CGPoint(x: randx, y: 0)

        spawnLocations.remove(at: 0)
        createSpecialObject()

    }
  }

then at the end of the spawn mine function I remove the mines location from the array so it can’t be picked again and then I run the create special object function which creates the coin object on the platform like so:

func createSpecialObject() {

  print("(spawnLocations.count)")

        for i in 0..<spawnLocations.count {

            let diceRoll = arc4random_uniform(7) + 1

            if (diceRoll <= 2) {

                let specialObstacle:Object = Object()
                specialObstacle.theType = LevelType.normal
                specialObstacle.createSpecialObject()
                specialObstacle.zPosition = 100

                addChild(specialObstacle)
                var randx = spawnLocations[i]

                specialObstacle.position = CGPoint(x: randx, y: 0)

        }
    }
}

All this works fine when only removing one of the potential spawn locations from the array but when I try and remove two or even three locations from the array that have more than one mine on a platform and run the game it crashes and gives me a fatal error saying index out of range. What am I doing wrong here and how can I fix this? I remove them like so if there are more than one mine on a platform

else if (theType == LevelType.normal && imageName == "WallObjectFarLeft&Middle&FarRight") {

    for num2 in 0 ..< 3 {

        getObjectSpawnLocation()

        let obstacle:Object = Object()
        obstacle.theType = LevelType.normal
        obstacle.createObject()
        obstacle.zPosition = 100

        addChild(obstacle)
        var randx = spawnLocations[0]

            if num2 == 1 {

                randx = spawnLocations[2]
            }

            if num2 == 2 {

                randx = spawnLocations[4]
            }

        obstacle.position = CGPoint(x: randx, y: 0)

    }

    spawnLocations.remove(at: 0)
    spawnLocations.remove(at: 2)
    spawnLocations.remove(at: 4)
    createSpecialObject()

  }

Here I am giving you example what actually what you’r trying to do in you below code work

spawnLocations.remove(at: 0)
spawnLocations.remove(at: 2)
spawnLocations.remove(at: 4)

In your project, your array have four values

Index - > 0, 1, 2, 3

Now, you remove object at index : 0, -> spawnLocations.remove(at: 0)
So now your updated array have 3 values,

index -> 0, 1, 2

In next step, you remove index : 2 -> spawnLocations.remove(at: 2)

So updated array will be,

index -> 0, 1

Now in the same array you have 2 values at index 0 & 1 and your are tring to remove object at index : 4 -> spawnLocations.remove(at: 4)

Hope you get your mistake.

Solution

Reverse you order of removing array object

spawnLocations.remove(at: 4)
spawnLocations.remove(at: 2)
spawnLocations.remove(at: 0)
Hello, buddy!稿源:Hello, buddy! (源链) | 关于 | 阅读提示

本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。
酷辣虫 » 移动开发 » Out-of-range array error to delete an item in Quick mode

喜欢 (0)or分享给?

专业 x 专注 x 聚合 x 分享 CC BY-NC-SA 4.0

使用声明 | 英豪名录