• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

Spritekit

My WordPress site

  • Top
  • SpriteKitBase
    • Collision
    • Enumerated
    • Shooting
    • Breakout
    • Physics1
      • physics4
      • physics3
      • Physics2
  • SwiftUI&SpriteKit
    • TabScroll
    • LocalWebView
      • Counter
      • StateObject
      • Button
      • SceneMove
    • Swift Learning
  • Photo Gallery
    • Photo Galler 2
    • Photo Gallery 3
    • リンク
      • プロフィール
      • p5.play test

Breakout

04/10/2022 by kyougif

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


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)
}
}

Filed Under: Programming, spritekit

Primary Sidebar

最近の投稿

  • ObservableObj
  • SideMenu
  • TabScroll
  • Text
  • TappedNodes
  • Collision2
  • CoreMotion
  • Collision
  • Timer
  • Enumerated
  • Random
  • SwiftUI-List
  • Shooting
  • Sound
  • Camera
  • SpriteSheet
  • SKPhysics42
  • physics4
  • physics3
  • Physics2

アーカイブ

  • 2022年9月
  • 2022年8月
  • 2022年7月
  • 2022年6月
  • 2022年5月
  • 2022年4月
  • 2014年9月
  • 2014年5月

カテゴリー

  • Programming
  • spritekit
  • swiftui

固定ページ

  • Home
  • p5.play test
  • Photo Galler 2
  • Photo Gallery
  • Photo Gallery 3
  • Top
  • プロフィール
  • リンク

Copyright © 2026 · Genesis-child on Genesis Framework · WordPress · Log in