gpt4 book ai didi

ios - swift - 游戏倒数计时器几乎可以工作

转载 作者:行者123 更新时间:2023-11-29 01:44:58 25 4
gpt4 key购买 nike

我正在使用脚本(如下),用作我游戏开始的倒计时,我使用的脚本来自 Gourav Nayyar 的 YouTube 视频,第一次调用时效果很好。但是,一旦游戏完成重置过程并再次调用脚本,我只会看到 5 而不是 5 - 4 - 3 - 2 - 1 - GO!。如果我从我的脚本中删除其中一个 cals,那么它可以在重置函数中或 gameScene 加载时正常工作。

这是 GameScene.swift 中的两个调用

override func didMoveToView(view: SKView) {

var gamelaunchTimerView:TimerView = TimerView.loadingCountDownTimerViewInView(self.view!)
gamelaunchTimerView.startTimer()

func resetScene() {

//code removed from here
return countdown()

}

func countdown() {
var gamelaunchTimerView:TimerView = TimerView.loadingCountDownTimerViewInView(self.view!)
gamelaunchTimerView.startTimer()

}

这是 GameLaunchTimer.swift 中的计时器代码,因为它设置了倒计时仅在第一次调用时有效并在第二次调用时挂起。

//
// TimerView.swift
// GameLaunchTimer
//
// Created by Gourav Nayyar on 7/3/14.
// Copyright (c) 2014 Gourav Nayyar. All rights reserved.
//

let VIEW_ALPHA:CGFloat = 0.5
let TIMERVIEW_RADIUS:CGFloat = 50
let TIMER_LABEL_INITIAL_VAL:Int = 5
let BORDER_WIDTH:CGFloat = 2
var timerVal:Int = TIMER_LABEL_INITIAL_VAL;
var timer:NSTimer!


import UIKit
import QuartzCore

class TimerView :UIView {

struct Stored {
static var timerLbl:UILabel!
}


class func loadingCountDownTimerViewInView (_superView:UIView)-> TimerView
{
var timerView:TimerView = TimerView(frame:_superView.frame)
// timerView.backgroundColor = UIColor.blackColor().colorWithAlphaComponent(VIEW_ALPHA)
_superView.addSubview(timerView)

/* add a custom Circle view */

let refFrame:CGRect = CGRectMake(_superView.center.x-TIMERVIEW_RADIUS, _superView.center.y-TIMERVIEW_RADIUS, 2*TIMERVIEW_RADIUS, 2*TIMERVIEW_RADIUS)
var circleView:UIView = UIView(frame:refFrame)
circleView.layer.cornerRadius = TIMERVIEW_RADIUS
circleView.layer.borderColor = UIColor.whiteColor().CGColor
circleView.layer.borderWidth = BORDER_WIDTH

/* add a custom Label */

Stored.timerLbl = UILabel(frame:circleView.bounds)
Stored.timerLbl.text = "\(TIMER_LABEL_INITIAL_VAL)"
Stored.timerLbl.textColor = UIColor.whiteColor()
Stored.timerLbl.font = UIFont(name: "MarkerFelt-Thin", size: 40)
Stored.timerLbl.textAlignment = NSTextAlignment.Center

circleView.addSubview(Stored.timerLbl)
timerView.addSubview(circleView)

return timerView
}

func startTimer()
{
timer = NSTimer.scheduledTimerWithTimeInterval(1.0
, target: self, selector: Selector("updateTimer:"), userInfo: nil, repeats: true)
}

func updateTimer(dt:NSTimer)
{
timerVal--
if timerVal==0{
Stored.timerLbl.text = "GO!"
}else if timerVal<0{
timer.invalidate()
removeCountDownTimerView()
} else{
Stored.timerLbl.text = "\(timerVal)"
}
}

func removeCountDownTimerView()
{
var mySuperView:UIView = self.superview!
mySuperView.userInteractionEnabled = true
super.removeFromSuperview()
}
}

最佳答案

在类体下定义你的变量;

import UIKit
import QuartzCore

class TimerView :UIView {

let VIEW_ALPHA:CGFloat = 0.5
let TIMERVIEW_RADIUS:CGFloat = 50
let TIMER_LABEL_INITIAL_VAL:Int = 5
let BORDER_WIDTH:CGFloat = 2
var timerVal:Int = TIMER_LABEL_INITIAL_VAL;
var timer:NSTimer!

... // other code

或者,可能是

    let VIEW_ALPHA:CGFloat = 0.5
let TIMERVIEW_RADIUS:CGFloat = 50
let TIMER_LABEL_INITIAL_VAL:Int = 5
let BORDER_WIDTH:CGFloat = 2

import UIKit
import QuartzCore

class TimerView :UIView {

var timerVal:Int = TIMER_LABEL_INITIAL_VAL;
var timer:NSTimer!
... //other code

关于ios - swift - 游戏倒数计时器几乎可以工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32027729/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com