迅速-随着时间的推移增加物体的速度 [英] swift - Increase speed of objects over time

查看:104
本文介绍了迅速-随着时间的推移增加物体的速度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种提高游戏速度的方法,我希望通过增加在一定时间后(即每30秒或更佳地在10个对象之后)产生障碍物的频率来实现这一目标(树)已经生成,因此您玩的时间越长,越难玩。

I'm looking for a way to increase the pace of my game the longer you play it, I would like to achieve this by increasing the frequency of obstacles generated either after a certain time i.e. every 30 seconds or preferably after 10 objects (trees), have been generated so the longer you play the harder it gets.

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

This is my current set up, I use repeatActionForever how could I change this to something like repeatAction10Times with a different delay variable for each loop?

//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 代码。

You should try to use the simple action.speed code.

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

For example: Instead of running the action spawnThenDelay ten times and then running some code and repeating, try making a counter. Create a global variable at the very top of the code called counter, or whatever you want to call it. In spawnTrees(), change the code to this:

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

    ... }

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

And then in the update(), check to see if counter is above 10.

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

现在,要做的就是每产生10次,就在if语句中运行代码。但是,为此,您必须更新对 spawnAndThenDelayForever 的调用,以添加引用它的键。

Now, what this will do is run the code inside that if-statement for every 10 times you spawn something. But to do this, you'll have to update your calling of spawnAndThenDelayForever to add a key to reference it with.

self.runAction(spawnThenDelayForever, withKey "spawnThenDelayForever")

让我知道我给您的内容中是否存在语法错误,或者它是否工作不正确。

Let me know if there are any syntactical errors in what I gave you, or if it doesn't work quite right.

这篇关于迅速-随着时间的推移增加物体的速度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆