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  

Breakout

表示するフォントのサイズを引数で指定します。
標準で使える定義済フォントサイズには次のようなものがあります。


GameScene
import SpriteKit
import GameplayKit


class GameScene: SKScene, SKPhysicsContactDelegate {

let kBlockWidth             : CGFloat = 55.0
let kBlockHeight            : CGFloat = 25.0
let kBlockRows              : Int = 8
let kBlockColumns           : Int = 7
var kBlockRecoverTime = 10.0

let ballCategoryName = "ball"
let paddleCategoryName = "paddle"
let blockCategoryName = "block"

let ballCategory:UInt32 = 0x1 << 0
let bottomCategory:UInt32 = 0x1 << 1
let blockCategory:UInt32 = 0x1 << 2
let paddleCategory:UInt32 = 0x1 << 3

var label = SKLabelNode(fontNamed: "HelveticaNeue-Light")
var score = 0
let paddle = SKShapeNode(rectOf: CGSize(width:110, height:25),cornerRadius: 2)

override func didMove(to view: SKView) {
backgroundColor = .white
self.physicsWorld.contactDelegate = self
self.physicsBody = SKPhysicsBody(edgeLoopFrom: self.frame)
physicsWorld.gravity = CGVector(dx: 0.0, dy: 0.0)

let ball = SKShapeNode(circleOfRadius: 12)//circleOfRadiusで円の半径
ball.position = CGPoint(x:self.frame.midX, y:self.frame.midY-100)
ball.fillColor = UIColor.red
ball.lineWidth = 0.0
ball.strokeColor = UIColor.red

ball.physicsBody = SKPhysicsBody(circleOfRadius: ball.frame.width/2)
self.addChild(ball)
//ボールの飛んでいく方向
ball.physicsBody?.friction = 0
ball.physicsBody?.restitution = 1
ball.physicsBody?.linearDamping = 0
ball.physicsBody?.allowsRotation = false
ball.physicsBody?.applyImpulse(CGVector(dx: 5, dy: 5))

// 四角形を作成
paddle.position = CGPoint(x:self.frame.midX, y:self.frame.midY-360)
paddle.name = paddleCategoryName
paddle.fillColor = .green
paddle.lineWidth = 0.0
paddle.physicsBody = SKPhysicsBody(rectangleOf: paddle.frame.size)
paddle.physicsBody?.restitution = 0.1
paddle.physicsBody?.friction = 0.2
paddle.physicsBody?.linearDamping = 0
paddle.physicsBody?.isDynamic = false
self.addChild(paddle)

let totalBlocksWidth = kBlockWidth * CGFloat(kBlockColumns)
let xOffset = (frame.width - totalBlocksWidth) / 2
let yOffset = frame.height * 0.6
for i in 0.., with event: UIEvent?) {
// for touch in touches
// {
// let toucLocation = touch.location(in: self)
// border.position.x = toucLocation.x
// }
}

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

func didBegin(_ contact: SKPhysicsContact) {
var firstBody = SKPhysicsBody()
var secondBody = SKPhysicsBody()

if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask{
firstBody = contact.bodyA
secondBody = contact.bodyB
}else{
firstBody = contact.bodyB
secondBody = contact.bodyA
}

if firstBody.categoryBitMask == ballCategory && secondBody.categoryBitMask == bottomCategory{
//  let changeSceneAction = SKAction.run(changeScene)
//  self.run(changeSceneAction)
let gameOverScene = GameOverScene(size: self.frame.size)
self.view?.presentScene(gameOverScene)
}
if firstBody.categoryBitMask == ballCategory && secondBody.categoryBitMask == blockCategory{
secondBody.node?.removeFromParent()
score += 1
label.text = "\(score)"
if score >= 12 {
let gameOverScene = GameClearScene(size: self.frame.size)
self.view?.presentScene(gameOverScene)
}
}
}
/*
func changeScene(){
let sceneToMoveTo = GameOverScene(size: self.frame.size)
sceneToMoveTo.scaleMode = self.scaleMode
let myTransition = SKTransition.fade(withDuration: 0.5)
self.view!.presentScene(sceneToMoveTo, transition: myTransition)
}
*/
}

///////////////////////////////////
GameOverScene.swift

import SpriteKit

class GameOverScene: SKScene {

override func didMove(to view: SKView) {
backgroundColor = .blue

let gameOverLabel = SKLabelNode(fontNamed: "Avenir-Black")
gameOverLabel.fontSize = 46
gameOverLabel.position = CGPoint(x: self.frame.size.width/2, y: self.frame.size.height/2)
gameOverLabel.text = "GAME OVER!"
self.addChild(gameOverLabel)
}

override func touchesBegan(_ touches: Set, with event: UIEvent?) {
let breakOutGameScene = GameScene(size: self.size)
self.view?.presentScene(breakOutGameScene)
}
}
/////////////////////////////
GameClearScene.swift

import SpriteKit
class GameClearScene: SKScene {
override func didMove(to view: SKView) {
backgroundColor = .green
let gameOverLabel = SKLabelNode(fontNamed: "Avenir-Black")
gameOverLabel.fontSize = 46
gameOverLabel.position = CGPoint(x: self.frame.size.width/2, y: self.frame.size.height/2)
gameOverLabel.text = "GAME CLEAR!"
self.addChild(gameOverLabel)
}
override func touchesBegan(_ touches: Set, with event: UIEvent?) {
let breakOutGameScene = GameScene(size: self.size)
self.view?.presentScene(breakOutGameScene)
}
}