Created
October 19, 2023 13:02
-
-
Save alikaragoz/0f3731504147e84478affada4eff23f0 to your computer and use it in GitHub Desktop.
Emoji Party SpriteKit
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SpriteKit | |
import SwiftUI | |
// MARK: - GameScene | |
class GameScene: SKScene { | |
override func didMove(to _: SKView) { | |
physicsBody = SKPhysicsBody(edgeLoopFrom: frame) | |
} | |
var emojis: [String] = [ | |
"β€οΈ", "π", "π", "π", "π", "β€οΈβπ₯", "π«Ά", "π", "π₯°", "π", "π", "π", "π", "π€", "π€", "π", "π", "β¨", "π€", "π§βπ", "π»" | |
] | |
override func touchesBegan(_ touches: Set<UITouch>, with _: UIEvent?) { | |
guard let touch = touches.first else { return } | |
let location = touch.location(in: self) | |
let emoji = SKLabelNode(text: emojis.randomElement()) | |
emoji.fontSize = 40.0 | |
let texture = view!.texture(from: emoji)! | |
let spriteNode = SKSpriteNode(texture: texture) | |
spriteNode.position = location | |
spriteNode.physicsBody = SKPhysicsBody(texture: texture, size: spriteNode.size) | |
addChild(spriteNode) | |
} | |
} | |
// MARK: - ContentView | |
struct ContentView: View { | |
func scene(size: CGSize) -> SKScene { | |
let scene = GameScene() | |
scene.size = size | |
scene.scaleMode = .aspectFit | |
return scene | |
} | |
var body: some View { | |
GeometryReader { metrics in | |
let scene = scene(size: metrics.size) | |
SpriteView(scene: scene) | |
}.ignoresSafeArea() | |
} | |
} | |
#Preview { | |
ContentView() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Avec resizeFill, pas besoin de GeometryReader