swift - 使 Sprite 套件对象向上移动

标签 swift sprite-kit

我的 swift 项目中有以下 Sprite Kit 场景。它在屏幕上移动SKNode。问题是我希望圆圈向上和向下移动,但它只向下或水平移动。我检查了 yMove 的值,以确认它在 y 轴上随机生成正向变化。它正在生成这些正值,但对象仍然仅水平和垂直移动。

class GameScene: SKScene {


override func didMoveToView(view: SKView) {
    backgroundColor = SKColor.whiteColor()

    runAction(SKAction.repeatActionForever(
        SKAction.sequence([
            SKAction.runBlock(addKugel),
            SKAction.waitForDuration(1.0)
            ])
        ))


}

func addKugel() {

    // Create sprite
    let kugel = SKShapeNode(circleOfRadius: 100 )
    kugel.strokeColor = SKColor.blackColor()
    kugel.glowWidth = 1.0

    // Determine where to spawn the monster along the Y axis
    let actualY = random(min: 200/2, max: size.height - 200/2)

    // Position the monster slightly off-screen along the right edge,
    // and along a random position along the Y axis as calculated above
    kugel.position = CGPoint(x: size.width + 200/2, y: actualY)

    // Add the monster to the scene
    addChild(kugel)

    // Determine speed of the monster
    let actualDuration = random(min: CGFloat(2.0), max: CGFloat(4.0))

    //randomly create coefficients for movement
    let xVector = random(min: CGFloat(0.0), max: CGFloat(1.0))
    var yVector = random(min: CGFloat(0.0), max: CGFloat(1.0))

    //overly complicated way to make a number negative 50 percent of the time
    var probNegative = random(min: CGFloat(0.0), max: CGFloat(1.0))
    if (probNegative > 0.5){
        yVector = 0.0 - yVector
    }
    debugPrint(yVector)
    // Create the actions
    let yMove = (200/2)*yVector
    debugPrint(yMove)
    let actionMove = SKAction.moveTo(CGPoint(x: -200/2*xVector, y: yMove), duration: NSTimeInterval(actualDuration))
    let actionMoveDone = SKAction.removeFromParent()
    kugel.runAction(SKAction.sequence([actionMove, actionMoveDone]))

}
}

最佳答案

所以我在 moveTo 所做的事情上犯了很大的错误。 moveTo 所做的只是将 Sprite 移动到特定的 CGPoint。我按照 Sprite 大小缩放 y 坐标,而不是随机选择 0size.height 之间的值。 这是上述代码片段的简化版本:

  func addKugel() {
    let KUGELWIDTH = 50.0

    let kugel = SKShapeNode(circleOfRadius: CGFloat(KUGELWIDTH)/2 )
    kugel.strokeColor = SKColor.blackColor()
    kugel.glowWidth = 1.0

    let actualY = random(min: CGFloat(KUGELWIDTH)/2, max: (size.height - CGFloat(KUGELWIDTH)/2))
    debugPrint("actualY: \(actualY)")

    kugel.position = CGPoint(x: size.width + CGFloat(KUGELWIDTH / 2), y: actualY)

    addChild(kugel)

    let actualDuration = random(min: CGFloat(2.0), max: CGFloat(4.0))

    let y = random(min: CGFloat(0.0), max: CGFloat(1.0))
    let yPos = CGFloat(y)*size.height

    let actionMove = SKAction.moveTo(CGPoint(x: 0.00, y: yPos), duration: NSTimeInterval(actualDuration))
    let actionMoveDone = SKAction.removeFromParent()
    kugel.runAction(SKAction.sequence([actionMove, actionMoveDone]))

}

关于swift - 使 Sprite 套件对象向上移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37513829/

相关文章:

ios - 如何删除用户移动手指的 UIImage 区域?

objective-c - SKAction scale physics body

ios - 在 SpriteKit 和 Swift 中关闭 SKScene

ios - 使用文本字段设置 rxSwift?

ios - 快速 iOS13 searchBar 文本字段高度更改

ios - 检查是否在特定日期添加了 Realm 数据

ios - 如何快速制作反向倒计时或正数计时器?

ios - 如何从 GameScene.swift 中关闭 ViewController?

swift - EXC_BREAKPOINT -- 由使用 swift spritekit 的碰撞检测引起

Swift Init 不符合预期类型 'Decoder'