2016-12-14 7 views
0

Donc j'essaie de faire un compte à rebours pour mon jeu. Fondamentalement, il y a un joueur en bas de l'écran qui tire une flèche sur un objet en mouvement au-dessus. Finalement, je vais avoir un jeu de fin où, si vous n'avez plus de flèches ou de temps, il sera exécuté. J'ai du mal à placer la fonction et NSTimer/Timer correctement dans le code. S'il vous plaît aider le chronomètre devrait commencer à 30 et soustraire 1 chaque seconde passée. Merci d'avance!!!J'essaie de faire une minuterie dans Swift avec un intervalle de temps

code cible:

let timerLabel = SKLabelNode(fontNamed: "The Bold Font") 

var timer = Timer() 

var counter = 30 

func viewDidLoad() { 


     timerLabel.text = String(counter) 
     timerLabel.fontSize = 250 
     timerLabel.fontColor = SKColor.white 
     timerLabel.verticalAlignmentMode = SKLabelVerticalAlignmentMode.center 
     timerLabel.position = CGPoint(x: self.size.width/2, y: self.size.height*0.70) 
     timerLabel.zPosition = 100 
     self.addChild(timerLabel) 

     timer = Timer.scheduledTimer(timeInterval: 1, target:self, selector: #selector(GameScene.updateCounter), userInfo: nil, repeats: true)() 
    } 

    func updateCounter(){ 

     timerLabel.text = String(describing: counter -= 1) 
    } 

complet Code:

// 
// GameScene.swift 
// Andrey's Game 
// 
// Created by Jeffrey Foster on 12/2/16. 
// Copyright © 2016 Jeffrey Foster. All rights reserved. 
// 

import SpriteKit 
import UIKit 




class GameScene: SKScene, SKPhysicsContactDelegate { 

var gameScore = 0 
let scoreLabel = SKLabelNode(fontNamed: "The Bold Font") 

var spermCount = 60 
let spermLabel = SKLabelNode(fontNamed: "The Bold Font") 

let timerLabel = SKLabelNode(fontNamed: "The Bold Font") 
var timer = Timer() 
var counter = 30 

let andrey = SKSpriteNode(imageNamed: "Andreys_Ass") 

let player = SKSpriteNode(imageNamed: "Player_Cucumber") 




struct physicsCategories { 
    static let None: UInt32 = 0 
    static let Arrow : UInt32 = 0b1 //1 
    static let Andrey : UInt32 = 0b10 //2 
    static let Wall : UInt32 = 0b100 //4 

} 

var gameArea: CGRect 

override init(size: CGSize){ 

    let maxAspectRatio: CGFloat = 16.0/9.0 
    let playableWidth = size.height/maxAspectRatio 
    let margin = (size.width - playableWidth)/2 
    gameArea = CGRect(x: margin, y: 0, width: playableWidth, height: size.height) 

    super.init(size: size) 
} 

required init?(coder aDecoder: NSCoder) { 
    fatalError("init(coder:) has not been implemented") 
} 


override func didMove(to view: SKView) { 

    self.physicsWorld.contactDelegate = self 


    andrey.setScale(4) 
    andrey.position = CGPoint(x: self.size.width/2, y: self.size.height*0.50) 
    andrey.zPosition = 2 
    andrey.physicsBody = SKPhysicsBody(rectangleOf: andrey.size) 
    andrey.physicsBody!.affectedByGravity = false 
    andrey.physicsBody!.categoryBitMask = physicsCategories.Andrey 
    andrey.physicsBody!.collisionBitMask = physicsCategories.None 
    andrey.physicsBody!.contactTestBitMask = physicsCategories.Arrow 
    self.addChild(andrey) 

    let wall = SKSpriteNode(imageNamed: "wall") 
    wall.position = CGPoint(x: self.size.width/2, y: self.size.height*0.31) 
    wall.size.height = 0.000005 
    wall.size.width = self.size.width 
    wall.zPosition = 3 
    wall.physicsBody = SKPhysicsBody(rectangleOf: wall.size) 
    wall.physicsBody!.affectedByGravity = false 
    wall.physicsBody!.categoryBitMask = physicsCategories.Wall 
    wall.physicsBody!.collisionBitMask = physicsCategories.None 
    wall.physicsBody!.contactTestBitMask = physicsCategories.Arrow 
    self.addChild(wall) 



    let background = SKSpriteNode(imageNamed: "background") 
    background.size = self.size 
    background.position = CGPoint(x: self.size.width/2, y: self.size.height/2) 
    background.zPosition = 0 
    self.addChild(background) 


    player.setScale(1) 
    player.position = CGPoint(x: self.size.width/2, y: self.size.height*0.05) 
    player.zPosition = 2 
    self.addChild(player) 

    scoreLabel.text = "Score: 0" 
    scoreLabel.fontSize = 70 
    scoreLabel.fontColor = SKColor.white 
    scoreLabel.horizontalAlignmentMode = SKLabelHorizontalAlignmentMode.left 
    scoreLabel.position = CGPoint(x: self.size.width*0.15, y: self.size.height*0.00) 
    scoreLabel.zPosition = 100 
    self.addChild(scoreLabel) 

    spermLabel.text = "Sperm: 60" 
    spermLabel.fontSize = 70 
    spermLabel.fontColor = SKColor.white 
    spermLabel.horizontalAlignmentMode = SKLabelHorizontalAlignmentMode.right 
    spermLabel.position = CGPoint(x: self.size.width*0.85, y: self.size.height*0.00) 
    spermLabel.zPosition = 100 
    self.addChild(spermLabel) 

    func viewDidLoad() { 


     timerLabel.text = String(counter) 
     timerLabel.fontSize = 250 
     timerLabel.fontColor = SKColor.white 
     timerLabel.verticalAlignmentMode = SKLabelVerticalAlignmentMode.center 
     timerLabel.position = CGPoint(x: self.size.width/2, y: self.size.height*0.70) 
     timerLabel.zPosition = 100 
     self.addChild(timerLabel) 

     timer = Timer.scheduledTimer(timeInterval: 1, target:self, selector: #selector(GameScene.updateCounter), userInfo: nil, repeats: true)() 
    } 

    func updateCounter(){ 

     timerLabel.text = String(describing: counter -= 1) 
    } 




    func moveAndrey(){ 


      let moveAndreyRight = SKAction.moveTo(x: self.size.width, duration: 0.50) 
      let moveAndreyLeft = SKAction.moveTo(x: self.size.width*0.00, duration: 1.00) 
      let moveAndreyStart = SKAction.moveTo(x: self.size.width/2, duration: 0.50) 
      let andreySequence = SKAction.sequence([moveAndreyRight, moveAndreyLeft, moveAndreyStart]) 
      let endlessAction = SKAction.repeatForever(andreySequence) 
      andrey.run(endlessAction) 


    } 

    moveAndrey() 


} 







func addScore(){ 

    gameScore += 1 
    scoreLabel.text = "Score: \(gameScore)" 
} 


func subtractScore(){ 
    gameScore -= 1 
    scoreLabel.text = "Score: \(gameScore)" 
} 

func subtractSperm(){ 
    spermCount -= 1 
    spermLabel.text = "Sperm: \(spermCount)" 

} 



func didBegin(_ contact: SKPhysicsContact) { 

    var body1 = SKPhysicsBody() 
    var body2 = SKPhysicsBody() 

    if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask{ 
     body1 = contact.bodyA 
     body2 = contact.bodyB 
    } 
    else{ 
     body1 = contact.bodyB 
     body2 = contact.bodyA 
    } 

    if body1.categoryBitMask == physicsCategories.Arrow && body2.categoryBitMask == physicsCategories.Andrey{ 
     //if the arrow hits Andrey 

     if body1.node != nil { 
     spawnHit(spawnPosition: body1.node!.position) 
     } 

     addScore() 

     body1.node?.removeFromParent() 

    } 

    if body1.categoryBitMask == physicsCategories.Arrow && body2.categoryBitMask == physicsCategories.Wall{ 
     //if the arrow hits Wall 

     if body1.node != nil { 
      spawnMissedShot(spawnPosition: body1.node!.position) 
     } 

     subtractScore() 

     body1.node?.removeFromParent() 

    } 


} 

func spawnHit(spawnPosition: CGPoint){ 

    let hit = SKSpriteNode(imageNamed: "Andrey_Hit") 
    hit.position = spawnPosition 
    hit.zPosition = 4 
    hit.setScale(3) 
    self.addChild(hit) 


    let scaleIn = SKAction.scale(to: 1, duration: 0.1) 
    let fadeOut = SKAction.fadeOut(withDuration: 0.1) 
    let delete = SKAction.removeFromParent() 

    let hitSequence = SKAction.sequence([scaleIn, fadeOut, delete]) 

    hit.run(hitSequence) 

} 

func spawnMissedShot(spawnPosition: CGPoint){ 

    let missedShot = SKSpriteNode(imageNamed: "missedShot") 
    missedShot.position = spawnPosition 
    missedShot.zPosition = 4 
    missedShot.setScale(3) 
    self.addChild(missedShot) 


    let scaleIn = SKAction.scale(to: 1, duration: 0.1) 
    let fadeOut = SKAction.fadeOut(withDuration: 0.1) 
    let delete = SKAction.removeFromParent() 

    let hitSequence = SKAction.sequence([scaleIn, fadeOut, delete]) 

    missedShot.run(hitSequence) 

} 





func fireArrow() { 

    let arrow = SKSpriteNode(imageNamed: "Arrow_Cucumber") 
    arrow.name = "Arrow" 
    arrow.setScale(0.75) 
    arrow.position = player.position 
    arrow.zPosition = 3 
    arrow.physicsBody = SKPhysicsBody(rectangleOf: arrow.size) 
    arrow.physicsBody!.affectedByGravity = false 
    arrow.physicsBody!.categoryBitMask = physicsCategories.Arrow 
    arrow.physicsBody!.collisionBitMask = physicsCategories.None 
    arrow.physicsBody!.contactTestBitMask = physicsCategories.Andrey | physicsCategories.Wall 
    self.addChild(arrow) 


    let moveArrow = SKAction.moveTo(y: self.size.height+arrow.size.height, duration: 1.50) 
    let deleteArrow = SKAction.removeFromParent() 
    let arrowSequence = SKAction.sequence([moveArrow, deleteArrow]) 
    arrow.run(arrowSequence) 

} 





override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 
    fireArrow() 
    subtractSperm() 



    } 
} 

Répondre

1

Vous pouvez faire cela avec un Timer ou un SKAction avec des retards et des répétitions.

Pour utiliser une minuterie, appelez la méthode de classe scheduledTimer et spécifiez une méthode à appeler lorsqu'elle se déclenche.

timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(GameScene.updateCounter), userInfo: nil, repeats: true) 

Cette méthode devrait mettre à jour une variable compteur, afficher l'heure actuelle à l'utilisateur, et vérifier si le jeu sur les critères sont respectés.

func updateCounter() { 
    counter -= 1 
    timerLabel.text = "\(counter)" 
    if counter == 0 { gameOver() } 
} 

Dans la fonction gameOver vous pouvez arrêter le chronomètre en appelant

timer.invalidate() 

Comme la complexité de votre jeu se développe, vous pouvez factoriser différemment. Un autre méthode consiste à utiliser un SKAction.

let waitAction = SKAction.wait(forDuration: 1) 
let fireAction = SKAction.run { 
    counter -= 1 
    timerLabel.text = "\(counter)" 
    if counter == 0 { gameOver() } 
} 
let actionSequence = SKAction.sequence([waitAction, fireAction]) 
let repeatAction = SKAction.repeatForever(actionSequence) 
self.run(repeatAction, withKey: "timer") 

Dans la fonction gameOver vous pouvez arrêter l'action en appelant

self.removeAction(forKey: "timer") 

Hope that helps! Bonne chance avec votre jeu.

+0

Merci beaucoup, aidé une tonne! J'ai utilisé la fonction SKAction et cela a fonctionné parfaitement. Cependant, lorsque j'essaie de créer la fonction avec une minuterie, je ne sais pas trop où insérer la ligne scheduledTimer. Si vous pouviez me dire où cela appartient pour référence future, je l'apprécierais grandement. – Jfost99

+0

Appelez la méthode 'scheduledTimer' lorsque vous voulez que le minuteur démarre. Dans votre cas, une fois que le jeu commence. Si ma réponse vous a été utile, veuillez la marquer comme acceptée afin que les autres puissent en bénéficier à l'avenir. Bonne chance avec votre jeu! – nathan