我正在编写一个我有木地板的游戏,我希望在那里产生1到20个大理石。这些大理石占据了网格上20个设定位置中的1个。我希望弹珠滚到另一个目前没有定位的网格上;然后再一次这样做。
首先,我生成大理石可以放在网格上的所有可能位置(使用设置常数计算,在场景中给出20个位置):
func generateMarblePositions() {
let divisionsAcross = (randomMarbleX.highestValue - randomMarbleX.lowestValue)/SearchConstant.columns
let divisionsDown = (randomMarbleY.highestValue - randomMarbleY.lowestValue)/SearchConstant.rows
var nextPoint = CGPoint()
var startingPointX = randomMarbleX.lowestValue
var startingPointY = randomMarbleY.lowestValue
for divisionsX in 1...SearchConstant.rows {
for divisionsY in 1...SearchConstant.columns {
if divisionsX % 2 == 0 && divisionsY == 1 { // if it is even and it is the first row
startingPointX = startingPointX + divisionsAcross/2 // stagger the rows
}
nextPoint = CGPoint(x: startingPointX, y: startingPointY)
allPositions.append(nextPoint)
startingPointX = startingPointX + divisionsAcross
}
startingPointX = randomBoatX.lowestValue
startingPointY = startingPointY + divisionsDown
}
}
然后,我生成大理石的数量并将所有大理石放置在网格的某个位置。
func generateMarbleLocations(_ locationNumber: Int) {
for count in 0...(locationNumber - 1) {
hiddenLocationNode = LocationNode(imageNamed: images[count])
hiddenLocationNode.name = "\(count)"
hiddenLocationNode.size = SearchConstant.marbleSize
hiddenLocationNode.zPosition = 0
fgNode.addChild(hiddenLocationNode)
hiddenLocationNode.position = generateRandomLocation(check: hiddenLocationPositions)
hiddenLocationNode.didMoveToScene()
hiddenLocationNode = fgNode.childNode(withName: hiddenLocationNode.name!) as! LocationNode
hiddenLocationPositions[hiddenLocationNode.name!] = hiddenLocationNode.position
hiddenLocationsTried[hiddenLocationNode.name!] = 0
}
}
最后,我两次生成一系列新位置并移动节点。
func spinLocations() {
let firstSpinLocations = generateNewLocationArray(previous: hiddenLocationPositions)
let secondSpinLocations = generateNewLocationArray(previous: firstSpinLocations)
for (item, _) in hiddenLocationPositions {
let node = fgNode.childNode(withName: item) as! LocationNode
print("moving \(node.name!) from \(firstSpinLocations[node.name!]) to \(secondSpinLocations[node.name!])")
node.spin(position: firstSpinLocations[node.name!]!, finalPosition: secondSpinLocations[node.name!]!)
}
hiddenLocationPositions = secondSpinLocations
}
然而,将20个节点移动到随机生成的旋转位置两次似乎非常低效。有这样的原因吗?我怎么能更有效地做到这一点?