【发布时间】:2016-08-19 03:16:36
【问题描述】:
我正在开发一个非常简单的快速游戏。游戏的主要布局是一个带有 16 个单元(门)的 UICollectionView。其中 1 个单元(门)是一个单元格上的游戏。您需要点击(打开)除了游戏结束单元格之外的所有门。如果在单元格上的游戏被点击,单元格将全部随机化,您将失去生命。如果你打开除了游戏结束单元之外的所有门,你就赢了。
我的代码如下。
什么有效?
- 在初始加载时,门会通过一组 15 个 DoorIdentifier 和 1 个 GameOverIdentifier。我就是这样 检测单元格上的游戏。
什么不起作用?
如果我点击一些普通门(禁用它们),然后点击游戏 越过门,它会左右移动门(应该如此),但随后 其他随机门被禁用并看起来启用(1.0 alpha)。不打算。
如果我点击一些普通门(禁用它们),然后点击 Game Over 门,所有门都变为 1.0 alpha,只有一些门有 启用交互。此外,在按下 Game Over 门后, 有时只有一扇门会维持 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 个号码。
标签: ios swift random uicollectionview cell