• 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

SwiftUI&SpriteKit

04/02/2022 by kyougif


ContentView.swift

import SwiftUI
import SpriteKit
struct ContentView: View {
    var scene: SKScene {
        let scene = GameScene()
        scene.size = CGSize(width: 300, height: 400)
        scene.scaleMode = .fill
        return scene
    }

    var body: some View {
        SpriteView(scene: scene)
            .frame(width: 300, height: 400)
            .ignoresSafeArea()
    }
}
/*
struct ContentView: View {
let width = UIScreen.main.bounds.size.width
let height = UIScreen.main.bounds.size.height

var body: some View {
let scene = GameScene()
scene.anchorPoint = CGPoint(x: 0.5, y: 0.5)
scene.scaleMode = .resizeFill
return SpriteView(scene: scene)
.frame(width: width, height: height)
.edgesIgnoringSafeArea(.all)
}
}
*/
/////////////////////
GameScene.swift

import SpriteKit
import GameplayKit
class GameScene: SKScene {
let width = UIScreen.main.bounds.size.width
let height = UIScreen.main.bounds.size.height
var shapeNode = SKShapeNode()
override func didMove(to view: SKView) {
backgroundColor = .blue
let rect = CGRect(x: -1 * width / 2,
y: -1 * height / 2,
width: width,
height: height);
self.physicsBody = SKPhysicsBody(edgeLoopFrom: rect)
physicsWorld.gravity = CGVector(dx: 0.0, dy: -1.0)
print("frame what?")
print(frame)
// 落下するボール
shapeNode = SKShapeNode(circleOfRadius: 50)
shapeNode.position = CGPoint(x: 0,
y: 150)
shapeNode.fillColor = .white
shapeNode.strokeColor = .blue
shapeNode.physicsBody = SKPhysicsBody(circleOfRadius: shapeNode.frame.width / 2)
shapeNode.physicsBody?.isDynamic = true
shapeNode.physicsBody?.linearDamping = 0.1
shapeNode.physicsBody?.restitution = 0.8
addChild(shapeNode)
// 動かないボール
let shapeNode3 = SKShapeNode(circleOfRadius: 50)
shapeNode3.position = CGPoint(x: 40, y: -90)
shapeNode3.fillColor = .red
shapeNode3.strokeColor = .red
shapeNode3.physicsBody = SKPhysicsBody(circleOfRadius: shapeNode.frame.width / 2)
shapeNode3.physicsBody?.isDynamic = false
shapeNode3.physicsBody?.affectedByGravity = false
addChild(shapeNode3)
}
override func touchesBegan(_ touches: Set, with event: UIEvent?) {
shapeNode.physicsBody?.applyImpulse(CGVector(
dx: Double(arc4random_uniform(200)) - 100.0,
dy: Double(arc4random_uniform(200)) - 100.0))
}
override func touchesMoved(_ touches: Set, with event: UIEvent?) {
}
}

Filed Under: Programming, swiftui

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