swift - 随着时间的推移提高对象的速度

时间:2015-09-02 15:59:21

标签: ios swift sprite-kit delay

我想要一种方法来增加游戏的节奏,你玩的时间越长,我想通过增加在一定时间后产生的障碍物的频率,即每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)

}

1 个答案:

答案 0 :(得分:1)

您应该尝试使用简单的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-statement中的代码。但要执行此操作,您必须更新调用spawnAndThenDelayForever以添加用于引用它的密钥。

self.runAction(spawnThenDelayForever, withKey "spawnThenDelayForever")

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