gpt4 book ai didi

swift - 在 if 语句中更改计时器的时间间隔

转载 作者:行者123 更新时间:2023-11-30 10:04:47 25 4
gpt4 key购买 nike

我正在创建一个游戏,其中涉及一个计时器,该计时器每秒都会生成从屏幕上掉落的对象。要获得积分,您必须捕获物体。

我希望一旦玩家达到一定数量的分数,产卵率就会增加。

我尝试通过为计时器的间隔分配一个整数(SpeedNumber)值 1 秒来开始,并创建了 if 语句,一旦玩家达到一定数量,该语句应该将该整数更改为 0.5点。这对我来说很有意义,但不起作用。

为什么这不起作用?我应该更改什么?

import SpriteKit

struct physicsCatagory {
static let person : UInt32 = 0x1 << 1
static let Ice : UInt32 = 0x1 << 2
static let IceTwo : UInt32 = 0x1 << 3
static let IceThree : UInt32 = 0x1 << 4
static let Score : UInt32 = 0x1 << 5
}


class GameScene: SKScene, SKPhysicsContactDelegate {


var scorenumber = Int()
var lifenumber = Int()
var SpeedNumber : Double = 0.5
var person = SKSpriteNode(imageNamed: "Person")
let Score = SKSpriteNode()
var ScoreLable = SKLabelNode()


override func didMoveToView(view: SKView) {

self.scene?.backgroundColor = UIColor.purpleColor()

physicsWorld.contactDelegate = self

self.scene?.size = CGSize(width: 640, height: 1136)

lifenumber = 0
SpeedNumber = 1

Score.size = CGSize(width: 648, height: 1)
Score.position = CGPoint(x: 320, y: -90)
Score.physicsBody = SKPhysicsBody(rectangleOfSize: Score.size)
Score.physicsBody?.affectedByGravity = false
Score.physicsBody?.dynamic = false
Score.physicsBody?.categoryBitMask = physicsCatagory.Score
Score.physicsBody?.collisionBitMask = 0
Score.physicsBody?.contactTestBitMask = physicsCatagory.IceThree
Score.color = SKColor.blueColor()
self.addChild(Score)



person.position = CGPointMake(self.size.width/2, self.size.height/12)
person.setScale(0.4)
person.physicsBody = SKPhysicsBody(rectangleOfSize: person.size)
person.physicsBody?.affectedByGravity = false
person.physicsBody?.categoryBitMask = physicsCatagory.person
person.physicsBody?.contactTestBitMask = physicsCatagory.Ice
person.physicsBody?.collisionBitMask = physicsCatagory.Ice
person.physicsBody?.dynamic = false




ScoreLable.position = CGPoint(x: self.frame.width / 2, y: 1000)
ScoreLable.text = "\(scorenumber)"
ScoreLable.fontColor = UIColor.yellowColor()
ScoreLable.fontSize = 100
ScoreLable.fontName = "Zapfino "
self.addChild(ScoreLable)



var IceThreeTimer = NSTimer.scheduledTimerWithTimeInterval(SpeedNumber, target: self, selector: ("spawnThirdIce"), userInfo: nil, repeats: true)


self.addChild(person)





}




func didBeginContact(contact: SKPhysicsContact) {
let firstBody = contact.bodyA
let secondBody = contact.bodyB


if firstBody.categoryBitMask == physicsCatagory.person && secondBody.categoryBitMask == physicsCatagory.IceThree || firstBody.categoryBitMask == physicsCatagory.IceThree && secondBody.categoryBitMask == physicsCatagory.person{

scorenumber++

if scorenumber == 5 {

SpeedNumber = 0.5


}


ScoreLable.text = "\(scorenumber)"
CollisionWithPerson(firstBody.node as! SKSpriteNode, Person: secondBody.node as! SKSpriteNode)


}

if firstBody.categoryBitMask == physicsCatagory.Score && secondBody.categoryBitMask == physicsCatagory.IceThree ||
firstBody.categoryBitMask == physicsCatagory.IceThree && secondBody.categoryBitMask == physicsCatagory.Score{
lifenumber++

if lifenumber == 3{

self.view?.presentScene(EndScene())

}

//self.view?.presentScene(EndScene())

}
}



func CollisionWithPerson (Ice: SKSpriteNode, Person: SKSpriteNode){

Person.removeFromParent()

}



func spawnThirdIce(){

var Ice = SKSpriteNode(imageNamed: "Ice")
Ice.setScale(0.9)
Ice.physicsBody = SKPhysicsBody(rectangleOfSize: Ice.size)
Ice.physicsBody?.categoryBitMask = physicsCatagory.IceThree
Ice.physicsBody?.contactTestBitMask = physicsCatagory.person | physicsCatagory.Score
Ice.physicsBody?.affectedByGravity = false
Ice.physicsBody?.dynamic = true
let MinValue = self.size.width / 8
let MaxValue = self.size.width - 20
let SpawnPoint = UInt32(MaxValue - MinValue)
Ice.position = CGPoint(x: CGFloat(arc4random_uniform(SpawnPoint)), y: self.size.height)
self.addChild(Ice)

let action = SKAction.moveToY(-85, duration: 2.5)
let actionDone = SKAction.removeFromParent()
Ice.runAction(SKAction.sequence([action,actionDone]))


}



override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {


for touch in touches {
let location = touch.locationInNode(self)

person.position.x = location.x



}
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {

for touch in touches {
let location = touch.locationInNode(self)

person.position.x = location.x



}
}




override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
}
}

最佳答案

您可以按如下方式执行您想要的操作:

首先声明 2 个属性(在类中但在所有函数定义之外)

var timeOfLastSpawn: CFTimeInterval = 0.0
var timePerSpawn: CFTimeInterval = 1.0

然后,在Update中检查是否超出了timePerSpawn。如果是这样,请调用生成新对象的生成过程,然后重置自上次生成以来的时间:

override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
if (currentTime - timeOfLastSpawn > timePerSpawn) {
spawnObject()
self.timeOfLastSpawn = currentTime
}
}

func spawnObject() {
// Your spawn code here
}

使生成过程成为一个单独的函数的优点是,您可以从 didMoveToView 或任何其他位置调用它,以在正常时间控制周期之外生成对象。

您可以根据需要更改 timePerSpawn 的值,以控制生成对象的速率。

您还可以考虑创建一个在指定时间间隔运行 spawnObjectSKAction ,但我认为要更改生成对象的速率,您将拥有删除并重新创建 SKAction,但您可以在 timePerSpawn 的 setter 中执行此操作。

你不应该在 SpriteKit 中真正使用 NSTimer,因为 SpriteKit 引擎将不知道计时器在做什么并且无法控制它(一个例子是,如果将场景设置为暂停,计时器会继续运行)。

关于swift - 在 if 语句中更改计时器的时间间隔,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36638868/

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