我试图让我的两个精灵节点从屏幕顶部落到屏幕的底部,移回到它们的起始位置然后再次下降,但是当它们达到大约3/4的时候在屏幕下方,它们被传送到了#39;屏幕稍微向上,然后从那里继续下降,然后被传送到屏幕上。备份更多,最终他们按预期从屏幕底部掉下来。这就是我所拥有的:
override func didMoveToView(view: SKView) {
bounds = UIScreen.mainScreen().bounds
height = bounds.size.height
initFruit()
fallTimer = NSTimer.scheduledTimerWithTimeInterval(0.2, target: self, selector: "fall_timer", userInfo: nil, repeats: true)
}
func initFruit(){
fruit1 = SKSpriteNode(imageNamed: "Fruit_Apple")
fruit2 = SKSpriteNode(imageNamed: "Fruit_Banana")
fruit1.xScale = 2.5
fruit1.yScale = 2.5
fruit2.xScale = 2.5
fruit2.yScale = 2.5
//position fruit off the top of the screen, at separate x coordinates
fruit1.position = CGPoint(x: (size.width / 3), y: height + fruit1.frame.height)
fruit2.position = CGPoint(x: (size.width / 3) * 2, y: height + fruit1.frame.height)
addChild(fruit1)
addChild(fruit2)
}
func fall_timer() {
var fallDuration: NSTimeInterval
var moveActionDown: SKAction
//If fruit are at the bottom, move them back up to the top
if fruit1.position.y == -100 {
fruit1.position.y = CGFloat(height + fruit1.frame.height)
fruit2.position.y = CGFloat(height + fruit1.frame.height)
}
if fruit3.position.y == -100 {
fruit3.position.y = CGFloat(height + fruit1.frame.height)
fruit4.position.y = CGFloat(height + fruit1.frame.height)
}
//Check if fruit are at top, ready to fall again
if fruit1.position.y == height + fruit1.frame.height {
fallDuration = NSTimeInterval(arc4random_uniform(6) + 2)
moveActionDown = SKAction.moveToY(-100.0, duration: fallDuration)
fruit1.runAction(moveActionDown)
}
if fruit2.position.y == height + fruit1.frame.height {
fallDuration = NSTimeInterval(arc4random_uniform(6) + 2)
moveActionDown = SKAction.moveToY(-100.0, duration: fallDuration)
fruit2.runAction(moveActionDown)
}
}
我不能为我的生活想象为什么会发生这种情况。它只在第一次跌倒时才会这样做。第一次跌倒后,果实完全落下,并按预期移回到顶部。
答案 0 :(得分:1)
你能不能对水果采取无休止的行动
let fall=SKAction.repeatActionForever(SKAction.sequence([
SKAction.moveToY(height, duration: 0),
SKAction.moveToY(0, duration: 1)]))
并且当他们到达底部时可能会阻止
let fall=SKAction.repeatActionForever(SKAction.sequence([
SKAction.moveToY(height, duration: 0),
SKAction.moveToY(0, duration: 1),
SKAction.runBlock({ () -> Void in
println("reached bottom")
})]))