1

I am building my first game with SpriteKit, written in Swift 3.

I have been trying for hours to accomplish this "winning stars effect", but without any acceptable result yet :/

The effect i am looking for is demonstrated in this video: https://youtu.be/9CeK5_G8T9E

It's not a problem adding one or multiple stars; the problem is to make them move in different directions by different paths to the same location, just like it's done in this video example.

Hope for your help to lead me in the right direction.

Any solution, tutorials, examples etc. is more than welcome.

Thanks for your time.

Steve Ives
  • 7,894
  • 3
  • 24
  • 55
thar
  • 1,094
  • 2
  • 13
  • 25
  • Please post the code you have so far along with a description of what it is NOT doing that you would like it to do. – Steve Ives May 16 '17 at 10:40
  • Hi Steve, I have been working with SKAction, but im not sure this is the right solution? I do not look for help to debug any code, just a direction pointer :) – thar May 16 '17 at 10:49
  • Then you might want to look at this questions and it's answer - http://stackoverflow.com/q/43960262/1430420 It also is about creating new sprites and moving them to a specific part of the screen.although it's very hard to help if you do not say of the problem is adding a star, or adding multiple stars, or making them move, or making them move in a certain way. You might need help with creating a new sprite – Steve Ives May 16 '17 at 11:15
  • You really do need to tell us more - are you having problems putting a star sprite ion screen? Are you having trouble putting *multiple* star sprites on screen? Are you having problems making them move? Are you having problems making them move *in a specific way*? – Steve Ives May 16 '17 at 11:17
  • Okay Steve it makes sense. It's not a problem adding one or multiple stars, the problem is to make them move in different directions by different paths to the same location – thar May 16 '17 at 11:18
  • 2
    OK - that helps. Then you need to generate various curved paths from the star's spawn point to where you want them to end up. You can create curved paths using `func addQuadCurve(to end: CGPoint, control: CGPoint, transform: nil)`. If you use random control points (within reason), you should be able to generate a selection of curves which your stars can follow using `SKAction.follow(_ path: CGPath, duration sec: TimeInterval)' – Steve Ives May 16 '17 at 11:32
  • Hi Steve, thanks. I have tried to import the core graphic library to gamescene, bud can't acces this AddQuadCurve method? – thar May 16 '17 at 11:52
  • It should be a method on an instance of a CGMutablePath... I'll see if I can knock up an example at lunchtime. – Steve Ives May 16 '17 at 11:58
  • Thanks @Steve Ives, looking forward to play around whit this and see your example... really appreciate this :) – thar May 16 '17 at 12:10
  • I've achieved this effect with two simple SKAction for eatch spawn node. One move the node from the center to a random point of an edge circle (simple with CGPoint) and once finisched this animation, the node goes in the barNode – Simone Pistecchia May 18 '17 at 15:17

1 Answers1

7

Took a while but here's something:

import SpriteKit

class GameScene: SKScene {

var sprite = SKSpriteNode()

override func didMove(to view: SKView) {
    
    for _ in 1...15 {
        spawnsprite()
    }
}

func spawnsprite() {
    
    sprite = SKSpriteNode(color: SKColor.yellow, size: CGSize(width: 50, height: 50))
    sprite.position = CGPoint(x: (-size.width/2)+50, y: (-size.height/2)+50)
    addChild(sprite)
    
    let destination = CGPoint(x: (size.width/2)-50, y: (size.height/2)-50)
    let path = createCurvedPath(from: sprite.position, to: destination, varyingBy: 500)
    
    let squareSpeed = CGFloat(arc4random_uniform(600)) + 200
    let moveAction = SKAction.follow(path, asOffset: false, orientToPath: false, speed: squareSpeed)
    let rotateAction = SKAction.repeatForever(SKAction.rotate(byAngle: 2 * CGFloat.pi, duration: 2))
    sprite.run(SKAction.group([moveAction, rotateAction]))
}

func createCurvedPath(from start: CGPoint, to destination: CGPoint, varyingBy offset: UInt32) -> CGMutablePath {
    let pathToMove = CGMutablePath()
    pathToMove.move(to: start)
    let controlPoint = CGPoint(x: CGFloat(arc4random_uniform(offset)) - CGFloat(offset/2),
                               y: CGFloat(arc4random_uniform(offset)) - CGFloat(offset/2))
    pathToMove.addQuadCurve(to: destination, control: controlPoint)
    return pathToMove
}

override func update(_ currentTime: TimeInterval) {
    // Called before each frame is rendered
}
}

There's a lot that can be done to make this nicer (e.g. subclass SKSpriteNode and make the path that the node follows a property of the sprite itself), but it illustrates the principle.

It could probably also be done with an SKEmitter.

Steve Ives
  • 7,894
  • 3
  • 24
  • 55