Ich habe die folgende Sprite Kit-Szene in meinem schnellen Projekt. Es bewegt sich eine SKNode
über den Bildschirm. Das Problem ist, dass ich möchte, dass sich der Kreis sowohl nach oben als auch nach unten bewegt, aber er bewegt sich nur abwärts oder horizontal. Ich überprüfte den Wert der yMove
, um zu bestätigen, dass es zufällig positive Änderungen in der y-Achse erzeugte. Es erzeugt diese positiven Werte, aber das Objekt bewegt sich immer noch nur horizontal und vertikal.Ein Sprite-Kit-Objekt erhalten, um nach oben zu gehen
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]))
}
}