Swift 使 sksprite 节点随时间移动得更快
Swift making sksprite node move faster with time
我有一个通过函数生成的精灵节点:
let NBrick1 = SKSpriteNode(texture: Brick1)
NBrick1.setScale(0.04)
NBrick1.position = CGPointMake(0.0, CGFloat(y1 + bounds/2))
NBrick1.physicsBody = SKPhysicsBody(rectangleOfSize: NBrick1.size)
NBrick1.physicsBody?.dynamic = true
BrickPairs.addChild(NBrick1)
我正在使用这个 SKAction 来移动积木:
let moveBricks = SKAction.moveByX(-distanceToMove, y: 0.0, duration: NSTimeInterval(0.01 * distanceToMove))
let removeBricks = SKAction.removeFromParent()
BricksMoveAndRemove = SKAction.sequence([moveBricks,removeBricks])
我想知道是否有办法让砖块随着时间的推移移动得更快。
其实很简单,你不需要参与SKActions
。下面的代码将加速一个精灵或多个精灵的移动,(如果你应用相同的代码。)它所做的是每次将精灵的位置在每一帧中改变一点点。这是 moveFactor
变量。要更改它加速的启动速度和速率,请更改我输入的值。
var moveFactor:CGFloat = 0.5
override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
sprite.position = CGPoint(x: sprite.position.x + moveFactor, y: sprite.position.y)
moveFactor *= 1.005
}
我有一个通过函数生成的精灵节点:
let NBrick1 = SKSpriteNode(texture: Brick1)
NBrick1.setScale(0.04)
NBrick1.position = CGPointMake(0.0, CGFloat(y1 + bounds/2))
NBrick1.physicsBody = SKPhysicsBody(rectangleOfSize: NBrick1.size)
NBrick1.physicsBody?.dynamic = true
BrickPairs.addChild(NBrick1)
我正在使用这个 SKAction 来移动积木:
let moveBricks = SKAction.moveByX(-distanceToMove, y: 0.0, duration: NSTimeInterval(0.01 * distanceToMove))
let removeBricks = SKAction.removeFromParent()
BricksMoveAndRemove = SKAction.sequence([moveBricks,removeBricks])
我想知道是否有办法让砖块随着时间的推移移动得更快。
其实很简单,你不需要参与SKActions
。下面的代码将加速一个精灵或多个精灵的移动,(如果你应用相同的代码。)它所做的是每次将精灵的位置在每一帧中改变一点点。这是 moveFactor
变量。要更改它加速的启动速度和速率,请更改我输入的值。
var moveFactor:CGFloat = 0.5
override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
sprite.position = CGPoint(x: sprite.position.x + moveFactor, y: sprite.position.y)
moveFactor *= 1.005
}