PK creative Spring Festival, I am participating in the “Spring Festival creative submission contest”, please see: Spring Festival creative submission Contest

preface

The eighth day of the 2022 Chinese New Year has officially started. My technology export line is also ready to officially open today. First of all, let’s use Apple’s SpriteKit 2D engine to draw a small fireworks!

SpriteKit is easy to get started

SpriteKit is Apple’s official 2D game development engine. It leverages Metal for high-performance rendering while providing a simple programming interface that makes building games easy.

Engineering to create

Create a new Game project in Xcode and select SpriteKit for your Game type.

Configuring particle files

Add the prepared particle material to the project.

Generate particle objects

Initialize the SKSpriteNode object and add a particle effect to it as follows:

func generateNewSpriteNode(color: UIColor) -> SKSpriteNode{
        let node = SKSpriteNode(color: color, size: CGSize(width: 30, height: 30))
        node.position = CGPoint(x: 0, y: -500)
        node.physicsBody = SKPhysicsBody(circleOfRadius: 30)
        node.physicsBody?.isDynamic = true
        node.physicsBody?.restitution = 0
        
        let fire = SKEmitterNode(fileNamed: "Fire")
        fire?.targetNode = self
        fire?.particleColorBlendFactor = 1.0
        fire?.particleColorSequence = nil
        fire?.particleColor = color
        node.addChild(fire!)
        
        self.addChild(node)
        
        return node
    }
Copy the code

The next step is to launch the node. You can use the applyImpulse function to add an impulse force in one direction to the node.

In order to make the nodes spread out like fireworks, we can use the launching method of random Angle to apply pulses to the nodes, and the code is as follows:

func fire(a){
        // create particle
        let random = Int(arc4random_uniform(UInt32(self.colors.count)))
        let node: SKSpriteNode = generateNewSpriteNode(color: colors[random])
        let randomAngle = Int.random(in: -100.100)
        node.physicsBody?.applyImpulse(CGVector(dx: randomAngle, dy: 300))}Copy the code

The timer

Start a timer so that the node can automatically fire the node at regular intervals to make the fireworks look fuller and more beautiful, the code is as follows:

 / / timer
        Timer.scheduledTimer(withTimeInterval: 0.1, repeats: true) { time in
            self.fire()
        }
Copy the code

Ok, a small fireworks is done, look at the effect!

The last

This is the end of this article, although the length is relatively short, but it contains some common knowledge of game client engine, particle effects, object collision, rigid body, vector, etc. These content and our daily contact iOS development is still a little different, interested students can create their own engineering experience. Finally, IN the New Year, I wish you all a happy start, smooth sailing, promotion and salary increase, and all the bugs go away. PS: Did you get any red envelopes? Talk to each other in the comments section.)

Project address: github.com/ShenJieSuzh…

I am Jie Shao, if you think my writing is good, then please give me a thumbs-up + comments + favorites before leaving oh!

Previous articles:

  • Love and Hate with Apple auditors (Part 1)
  • A common tool of 2021 | 2021 year-end summary
  • Binary tree brush summary: binary search tree properties
  • Binary tree summary: binary tree properties
  • Binary tree summary: binary tree modification and construction
  • StoreKit2 smells this good? Yeah, I tried it. It smells good
  • After reading this article, I am no longer afraid of being asked how to construct a binary tree.
  • The game guys are trying to get people to pay again. That’s bad!
  • Take you rolled a netease holding cloud music home | adapter
  • NetEase Cloud Music Home Page (3)
  • NetEase Cloud Music Home Page (2)
  • NetEase Cloud Music Home Page (a)
  • Does the code need comments? Write and you lose
  • I would not study in Codable for a long time. They are for fun
  • IOS handles web data gracefully. Do you really? Why don’t you read this one
  • UICollectionView custom layout! This one is enough

Please drink a cup ☕️ + attention oh ~

  1. After reading, remember to give me a thumbs-up oh, there is 👍 power
  2. Follow the public number – HelloWorld Jie Shao, the first time push new posture

Finally, creation is not easy, if it is helpful to you, I hope you can praise and support, what questions can also be discussed in the comments section 😄 ~ **