I am currently experiencing difficulties in the development of my app. To make it simple, it is an application that allows you to do mental calculation.
My problem is with the display of the calculations that have been proposed, the results provided by the player, and the result of the calculation. In fact, when the program knows that there is no more calculation to propose it launches in the "case 2" of a function that contains a for
loop that calls a CreatLabelJuste()
function if the result provided by the player is correct, and a CreatLabelFAux()
function if the calculation is wrong.
These functions create labels. It works pretty well. The problem is that to restart a game, I call the viewDidLoad()
function which resets my game. But when the labels have to be recreated the old ones haven't removed themselves from the view that contains them!
I don't know if it's clear enough, but if someone knows if it's possible to "destroy" these labels, so that when the game starts again, in the end nothing overlaps.
func creatLabelJuste(TagX : Int) {
let labelJ = UILabel(frame: CGRect(x: 0, y: 0, width: 100, height: 20))
let labelForAnswerJ = UILabel(frame: CGRect(x: 0, y: 0, width: 100, height: 20))
let labelForResultJ = UILabel(frame: CGRect(x: 0, y: 0, width: 100, height: 20))
switch TagX {
case 0 :
labelJ.font = UIFont(name: "futura", size: 20)
labelJ.center = CGPoint(x: 96, y: yAxis)
labelJ.textAlignment = .center
labelJ.text = "\(memoriseCalcule[compter])³"
self.myView.addSubview(labelJ)
labelForAnswerJ.font = UIFont(name: "futura", size: 20)
labelForAnswerJ.center = CGPoint(x: 187, y: yAxis)
labelForAnswerJ.textAlignment = .center
labelForAnswerJ.text = "\(memoriseAnswer[compter])"
self.myView.addSubview(labelForAnswerJ)
labelForResultJ.font = UIFont(name: "futura", size: 20)
labelForResultJ.center = CGPoint(x: 278, y: yAxis)
labelForResultJ.textAlignment = .center
labelForResultJ.text = "\(memoriseResult[compter])"
self.myView.addSubview(labelForResultJ)
// print("/////////////////////////////////////CreateLabelJuste : LabelJ = \(labelJ.text)/ laberForAwnswerJ = \(labelForAnswerJ.text!)/ labelforResultJ+ = \(labelForResultJ.text) ////////////////////////////////////////////")
case 1 :
self.myView.removeFromSuperview()
default :
print("defaut")
}
}
func creatLabelFaux(Tag : Int) {
let label = UILabel(frame: CGRect(x: 0, y: 0, width: 100, height: 20))
let labelForAnswer = UILabel(frame: CGRect(x: 0, y: 0, width: 100, height: 20))
let labelForResult = UILabel(frame: CGRect(x: 0, y: 0, width: 100, height: 20))
label.text = ""
labelForResult.text = ""
labelForAnswer.text = ""
switch Tag {
case 0:
label.font = UIFont(name: "futura", size: 20)
label.center = CGPoint(x: 96, y: yAxis)
label.textAlignment = .center
label.text = "\(memoriseCalcule[compter])³"
label.backgroundColor = UIColor(red: 238/255, green: 105/255, blue: 86/255, alpha: 1)
label.layer.cornerRadius = 10
label.clipsToBounds = true
self.myView.addSubview(label)
labelForAnswer.font = UIFont(name: "futura", size: 20)
labelForAnswer.center = CGPoint(x: 187, y: yAxis)
labelForAnswer.textAlignment = .center
labelForAnswer.backgroundColor = UIColor(red: 238/255, green: 105/255, blue: 86/255, alpha: 1)
labelForAnswer.text = "\(memoriseAnswer[compter])"
self.myView.addSubview(labelForAnswer)
labelForResult.font = UIFont(name: "futura", size: 20)
labelForResult.center = CGPoint(x: 278, y: yAxis)
labelForResult.textAlignment = .center
labelForResult.backgroundColor = UIColor(red: 238/255, green: 105/255, blue: 86/255, alpha: 1)
labelForResult.text = "\(memoriseResult[compter])"
labelForResult.layer.cornerRadius = 10
labelForResult.clipsToBounds = true
self.myView.addSubview(labelForResult)
case 1 :
// label.delete(true)
//labelForAnswer.delete(true)
//labelForResult.delete(true)
self.myView.removeFromSuperview()
default:
print("merde")
}
}
for _ in 0..<setLimite{
yAxis = yAxis + 30
if memoriseAnswer[compter] == memoriseResult[compter]{
creatLabelJuste(TagX: 0)
} else {//if memoriseAnswer[compter] != memoriseResult[compter] {
creatLabelFaux(Tag: 0)
}
compter = compter + 1
}