gpt4 book ai didi

ios - 在swift中删除元素时索引超出范围数组错误

转载 作者:行者123 更新时间:2023-11-28 10:57:32 25 4
gpt4 key购买 nike

所以我有一个生成地雷和硬币的平台,所以首先我使用根据屏幕宽度和播放器的大小创建的一堆生成位置来生成地雷,如下所示:

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()

}
}

然后在 spawn mine 函数的末尾,我从数组中删除了地雷位置,这样它就不能被再次拾取,然后我运行创建特殊对象函数,它在平台上创建硬币对象,如下所示:

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)

}
}
}

当仅从数组中删除一个潜在的生成位置时,所有这些都可以正常工作,但是当我尝试从数组中删除两个甚至三个位置时,这些位置在一个平台上有多个地雷并运行游戏时它崩溃并给出我说索引超出范围的 fatal error 。我在这里做错了什么,我该如何解决?如果平台上有多个地雷,我会像这样删除它们

  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()

}

最佳答案

在这里,我给你举个例子,你在下面的代码工作中到底想做什么

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

在你的项目中,你的数组有四个值

Index - > 0, 1, 2, 3

现在,您删除索引处的对象:0,-> spawnLocations.remove(at: 0)所以现在你更新的数组有 3 个值,

index -> 0, 1, 2

在下一步中,您删除索引:2 -> spawnLocations.remove(at: 2)

所以更新的数组将是,

index -> 0, 1

现在在同一个数组中,您在索引 0 和 1 处有 2 个值,您正在尝试删除索引处的对象:4 -> spawnLocations.remove(at: 4)

希望你弄错了。

解决方案

颠倒删除数组对象的顺序

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

关于ios - 在swift中删除元素时索引超出范围数组错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42432475/

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