gpt4 book ai didi

ios - swift - 随着时间的推移增加物体的速度

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

我正在寻找一种方法来提高我的游戏节奏,你玩的时间越长,我想通过增加在特定时间后(即每 30 秒或最好是在 10 个对象后)生成障碍物的频率来实现这一点(树),已经生成,所以你玩的时间越长,它就会变得越难。

这是我当前的设置,我使用 repeatActionForever 如何将其更改为类似 repeatAction10Times 的设置,每个循环都有不同的延迟变量?

//in didMoveToView

treeTexture1 = SKTexture(imageNamed: "tree")
treeTexture1.filteringMode = SKTextureFilteringMode.Nearest

var distanceToMove = CGFloat(self.frame.size.width + 0.1);
var moveTrees = SKAction.moveByX(-distanceToMove, y:0, duration:NSTimeInterval(0.006 * distanceToMove));
var removeTrees = SKAction.removeFromParent();
moveAndRemoveTrees = SKAction.sequence([moveTrees, removeTrees]);

var spawn = SKAction.runBlock({() in self.spawnTrees()})

//this delay is what I would like to alter for each loop

var delay = SKAction.waitForDuration(NSTimeInterval(1.2))

var spawnThenDelay = SKAction.sequence([spawn, delay])
var spawnThenDelayForever = SKAction.repeatActionForever(spawnThenDelay)
self.runAction(spawnThenDelayForever)


func spawnTrees() {
var tree = SKNode()
tree.position = CGPointMake( self.frame.size.width + treeTexture1.size().width * 2, 0 );
tree.zPosition = -10;

var height = UInt32( self.frame.size.height / 1 )
var height_max = UInt32( 220 )
var height_min = UInt32( 100 )
var y = arc4random_uniform(height_max - height_min + 1) + height_min;

var tree1 = SKSpriteNode(texture: treeTexture1)
tree1.position = CGPointMake(0.0, CGFloat(y))
tree1.physicsBody = SKPhysicsBody(rectangleOfSize: tree1.size)
tree1.physicsBody?.dynamic = false
tree1.physicsBody?.categoryBitMask = treeCategory;
tree1.physicsBody?.collisionBitMask = 0
tree1.physicsBody?.contactTestBitMask = 0
tree.addChild(tree1)



tree.runAction(moveAndRemoveTrees)

trees.addChild(tree)

}

最佳答案

您应该尝试使用简单的action.speed代码。

例如:不要运行操作 spawnThenDelay 十次,然后运行一些代码并重复,而是尝试创建一个计数器。在代码的最顶部创建一个名为 counter 的全局变量,或者您想要的任何名称。在 spawnTrees() 中,将代码更改为:

func spawnTrees() {
var tree = SKNode()
tree.position = CGPointMake( self.frame.size.width + treeTexture1.size().width * 2, 0 );
tree.zPosition = -10;
counter++

... }

然后在 update() 中,检查计数器是否高于 10。

if counter == 10 {
self.actionForKey("spawnThenDelayForever").speed += 10.0 // Or some integer/float like that
counter = 0
}

现在,这将执行每 10 次生成某些内容时运行该 if 语句内的代码。但要做到这一点,您必须更新对 spawnAndThenDelayForever 的调用,以添加一个引用它的键。

self.runAction(spawnThenDelayForever, withKey "spawnThenDelayForever")

如果我给您的内容中有任何语法错误,或者它不能正常工作,请告诉我。

关于ios - swift - 随着时间的推移增加物体的速度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32357860/

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