Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

158
Views
¿Cómo animar acciones aleatorias repetidas?

¿Cómo puedo hacer que SKAction.move() sea aleatorio y se repita para siempre?

Por ejemplo:

 let boxPosition = Double.random(in: 100…600) let boxMove = SKAction.move(to: CGPoint(x: boxPosition, y: 100), duration: 10) let boxRepeat = SKAction.repeatForever(boxMove) box.run(boxRepeat)

Sé que en el ejemplo anterior no actualizará el valor...

Tengo una solución que funciona pero no se siente bien. La secuencia no funcionará...

 import SpriteKit class GameScene: SKScene { let box = SKSpriteNode() override func didMove(to view: SKView) { box.texture = SKTexture(imageNamed: "box") box.size = CGSize(width: 50, height: 50) box.position = CGPoint(x: 200, y: 200) addChild(box) let boxRandom = SKAction.run({ let boxPosition = Double.random(in: 300...500) let boxMove = SKAction.move(to: CGPoint(x: 200, y: boxPosition), duration: 2) self.box.run(boxMove) }) let boxMove = SKAction.move(to: CGPoint(x: 200, y: 100), duration: 2) let boxGroup = SKAction.sequence([boxMove, boxRandom]) let boxRepeat = SKAction.repeatForever(boxGroup) box.run(boxRepeat) } }

¿Cualquier sugerencia? Gracias por todas las respuestas de antemano.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

El SKAction.run se lleva a cabo instantáneamente, por lo que no esperará a que se complete el SKAction.move dentro de él.

Una solución simple para su código sería agregar un SKAction.wait por la misma duración que el SKAction.move dentro del bloque a la secuencia:

 let wait = SKAction.wait(forDuration: 2) let boxGroup = SKAction.sequence([boxMove, boxRandom, wait])

Si necesita usar una duración personalizada para cada acción de movimiento, puede crear un método que ejecute el movimiento aleatorio y llame recursivamente al mismo método cuando se complete el movimiento. Algo como esto:

 override func didMove(to view: SKView) { recursiveRandomMove() } func recursiveRandomMove() { let random = CGFloat.random(in: 100...400) let duration = Double.random(0...10) // your custom duration here box.run(SKAction.sequence([ SKAction.moveTo(x: random, duration: duration), SKAction.run { self.recursiveRandomMove() } ])) }

También considere mirar Drive Game Logic para tener más control sobre sus resultados.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!