I am working on a very simple swift game. The main layout of the game is a UICollectionView with 16 cells (doors.) 1 of the cells (doors) is a game over cell. You need to tap (open) all doors besides the game over cell. if a game over cell gets tapped, the cells will all randomize, and you lose a life. If you open all doors besides the game over cell, you win.
My code is below.
What works?
- On initial load, the doors get randomized properly with an array of 15 DoorIdentifier and 1 GameOverIdentifier. This is how I am detecting the game over cells.
What doesn't work?
-
If I tap on some normal doors (to disable them), then tap on the Game Over door, it shuffles the doors around (as it should), but then other random doors turned disabled and look enabled (by 1.0 alpha). Not intended.
-
If I tap on some normal doors (to disable them), then tap on the Game Over door, all doors become 1.0 alpha, and only some have interaction enabled. Additionally, after pressing the Game Over door, sometimes only 1 door will maintain the 0.2 alpha.
What I Need? When a normal door gets tapped, that cell will now always become disabled. If a game over door gets tapped, it will disregard the cells that were already tapped, as they should be "out of the game play."
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)
}
}
Aucun commentaire:
Enregistrer un commentaire