我正在研究一个非常简单的快速游戏。游戏的主要布局是带有16个单元(门)的UICollectionView。其中1个单元(门)是单元格上的游戏。您需要点击(打开)除单元格上的游戏以外的所有门。如果一个单元格的游戏被挖掘,细胞将全部随机化,并且你失去了生命。如果你打开了除单元格游戏之外的所有门,你就赢了。在水龙头禁用UICollectionViewCell,并随机化单元格
我的代码如下。
什么工作?
- 在初始负载,门得到与 15 DoorIdentifier和1个GameOverIdentifier的阵列适当随机化。这就是我在 上通过单元格检测游戏的方式。
什么不行?
如果我对一些正常的门点按(禁用它们),然后点击在游戏 在门,它周围洗牌门(因为它应该),但随后 其他随机门打开禁用,看启用(由1.0阿尔法)。不打算。
如果我对一些正常的门点按(禁用它们),然后点击在游戏结束的门,所有的门变成1.0α,只有一些启用 互动。此外,在按下游戏结束门后,有时只有1个门将保持0.2 alpha。
我需要什么? 当正常的门被轻敲时,该单元现在总是被禁用。如果一场比赛的门被击中,它将忽略那些已经被击中的单元格,因为它们应该“不在游戏中”。
import UIKit
class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {
@IBOutlet weak var mainCollectionView: UICollectionView!
var gameOverDoorArray = ["GameOverIdentifier"]
var normalDoors = ["DoorIdentifier", "DoorIdentifier", "DoorIdentifier", "DoorIdentifier", "DoorIdentifier", "DoorIdentifier", "DoorIdentifier", "DoorIdentifier", "DoorIdentifier", "DoorIdentifier", "DoorIdentifier", "DoorIdentifier", "DoorIdentifier", "DoorIdentifier", "DoorIdentifier"]
var reuseIdentifiers = [String]()
var livesLeftCounter = 3
@IBAction func randomizeButton(sender: UIButton) {
randomizeReuseIdentifiers()
}
override func viewDidLoad() {
super.viewDidLoad()
mainCollectionView.dataSource = self
mainCollectionView.delegate = self
mainCollectionView.backgroundColor = UIColor.clearColor()
reuseIdentifiers = gameOverDoorArray + normalDoors
randomizeReuseIdentifiers()
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return reuseIdentifiers.count
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let identifier = reuseIdentifiers[indexPath.item]
let cell: DoorCollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier(identifier, forIndexPath: indexPath) as! DoorCollectionViewCell
cell.backgroundColor = UIColor(patternImage: UIImage(named: "doorClosedImage")!)
return cell
}
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
let currentCell = mainCollectionView.cellForItemAtIndexPath(indexPath) as UICollectionViewCell!
// Game Over door was pressed!
if reuseIdentifiers[indexPath.item] == "GameOverIdentifier" {
gameOverDetection()
randomizeReuseIdentifiers()
mainCollectionView.reloadData()
}
// Normal door was pressed!
else if reuseIdentifiers[indexPath.item] == "DoorIdentifier"{
if currentCell.userInteractionEnabled && (normalDoors.count > 0){
currentCell.alpha = 0.2
print("reuse ID's array - \(reuseIdentifiers)")
// when all the above is done, the button gets disabled.
currentCell.userInteractionEnabled = false
// Remove last item in normalDoors Array.
normalDoors.removeLast()
}
print("Array count of allTheDoors - \(reuseIdentifiers.count).")
}
}
func randomizeReuseIdentifiers() {
var randomized = [String]()
for _ in reuseIdentifiers {
let random = reuseIdentifiers.removeAtIndex(Int(arc4random_uniform(UInt32(reuseIdentifiers.count))))
randomized.append(random)
}
reuseIdentifiers = randomized
}
func gameOverDetection() {
livesLeftCounter -= 1
if livesLeftCounter < 0 {
print("GAME OVER!")
}
print(livesLeftCounter)
}
}
我不明白你的游戏的所有细节,但我看了看代码,发现了一些可能导致这些问题的缺陷。请注意,您使用单元格本身来存储有关是否启用此门的信息。它不会工作。您应该创建一些属性(可能是数组),并将其存储在其中,而不是单元格中。在'cellForItemAtIndexPath'里面,你需要根据这个数组的信息更新单元格的状态。 –
通过保持重用标识符阵列看起来很奇怪,还存储游戏在单元格上的信息。你只需要1个数字。 –