2022.09.25 Programming  spritekit  swiftui  
 2022.09.19 Programming  swiftui  
 2022.09.11 Programming  swiftui  
 2022.09.04 Programming  swiftui  
 2022.08.28 Programming  spritekit  
 2022.08.28 Programming  spritekit  
 2022.08.28 Programming  spritekit  
 2022.07.31 Programming  spritekit  
 2022.07.10 Programming  spritekit  
 2022.07.03 Programming  spritekit  
 2022.06.26 Programming  spritekit  
 2022.06.19 Programming  spritekit  
 2022.06.14 Programming  spritekit  swiftui  
 2022.06.04 Programming  spritekit  
 2022.05.22 Programming  spritekit  
 2022.05.04 Programming  spritekit  
 2022.04.29 Programming  spritekit  
 2022.04.24 Programming  spritekit  

Shooting


import SpriteKit
import GameplayKit
import CoreMotion


class GameScene: SKScene, SKPhysicsContactDelegate {
  let motionManager = CMMotionManager()
  var accelaration: CGFloat = 0.0
  // CoreMotion  // do not use
  let shipCategory: UInt32 = 0x1 << 0
  let missileCategory:UInt32 = 0x1 << 1
  let asCategory: UInt32 = 0x1 << 2
  let earthCategory: UInt32 = 0x1 << 3

 var earth: SKSpriteNode!
 var spaceship: SKSpriteNode!
 var hearts: [SKSpriteNode] = []
 var scoreLabel: SKLabelNode!
 var Color1 = UIColor(red: 0.01, green: 0.1, blue: 0.4, alpha: 1.0)
 var audio = SKAudioNode()
    
 var timer: Timer?
 var timerForAsteroud: Timer?
 var asteroudDuration: TimeInterval = 6.0 {
     didSet {
     if asteroudDuration < 2.0 {
    timerForAsteroud?.invalidate()
   }
 }
}    
var score: Int = 0 {
  didSet {
   scoreLabel.text = "Score: \(score)"
   }
}

override func didMove(to view: SKView) {
   backgroundColor = Color1
   physicsWorld.gravity = CGVector(dx: 0, dy: 0)
   physicsWorld.contactDelegate = self
    
   self.earth = SKSpriteNode(imageNamed: "earth")
   self.earth.xScale = 1.5
   self.earth.yScale = 0.3
   self.earth.position = CGPoint(x: self.frame.midX,  y: self.frame.midY - 400)
   self.earth.zPosition = -1.0
   self.earth.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: frame.width, height: 100))
   self.earth.physicsBody?.categoryBitMask = earthCategory
   self.earth.physicsBody?.contactTestBitMask = asCategory
   self.earth.physicsBody?.collisionBitMask = 0
   addChild(self.earth)
    
   var sizeRate : CGFloat = 0.0
   var node1Size = CGSize(width: 0.0, height: 0.0)
   self.spaceship = SKSpriteNode(imageNamed: "ship")
   sizeRate = (frame.width / 6) / self.spaceship.size.width
   node1Size = CGSize(width: self.spaceship.size.width * sizeRate,
   height: self.spaceship.size.height * sizeRate)
   self.spaceship.scale(to: node1Size)
   self.spaceship.position = CGPoint(x: self.frame.midX, y: self.frame.maxY - 510)
   self.spaceship.physicsBody = SKPhysicsBody(circleOfRadius: self.spaceship.frame.width * 0.1)
   self.spaceship.physicsBody?.categoryBitMask = shipCategory
   self.spaceship.physicsBody?.contactTestBitMask = asCategory
   self.spaceship.physicsBody?.collisionBitMask = 0
   addChild(self.spaceship)
    
timer = Timer.scheduledTimer(withTimeInterval: 1.6, repeats: true, block: { _ in
   self.addAsteroid()
})
    
for i in 1...5 {
 let heart = SKSpriteNode(imageNamed: "heart")
 heart.position = CGPoint(x: self.frame.midX / 8 + heart.frame.height * CGFloat(i)*0.6, y: self.frame.maxY * 0.92 - heart.frame.height)
 addChild(heart)
 heart.setScale(0.6)
 hearts.append(heart)
 }
    
 scoreLabel = SKLabelNode(text: "Score: 0")
 scoreLabel.fontName = "HelveticaNeue-Bold"
 scoreLabel.fontSize  = 50
 scoreLabel.position = CGPoint(x: self.frame.midX / 90 + scoreLabel.frame.width / 2 + 40, y: self.frame.maxY * 1.15 - scoreLabel.frame.height * 5)
 addChild(scoreLabel)

timerForAsteroud = Timer.scheduledTimer(withTimeInterval: 5.0, repeats: true, block: { _ in
self.asteroudDuration -= 0.5
 }) 
}
    
func myAudio() {
 audio = SKAudioNode(fileNamed: "Sounds/beam.wav")
 audio.autoplayLooped = false
 addChild(audio)
 let playAction = SKAction.play()
 let wait = SKAction.wait(forDuration: 0.2)
 let grp = SKAction.group([playAction, wait])
 let rfp = SKAction.removeFromParent()
 let sequence = SKAction.sequence([grp, rfp])
 audio.run(sequence)
}
func myAudio2() {
  audio = SKAudioNode(fileNamed: "Sounds/explosion.wav")
  audio.autoplayLooped = false
  addChild(audio)
  let playAction = SKAction.play()
  let wait = SKAction.wait(forDuration: 0.2)
  let grp = SKAction.group([playAction, wait])
  let rfp = SKAction.removeFromParent()
  let sequence = SKAction.sequence([grp, rfp])
  audio.run(sequence)
 }

override func touchesBegan(_ touches: Set, with event: UIEvent?) {
      //  myAudio()
    }
override func touchesMoved(_ touches: Set, with event: UIEvent?) {
     for touch in touches {
         let toucLocation = touch.location(in: self)
         spaceship.position.x = toucLocation.x
    }
}

 func addAsteroid() {
    let names = ["as1", "as2", "as3"]
    let index = Int(arc4random_uniform(UInt32(names.count)))
    let name = names[index]
    let asteroid = SKSpriteNode(imageNamed: name)
    let random = CGFloat(arc4random_uniform(UINT32_MAX)) / CGFloat(UINT32_MAX)
    let positionX = frame.width * (random - 0.1)
    asteroid.position = CGPoint(x: positionX + 90, y: frame.height - 50 + asteroid.frame.height)
    asteroid.scale(to: CGSize(width: 40, height: 40))
    asteroid.physicsBody = SKPhysicsBody(circleOfRadius: asteroid.frame.width)
    asteroid.physicsBody?.categoryBitMask = asCategory
    asteroid.physicsBody?.contactTestBitMask = missileCategory + shipCategory + earthCategory
    asteroid.physicsBody?.collisionBitMask = 0
   addChild(asteroid)

   let move = SKAction.moveTo(y: -frame.height / 2 - asteroid.frame.height, duration: asteroudDuration)
   let remove = SKAction.removeFromParent()
   asteroid.run(SKAction.sequence([move, remove]))
 }

func testParticle(atPoint: CGPoint) {
   let explosion = SKEmitterNode(fileNamed: "MyParticle.sks")!
   explosion.position = atPoint
   explosion.name = "explosion1"
   explosion.xScale = 0.6
   explosion.yScale = 0.6
   explosion.zPosition = 10
   let action1 = SKAction.fadeOut(withDuration: 0.2)
   let action2 = SKAction.removeFromParent()
   let actionAll = SKAction.sequence([action1, action2])
   self.addChild(explosion)
   explosion.run(actionAll)
 }

override func touchesEnded(_ touches: Set, with event: UIEvent?) {
    if isPaused { return }
    // myAudio()
     let missile = SKSpriteNode(imageNamed: "missile")
    missile.position = CGPoint(x: self.spaceship.position.x, y: self.spaceship.position.y + 50)
    missile.physicsBody = SKPhysicsBody(circleOfRadius: missile.frame.height / 2)
    missile.physicsBody?.categoryBitMask = missileCategory
    missile.physicsBody?.contactTestBitMask = asCategory
    missile.physicsBody?.collisionBitMask = 0
    missile.setScale(0.4)
   addChild(missile)

    let moveToTop = SKAction.moveTo(y: frame.height + 10, duration: 0.3)
    let remove = SKAction.removeFromParent()
     missile.run(SKAction.sequence([moveToTop, remove]))
 }

func didBegin(_ contact: SKPhysicsContact) {
   var asteroid: SKPhysicsBody
   var target: SKPhysicsBody

  if contact.bodyA.categoryBitMask == asCategory {
      asteroid = contact.bodyA
      target = contact.bodyB
    } else {
       asteroid = contact.bodyB
       target = contact.bodyA
   }

  if target.categoryBitMask == missileCategory {
   // myAudio2()
    asteroid.node?.removeFromParent()
    testParticle(atPoint: contact.contactPoint)
    score += 5
 }

 if target.categoryBitMask == shipCategory || target.categoryBitMask == earthCategory {
   guard let heart = hearts.last else { return }
   heart.removeFromParent()
   hearts.removeLast()

 if hearts.isEmpty {
     self.run(SKAction.wait(forDuration: 1)) {
     let gameOverScene = GameOverScene(size: self.frame.size)
     self.view?.presentScene(gameOverScene)
   }
 }
 if self.score >= 100 {
   let gameClScene = GameClearScene(size: self.frame.size)
   self.view?.presentScene(gameClScene)
 }
}
}

}
// GameOverScene.swift
// GameClearScene.swift
// refer Breakout