gpt4 book ai didi

Swift SKAction 移动到屏幕底部

转载 作者:行者123 更新时间:2023-11-30 10:21:05 27 4
gpt4 key购买 nike

我试图让我的两个 Sprite 节点从屏幕顶部落到屏幕底部,移回到起始位置,然后再次落下,但是当它们达到大约 3/4 时在屏幕下方的过程中,它们会稍微“传送”到屏幕上,然后从那里继续下落,然后再“传送”回来一点,最终它们会按预期从屏幕底部掉下来。这是我所拥有的:

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)
}

}

我一生都无法弄清楚为什么会发生这种情况。它只在第一次秋天才会这样做。第一次落下后,果实完美落下,并如预期般回到顶部。

最佳答案

你能对水果进行无尽的操作吗

let fall=SKAction.repeatActionForever(SKAction.sequence([
SKAction.moveToY(height, duration: 0),
SKAction.moveToY(0, duration: 1)]))

也许当他们到达底部时会跑一个方 block

let fall=SKAction.repeatActionForever(SKAction.sequence([
SKAction.moveToY(height, duration: 0),
SKAction.moveToY(0, duration: 1),
SKAction.runBlock({ () -> Void in
println("reached bottom")
})]))

关于Swift SKAction 移动到屏幕底部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26569131/

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