2016-12-14 10 views
0

私は自分のゲームのカウントダウンタイマーを作ろうとしています。基本的に画面の下部には、その上の動くオブジェクトに矢印を発するプレーヤーがいます。最終的に私はあなたが矢印を使い果たした場合、または実行する時間が終了するゲームを終了します。関数とNSTimer/Timerをコードに正しく配置するのに問題があります。タイマーが30から始まり、1秒ごとに1を引いてください。前もって感謝します!!!私は時間間隔でSwiftでタイマーを作ろうとしています

ターゲットコード:

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) 
    } 

全コード:

// 
// 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() 



    } 
} 

答えて

1

あなたはTimerや遅延を繰り返すとSKActionでこれを行うことができます。

タイマーを使用するには、scheduledTimerクラスメソッドを呼び出して、起動時に呼び出すメソッドを指定します。

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

この方法は、いくつかのカウンタ変数を更新し、ユーザーに現在の時刻を表示し、基準を超えるゲームが満たされているかどうかを確認する必要があります。あなたがゲームの複雑さが大きくなるにつれて、あなたは違っこれをリファクタリングしたいと思うかもしれ

timer.invalidate() 
を呼び出すことにより、タイマーを停止することができます gameOver機能で

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

もう1つの方法は、SKActionを使用することです。 gameOver機能で

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") 

あなたは助け

self.removeAction(forKey: "timer") 

希望を呼び出すことでアクションを停止することができます!あなたのゲームで幸運。

+0

ありがとう、トンを助けました!私はSKAction関数を使用し、それは完全に働いた。しかし、私がタイマーを使って関数を作成しようとすると、scheduledTimer行をどこに挿入するのか分かりません。それが将来の参照のためにどこに属しているか教えていただけたら、私はそれを高く評価します。 – Jfost99

+0

タイマーを開始するときに 'scheduledTimer'メソッドを呼び出します。あなたのケースでは、ゲームが始まると。私の答えがあなたにとって有益だったら、それを受け入れられたものとしてマークして、将来の他の人にも同様に恩恵を受けるようにしてください。あなたのゲームで幸運! – nathan

関連する問題