私には10レベルのゲームがあり、各レベルにはユーザーが収集できるオーブがあります。ユーザーがorbを最初に収集すると、ラベルに+1が追加され、NSUserDefaultsを使用して保存する必要があります。それはうまくいきますが、私が同じレベルを演奏し、オーブをもう一度収集すると、別の+1がラベルに追加されません。私は各レベルの各球に+1を加える必要があります。これは、ユーザーがすべてのオーブを収集した場合、ラベルに保存された合計10桁になります。私は間違って何をしていますか?何の生来の重複防止がないのでUserDefaults
でInt
値を追跡Swiftのラベルに整数を1回だけ保存できるようにするフラグを設定するにはどうすればよいですか?
class LevelOne: SKScene, SKPhysicsContactDelegate {
var didCollectOrb = true
override func didMove(to view: SKView) {
if didCollectOrb == true {
UserDefaults().set(UserDefaults().integer(forKey: "saveOrbs")+0, forKey:"saveOrbs")
print("will add nothing to label")
}
}
func didBegin(_ contact:SKPhysicsContact){
var firstBody: SKPhysicsBody
var secondBody: SKPhysicsBody
// 2. Assign the two physics bodies so that the one with the lower category is always stored in firstBody
if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask {
firstBody = contact.bodyA
secondBody = contact.bodyB
} else {
firstBody = contact.bodyB
secondBody = contact.bodyA
}
if firstBody.categoryBitMask == HeroCategory && secondBody.categoryBitMask == OrbCategory {
//saves orbs
if didCollectOrb {
UserDefaults().set(UserDefaults().integer(forKey: "saveOrbs")+1, forKey:"saveOrbs")
print("will add +1 to label")
}
}
}
}
ラベルの 'text'値はどこで設定していますか? –
orbLabel.text = UserDefaults()。整数(forKey: "saveOrbs")。説明 – coding22
あなたのコメントを誤読します。didMoveToViewのGameScene.swiftファイルにラベルがあります。 – coding22