gpt4 book ai didi

swift - 如何添加 30 秒计时器来结束游戏回合?

转载 作者:搜寻专家 更新时间:2023-10-31 23:06:04 25 4
gpt4 key购买 nike

我目前正在试验一些我在 Internet 上找到的关于游戏的代码,在该游戏中,您必须点击一组项目并避免点击另一组项目。我目前正在尝试向游戏中添加一个计时器,以便它总共持续 30 秒,但我真的很难做到这一点,因为我对这种编程语言非常缺乏经验。

import UIKit
import QuartzCore
import SceneKit

class GameViewController: UIViewController, SCNSceneRendererDelegate {

var gameView:SCNView!
var SceneGame:SCNScene!
var NodeCamera:SCNNode!
var targetCreationTime:TimeInterval = 0


override func viewDidLoad() {
super.viewDidLoad()

View_in()
initScene()
initCamera()
}

func View_in(){
gameView = self.view as! SCNView
gameView.allowsCameraControl = true
gameView.autoenablesDefaultLighting = true

gameView.delegate = self
}

func initScene (){
SceneGame = SCNScene()
gameView.scene = SceneGame

gameView.isPlaying = true

}

func initCamera(){
NodeCamera = SCNNode()
NodeCamera.camera = SCNCamera()

NodeCamera.position = SCNVector3(x:0, y:5, z:10)

SceneGame.rootNode.addChildNode(NodeCamera)
}

func createTarget(){

let geometry:SCNGeometry = SCNPyramid( width: 1, height: 1, length: 1)

let randomColor = arc4random_uniform(2
) == 0 ? UIColor.green : UIColor.red

geometry.materials.first?.diffuse.contents = randomColor

let geometryNode = SCNNode(geometry: geometry)
geometryNode.physicsBody = SCNPhysicsBody(type: .dynamic, shape: nil)


if randomColor == UIColor.red {
geometryNode.name = "enemy"
}else{
geometryNode.name = "friend"
}


SceneGame.rootNode.addChildNode(geometryNode)


let randomDirection:Float = arc4random_uniform(2) == 0 ? -1.0 : 1.0

let force = SCNVector3(x: randomDirection, y: 15, z: 0)

geometryNode.physicsBody?.applyForce(force, at: SCNVector3(x: 0.05, y: 0.05, z: 0.05), asImpulse: true)
}

func renderer(_ renderer: SCNSceneRenderer, updateAtTime time: TimeInterval) {
if time > targetCreationTime{
createTarget()
targetCreationTime = time + 0.6
}

cleanUp()
}

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = touches.first!

let location = touch.location(in: gameView)

let hitList = gameView.hitTest(location, options: nil)

if let hitObject = hitList.first{
let node = hitObject.node

if node.name == "friend" {
node.removeFromParentNode()
self.gameView.backgroundColor = UIColor.black
}else {
node.removeFromParentNode()
self.gameView.backgroundColor = UIColor.red
}
}
}

func cleanUp() {
for node in SceneGame.rootNode.childNodes {
if node.presentation.position.y < -2 {
node.removeFromParentNode()
}
}
}

override var shouldAutorotate: Bool {
return true
}

override var prefersStatusBarHidden: Bool {
return true
}

override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
if UIDevice.current.userInterfaceIdiom == .phone {
return .allButUpsideDown
} else {
return .all
}
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Release any cached data, images, etc that aren't in use.
}

}

最佳答案

您可以使用 Timer 对象,已记录 here .只需在您希望游戏开始时设置计时器,可能是在您完成所有初始化后。当您设置计时器时,只需等待它在完成时回调您的代码,然后运行您想用来终止游戏的任何逻辑。

关于swift - 如何添加 30 秒计时器来结束游戏回合?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55715884/

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