axiom-spritekit-ref by charleswiltgen/axiom
npx skills add https://github.com/charleswiltgen/axiom --skill axiom-spritekit-ref按类别整理的完整 SpriteKit API 参考。
在以下情况下使用此参考:
| 节点 | 用途 | 批处理? | 性能说明 |
|---|---|---|---|
SKNode | 容器,分组 | 不适用 | 零渲染成本 |
SKSpriteNode | 带纹理的精灵 | 是(同一图集) | 主要的游戏玩法节点 |
SKShapeNode |
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
| 矢量路径 |
| 否 |
| 每个节点 1 次绘制调用 — 在游戏玩法中避免使用 |
SKLabelNode | 文本渲染 | 否 | 每个节点 1 次绘制调用 |
SKEmitterNode | 粒子系统 | 不适用 | GPU 受限,限制出生率 |
SKCameraNode | 视口控制 | 不适用 | 将 HUD 作为子节点附加 |
SKEffectNode | Core Image 滤镜 | 否 | 开销大 — 使用 shouldRasterize 进行缓存 |
SKCropNode | 遮罩 | 否 | 遮罩 + 内容 = 2+ 次绘制调用 |
SKTileMapNode | 基于图块的地图 | 是(同一图块集) | 适用于大地图的高效方案 |
SKVideoNode | 视频播放 | 否 | 使用 AVPlayer |
SK3DNode | SceneKit 内容 | 否 | 渲染 SceneKit 场景 |
SKReferenceNode | 可复用的 .sks 文件 | 不适用 | 在运行时加载存档 |
SKLightNode | 逐像素光照 | 不适用 | 限制:每个场景 8 个光源 |
SKFieldNode | 物理场 | 不适用 | 重力场、电场、磁场等 |
SKAudioNode | 位置音频 | 不适用 | 使用 AVAudioEngine |
SKTransformNode | 3D 旋转包装器 | 不适用 | 用于透视的 xRotation、yRotation |
// 创建
SKSpriteNode(imageNamed: "player") // 从资源目录
SKSpriteNode(texture: texture) // 从 SKTexture
SKSpriteNode(texture: texture, size: size) // 自定义大小
SKSpriteNode(color: .red, size: CGSize(width: 50, height: 50)) // 纯色
// 关键属性
sprite.anchorPoint = CGPoint(x: 0.5, y: 0) // 底部中心
sprite.colorBlendFactor = 0.5 // 着色强度 (0-1)
sprite.color = .red // 着色颜色
sprite.normalTexture = normalMap // 用于光照
sprite.lightingBitMask = 0x1 // 哪些光源影响此节点
sprite.shadowCastBitMask = 0x1 // 哪些光源投射阴影
sprite.shader = customShader // 逐像素效果
let label = SKLabelNode(text: "Score: 0")
label.fontName = "AvenirNext-Bold"
label.fontSize = 24
label.fontColor = .white
label.horizontalAlignmentMode = .left
label.verticalAlignmentMode = .top
label.numberOfLines = 0 // 多行 (iOS 11+)
label.preferredMaxLayoutWidth = 200
label.lineBreakMode = .byWordWrapping
// 体积体(有质量,受力的影响)
SKPhysicsBody(circleOfRadius: 20) // 最廉价
SKPhysicsBody(rectangleOf: CGSize(width: 40, height: 60))
SKPhysicsBody(polygonFrom: path) // 仅凸多边形
SKPhysicsBody(texture: texture, size: size) // 像素级精确(开销大)
SKPhysicsBody(texture: texture, alphaThreshold: 0.5, size: size)
SKPhysicsBody(bodies: [body1, body2]) // 复合体
// 边缘体(无质量边界)
SKPhysicsBody(edgeLoopFrom: rect) // 矩形边界
SKPhysicsBody(edgeLoopFrom: path) // 路径边界
SKPhysicsBody(edgeFrom: pointA, to: pointB) // 单一边缘
SKPhysicsBody(edgeChainFrom: path) // 开放路径
// 标识
body.categoryBitMask = 0x1 // 此物理体是什么
body.collisionBitMask = 0x2 // 与什么碰撞反弹
body.contactTestBitMask = 0x4 // 触发 didBegin/didEnd 的事件
// 物理特性
body.mass = 1.0 // 千克
body.density = 1.0 // 千克/平方米(自动计算质量)
body.friction = 0.2 // 0.0(冰)到 1.0(橡胶)
body.restitution = 0.3 // 0.0(无反弹)到 1.0(完全弹性)
body.linearDamping = 0.1 // 空气阻力(0 = 无)
body.angularDamping = 0.1 // 旋转阻尼
// 行为
body.isDynamic = true // 响应力的作用
body.affectedByGravity = true // 受世界重力影响
body.allowsRotation = true // 可由物理系统旋转
body.pinned = false // 固定到父节点位置
body.usesPreciseCollisionDetection = false // 用于快速移动的物体
// 运动(可读写)
body.velocity = CGVector(dx: 100, dy: 0)
body.angularVelocity = 0.0
// 施加力
body.applyForce(CGVector(dx: 0, dy: 100)) // 持续力
body.applyImpulse(CGVector(dx: 0, dy: 50)) // 瞬时冲量
body.applyTorque(0.5) // 持续扭矩
body.applyAngularImpulse(1.0) // 瞬时角冲量
body.applyForce(CGVector(dx: 10, dy: 0), at: point) // 在指定点施加力
scene.physicsWorld.gravity = CGVector(dx: 0, dy: -9.8)
scene.physicsWorld.speed = 1.0 // 0 = 暂停,2 = 双倍速度
scene.physicsWorld.contactDelegate = self
// 射线投射
let body = scene.physicsWorld.body(at: point)
let bodyInRect = scene.physicsWorld.body(in: rect)
scene.physicsWorld.enumerateBodies(alongRayStart: start, end: end) { body, point, normal, stop in
// 处理射线相交的每个物理体
}
// 销关节(枢轴)
let pin = SKPhysicsJointPin.joint(
withBodyA: bodyA, bodyB: bodyB,
anchor: anchorPoint
)
// 固定关节(刚性连接)
let fixed = SKPhysicsJointFixed.joint(
withBodyA: bodyA, bodyB: bodyB,
anchor: anchorPoint
)
// 弹簧关节
let spring = SKPhysicsJointSpring.joint(
withBodyA: bodyA, bodyB: bodyB,
anchorA: pointA, anchorB: pointB
)
spring.frequency = 1.0 // 每秒振荡次数
spring.damping = 0.5 // 0 = 无阻尼
// 滑动关节(线性约束)
let slide = SKPhysicsJointSliding.joint(
withBodyA: bodyA, bodyB: bodyB,
anchor: point, axis: CGVector(dx: 1, dy: 0)
)
// 限制关节(距离约束)
let limit = SKPhysicsJointLimit.joint(
withBodyA: bodyA, bodyB: bodyB,
anchorA: pointA, anchorB: pointB
)
// 将关节添加到世界
scene.physicsWorld.add(joint)
// 移除:scene.physicsWorld.remove(joint)
// 重力(定向)
let gravity = SKFieldNode.linearGravityField(withVector: vector_float3(0, -9.8, 0))
// 径向重力(朝向/远离某点)
let radial = SKFieldNode.radialGravityField()
radial.strength = 5.0
// 电场(依赖电荷)
let electric = SKFieldNode.electricField()
// 噪声场(湍流)
let noise = SKFieldNode.noiseField(withSmoothness: 0.5, animationSpeed: 1.0)
// 涡流场
let vortex = SKFieldNode.vortexField()
// 阻力场
let drag = SKFieldNode.dragField()
// 所有场共享的属性:
field.region = SKRegion(radius: 100) // 作用区域
field.strength = 1.0 // 强度
field.falloff = 0.0 // 距离衰减
field.minimumRadius = 10 // 内部无效区域
field.isEnabled = true
field.categoryBitMask = 0xFFFFFFFF // 影响哪些物理体
SKAction.move(to: point, duration: 1.0)
SKAction.move(by: CGVector(dx: 100, dy: 0), duration: 0.5)
SKAction.moveTo(x: 200, duration: 1.0)
SKAction.moveTo(y: 300, duration: 1.0)
SKAction.moveBy(x: 50, y: 0, duration: 0.5)
SKAction.follow(path, asOffset: true, orientToPath: true, duration: 2.0)
SKAction.rotate(byAngle: .pi, duration: 1.0) // 相对旋转
SKAction.rotate(toAngle: .pi / 2, duration: 0.5) // 绝对旋转
SKAction.rotate(toAngle: angle, duration: 0.5, shortestUnitArc: true)
SKAction.scale(to: 2.0, duration: 0.5)
SKAction.scale(by: 1.5, duration: 0.3)
SKAction.scaleX(to: 2.0, y: 1.0, duration: 0.5)
SKAction.resize(toWidth: 100, height: 50, duration: 0.5)
SKAction.fadeIn(withDuration: 0.5)
SKAction.fadeOut(withDuration: 0.5)
SKAction.fadeAlpha(to: 0.5, duration: 0.3)
SKAction.fadeAlpha(by: -0.2, duration: 0.3)
SKAction.sequence([action1, action2, action3]) // 顺序执行
SKAction.group([action1, action2]) // 并行执行
SKAction.repeat(action, count: 5) // 有限次重复
SKAction.repeatForever(action) // 无限重复
action.reversed() // 反向执行
SKAction.wait(forDuration: 1.0) // 延迟
SKAction.wait(forDuration: 1.0, withRange: 0.5) // 随机延迟
SKAction.setTexture(texture)
SKAction.setTexture(texture, resize: true)
SKAction.animate(with: [tex1, tex2, tex3], timePerFrame: 0.1)
SKAction.animate(with: textures, timePerFrame: 0.1, resize: false, restore: true)
SKAction.colorize(with: .red, colorBlendFactor: 1.0, duration: 0.5)
SKAction.colorize(withColorBlendFactor: 0, duration: 0.5)
SKAction.playSoundFileNamed("explosion.wav", waitForCompletion: false)
SKAction.removeFromParent()
SKAction.run(block)
SKAction.run(block, queue: .main)
SKAction.customAction(withDuration: 1.0) { node, elapsed in
// 自定义逐帧逻辑
}
SKAction.applyForce(CGVector(dx: 0, dy: 100), duration: 0.5)
SKAction.applyImpulse(CGVector(dx: 50, dy: 0), duration: 1.0/60.0) // 约 1 帧
SKAction.applyTorque(0.5, duration: 1.0)
SKAction.changeCharge(to: 1.0, duration: 0.5)
SKAction.changeMass(to: 2.0, duration: 0.5)
action.timingMode = .linear // 恒定速度
action.timingMode = .easeIn // 慢 → 快
action.timingMode = .easeOut // 快 → 慢
action.timingMode = .easeInEaseOut // 慢 → 快 → 慢
action.speed = 2.0 // 2 倍速度
// 从图像
let tex = SKTexture(imageNamed: "player")
// 从图集
let atlas = SKTextureAtlas(named: "Characters")
let tex = atlas.textureNamed("player_run_1")
// 子矩形(用于手动精灵表)
let sub = SKTexture(rect: CGRect(x: 0, y: 0, width: 0.25, height: 0.5), in: sheetTexture)
// 从 CGImage
let tex = SKTexture(cgImage: cgImage)
// 过滤模式
tex.filteringMode = .nearest // 像素艺术(无平滑)
tex.filteringMode = .linear // 平滑缩放(默认)
// 预加载
SKTexture.preload([tex1, tex2]) { /* 准备就绪 */ }
// 在 Xcode 中创建:Assets.xcassets → New Sprite Atlas
// 或在项目包中使用 .atlas 文件夹
let atlas = SKTextureAtlas(named: "Characters")
let textureNames = atlas.textureNames // 图集中所有纹理名称
// 预加载整个图集
atlas.preload { /* 图集准备就绪 */ }
// 预加载多个图集
SKTextureAtlas.preloadTextureAtlases([atlas1, atlas2]) { /* 全部准备就绪 */ }
// 从图集创建动画
let frames = (1...8).map { atlas.textureNamed("run_\($0)") }
let animate = SKAction.animate(with: frames, timePerFrame: 0.1)
// 朝向另一个节点
let orient = SKConstraint.orient(to: targetNode, offset: SKRange(constantValue: 0))
// 朝向一个点
let orient = SKConstraint.orient(to: point, offset: SKRange(constantValue: 0))
// 位置约束(保持 X 在范围内)
let xRange = SKConstraint.positionX(SKRange(lowerLimit: 0, upperLimit: 400))
// 位置约束(保持 Y 在范围内)
let yRange = SKConstraint.positionY(SKRange(lowerLimit: 50, upperLimit: 750))
// 距离约束(保持在节点的一定范围内)
let dist = SKConstraint.distance(SKRange(lowerLimit: 50, upperLimit: 200), to: targetNode)
// 旋转约束
let rot = SKConstraint.zRotation(SKRange(lowerLimit: -.pi/4, upperLimit: .pi/4))
// 应用约束(按顺序处理)
node.constraints = [orient, xRange, yRange]
// 切换启用状态
node.constraints?.first?.isEnabled = false
SKRange(constantValue: 100) // 精确为 100
SKRange(lowerLimit: 50, upperLimit: 200) // 50...200
SKRange(lowerLimit: 0) // >= 0
SKRange(upperLimit: 500) // <= 500
SKRange(value: 100, variance: 20) // 80...120
let skView = SKView(frame: view.bounds)
// 调试叠加层
skView.showsFPS = true
skView.showsNodeCount = true
skView.showsDrawCount = true
skView.showsPhysics = true
skView.showsFields = true
skView.showsQuadCount = true
// 性能
skView.ignoresSiblingOrder = true // 启用批处理优化
skView.shouldCullNonVisibleNodes = true // 自动隐藏屏幕外节点(手动更快)
skView.isAsynchronous = true // 默认:异步渲染
skView.allowsTransparency = false // 不透明背景更快
// 帧率
skView.preferredFramesPerSecond = 60 // 或 120 用于 ProMotion
// 呈现场景
skView.presentScene(scene)
skView.presentScene(scene, transition: .fade(withDuration: 0.5))
| 模式 | 宽高比 | 内容 | 最适合 |
|---|---|---|---|
.aspectFill | 保持 | 填充视图,裁剪边缘 | 大多数游戏 |
.aspectFit | 保持 | 适应视图,可能留黑边 | 需要精确布局 |
.resizeFill | 扭曲 | 拉伸以填充 | 几乎不用 |
.fill | 可变 | 场景调整大小以匹配视图 | 自适应场景 |
SKTransition.fade(withDuration: 0.5)
SKTransition.fade(with: .black, duration: 0.5)
SKTransition.crossFade(withDuration: 0.5)
SKTransition.flipHorizontal(withDuration: 0.5)
SKTransition.flipVertical(withDuration: 0.5)
SKTransition.reveal(with: .left, duration: 0.5)
SKTransition.moveIn(with: .right, duration: 0.5)
SKTransition.push(with: .up, duration: 0.5)
SKTransition.doorway(withDuration: 0.5)
SKTransition.doorsOpenHorizontal(withDuration: 0.5)
SKTransition.doorsOpenVertical(withDuration: 0.5)
SKTransition.doorsCloseHorizontal(withDuration: 0.5)
SKTransition.doorsCloseVertical(withDuration: 0.5)
// 使用 CIFilter 自定义:
SKTransition(ciFilter: filter, duration: 0.5)
let emitter = SKEmitterNode(fileNamed: "Spark")!
// 发射控制
emitter.particleBirthRate = 100 // 每秒粒子数
emitter.numParticlesToEmit = 0 // 0 = 无限
emitter.particleLifetime = 2.0 // 秒
emitter.particleLifetimeRange = 0.5 // ± 随机值
// 位置
emitter.particlePosition = .zero
emitter.particlePositionRange = CGVector(dx: 10, dy: 10)
// 运动
emitter.emissionAngle = .pi / 2 // 方向(弧度)
emitter.emissionAngleRange = .pi / 4 // 扩散范围
emitter.particleSpeed = 100 // 每秒点数
emitter.particleSpeedRange = 50 // ± 随机值
emitter.xAcceleration = 0
emitter.yAcceleration = -100 // 类似重力
// 外观
emitter.particleTexture = SKTexture(imageNamed: "spark")
emitter.particleSize = CGSize(width: 8, height: 8)
emitter.particleColor = .white
emitter.particleColorAlphaSpeed = -0.5 // 淡出
emitter.particleBlendMode = .add // 叠加模式用于火焰/辉光
emitter.particleAlpha = 1.0
emitter.particleAlphaSpeed = -0.5
// 缩放
emitter.particleScale = 1.0
emitter.particleScaleRange = 0.5
emitter.particleScaleSpeed = -0.3 // 随时间缩小
// 旋转
emitter.particleRotation = 0
emitter.particleRotationSpeed = 2.0
// 目标节点(用于轨迹)
emitter.targetNode = scene // 粒子保持在世界空间
// 渲染顺序
emitter.particleRenderOrder = .dontCare // .oldestFirst, .oldestLast, .dontCare
// 物理场交互
emitter.fieldBitMask = 0x1
| 效果 | 关键设置 |
|---|---|
| 火焰 | blendMode: .add, 快速的 alphaSpeed, 橙色→红色颜色,向上速度 |
| 烟雾 | blendMode: .alpha, 慢速,灰色,随时间放大 |
| 火花 | blendMode: .add, 高速 + 范围,短生命周期,小尺寸 |
| 雨 | 向下的 emissionAngle,窄范围,长生命周期,薄纹理 |
| 雪 | 缓慢向下速度,宽位置范围,轻微的 x 加速度 |
| 轨迹 | 设置 targetNode 为场景,窄发射角,中等生命周期 |
| 爆炸 | 高出生率,短的 numParticlesToEmit,高速范围 |
import MetalKit
let device = MTLCreateSystemDefaultDevice()!
let renderer = SKRenderer(device: device)
renderer.scene = gameScene
renderer.ignoresSiblingOrder = true
// 在 Metal 渲染循环中:
func draw(in view: MTKView) {
guard let commandBuffer = commandQueue.makeCommandBuffer(),
let rpd = view.currentRenderPassDescriptor else { return }
renderer.update(atTime: CACurrentMediaTime())
renderer.render(
withViewport: CGRect(origin: .zero, size: view.drawableSize),
commandBuffer: commandBuffer,
renderPassDescriptor: rpd
)
commandBuffer.present(view.currentDrawable!)
commandBuffer.commit()
}
// 用于逐像素效果的片段着色器
let shader = SKShader(source: """
void main() {
vec4 color = texture2D(u_texture, v_tex_coord);
// 去色
float gray = dot(color.rgb, vec3(0.299, 0.587, 0.114));
gl_FragColor = vec4(vec3(gray), color.a) * v_color_mix.a;
}
""")
sprite.shader = shader
// 使用 uniform 变量
let shader = SKShader(source: """
void main() {
vec4 color = texture2D(u_texture, v_tex_coord);
color.rgb *= u_intensity;
gl_FragColor = color;
}
""")
shader.uniforms = [
SKUniform(name: "u_intensity", float: 0.8)
]
// 内置 uniform 变量:
// u_texture — 精灵纹理
// u_time — 经过的时间
// u_path_length — 形状节点路径长度
// v_tex_coord — 纹理坐标
// v_color_mix — 颜色/alpha 混合
// SKAttribute 用于每个节点的值
import SpriteKit
import SwiftUI
// 基本嵌入
struct GameView: View {
var body: some View {
SpriteView(scene: makeScene())
.ignoresSafeArea()
}
func makeScene() -> SKScene {
let scene = GameScene(size: CGSize(width: 1024, height: 768))
scene.scaleMode = .aspectFill
return scene
}
}
// 带选项
SpriteView(
scene: scene,
transition: .fade(withDuration: 0.5), // 场景过渡
isPaused: false, // 暂停控制
preferredFramesPerSecond: 60, // 帧率
options: [.ignoresSiblingOrder, .shouldCullNonVisibleNodes],
debugOptions: [.showsFPS, .showsNodeCount] // 调试叠加层
)
| 选项 | 用途 |
|---|---|
.ignoresSiblingOrder | 启用绘制顺序批处理优化 |
.shouldCullNonVisibleNodes | 自动隐藏屏幕外节点 |
.allowsTransparency | 允许透明背景(较慢) |
| 选项 | 显示 |
|---|---|
.showsFPS | 每秒帧数 |
.showsNodeCount | 总可见节点数 |
.showsDrawCount | 每帧绘制调用次数 |
.showsPhysics | 物理体轮廓 |
.showsFields | 物理场区域 |
.showsQuadCount | 四边形细分数量 |
// 在 SwiftUI 和场景之间共享的可观察模型
@Observable
class GameState {
var score = 0
var isPaused = false
var lives = 3
}
// 场景读取/写入共享模型
class GameScene: SKScene {
var gameState: GameState?
override func update(_ currentTime: TimeInterval) {
guard let state = gameState, !state.isPaused else { return }
// 游戏逻辑更新 state.score, state.lives 等
}
}
// SwiftUI 视图拥有模型
struct GameContainerView: View {
@State private var gameState = GameState()
@State private var scene: GameScene = {
let s = GameScene(size: CGSize(width: 1024, height: 768))
s.scaleMode = .aspectFill
return s
}()
var body: some View {
VStack {
Text("Score: \(gameState.score)")
SpriteView(scene: scene, isPaused: gameState.isPaused)
.ignoresSafeArea()
}
.onAppear { scene.gameState = gameState }
}
}
关键模式:使用 @Observable 模型作为桥梁。场景修改它;SwiftUI 观察变化。避免在视图 body 中重新创建场景 — 使用 @State 来持久化场景实例。
WWDC:2014-608, 2016-610, 2017-609
文档:/spritekit/skspritenode, /spritekit/skphysicsbody, /spritekit/skaction, /spritekit/skemitternode, /spritekit/skrenderer
技能:axiom-spritekit, axiom-spritekit-diag
每周安装数
115
代码仓库
GitHub 星标数
678
首次出现
2026年2月5日
安全审计
安装于
opencode109
gemini-cli107
codex105
github-copilot104
kimi-cli103
amp102
Complete API reference for SpriteKit organized by category.
Use this reference when:
| Node | Purpose | Batches? | Performance Notes |
|---|---|---|---|
SKNode | Container, grouping | N/A | Zero rendering cost |
SKSpriteNode | Textured sprites | Yes (same atlas) | Primary gameplay node |
SKShapeNode | Vector paths | No | 1 draw call each — avoid in gameplay |
SKLabelNode | Text rendering | No | 1 draw call each |
SKEmitterNode | Particle systems | N/A | GPU-bound, limit birth rate |
SKCameraNode | Viewport control | N/A | Attach HUD as children |
SKEffectNode | Core Image filters | No | Expensive — cache with shouldRasterize |
SKCropNode | Masking | No | Mask + content = 2+ draw calls |
SKTileMapNode | Tile-based maps | Yes (same tileset) | Efficient for large maps |
SKVideoNode | Video playback | No | Uses AVPlayer |
SK3DNode | SceneKit content | No | Renders SceneKit scene |
SKReferenceNode | Reusable .sks files | N/A | Loads archive at runtime |
SKLightNode | Per-pixel lighting | N/A | Limits: 8 lights per scene |
SKFieldNode | Physics fields | N/A | Gravity, electric, magnetic, etc. |
SKAudioNode | Positional audio | N/A | Uses AVAudioEngine |
SKTransformNode | 3D rotation wrapper | N/A | xRotation, yRotation for perspective |
// Creation
SKSpriteNode(imageNamed: "player") // From asset catalog
SKSpriteNode(texture: texture) // From SKTexture
SKSpriteNode(texture: texture, size: size) // Custom size
SKSpriteNode(color: .red, size: CGSize(width: 50, height: 50)) // Solid color
// Key properties
sprite.anchorPoint = CGPoint(x: 0.5, y: 0) // Bottom-center
sprite.colorBlendFactor = 0.5 // Tint strength (0-1)
sprite.color = .red // Tint color
sprite.normalTexture = normalMap // For lighting
sprite.lightingBitMask = 0x1 // Which lights affect this
sprite.shadowCastBitMask = 0x1 // Which lights cast shadows
sprite.shader = customShader // Per-pixel effects
let label = SKLabelNode(text: "Score: 0")
label.fontName = "AvenirNext-Bold"
label.fontSize = 24
label.fontColor = .white
label.horizontalAlignmentMode = .left
label.verticalAlignmentMode = .top
label.numberOfLines = 0 // Multi-line (iOS 11+)
label.preferredMaxLayoutWidth = 200
label.lineBreakMode = .byWordWrapping
// Volume bodies (have mass, respond to forces)
SKPhysicsBody(circleOfRadius: 20) // Cheapest
SKPhysicsBody(rectangleOf: CGSize(width: 40, height: 60))
SKPhysicsBody(polygonFrom: path) // Convex only
SKPhysicsBody(texture: texture, size: size) // Pixel-perfect (expensive)
SKPhysicsBody(texture: texture, alphaThreshold: 0.5, size: size)
SKPhysicsBody(bodies: [body1, body2]) // Compound
// Edge bodies (massless boundaries)
SKPhysicsBody(edgeLoopFrom: rect) // Rectangle boundary
SKPhysicsBody(edgeLoopFrom: path) // Path boundary
SKPhysicsBody(edgeFrom: pointA, to: pointB) // Single edge
SKPhysicsBody(edgeChainFrom: path) // Open path
// Identity
body.categoryBitMask = 0x1 // What this body IS
body.collisionBitMask = 0x2 // What it bounces off
body.contactTestBitMask = 0x4 // What triggers didBegin/didEnd
// Physical characteristics
body.mass = 1.0 // kg
body.density = 1.0 // kg/m^2 (auto-calculates mass)
body.friction = 0.2 // 0.0 (ice) to 1.0 (rubber)
body.restitution = 0.3 // 0.0 (no bounce) to 1.0 (perfect bounce)
body.linearDamping = 0.1 // Air resistance (0 = none)
body.angularDamping = 0.1 // Rotational damping
// Behavior
body.isDynamic = true // Responds to forces
body.affectedByGravity = true // Subject to world gravity
body.allowsRotation = true // Can rotate from physics
body.pinned = false // Pinned to parent position
body.usesPreciseCollisionDetection = false // For fast objects
// Motion (read/write)
body.velocity = CGVector(dx: 100, dy: 0)
body.angularVelocity = 0.0
// Force application
body.applyForce(CGVector(dx: 0, dy: 100)) // Continuous
body.applyImpulse(CGVector(dx: 0, dy: 50)) // Instant
body.applyTorque(0.5) // Continuous rotation
body.applyAngularImpulse(1.0) // Instant rotation
body.applyForce(CGVector(dx: 10, dy: 0), at: point) // Force at point
scene.physicsWorld.gravity = CGVector(dx: 0, dy: -9.8)
scene.physicsWorld.speed = 1.0 // 0 = paused, 2 = double speed
scene.physicsWorld.contactDelegate = self
// Ray casting
let body = scene.physicsWorld.body(at: point)
let bodyInRect = scene.physicsWorld.body(in: rect)
scene.physicsWorld.enumerateBodies(alongRayStart: start, end: end) { body, point, normal, stop in
// Process each body the ray intersects
}
// Pin joint (pivot)
let pin = SKPhysicsJointPin.joint(
withBodyA: bodyA, bodyB: bodyB,
anchor: anchorPoint
)
// Fixed joint (rigid connection)
let fixed = SKPhysicsJointFixed.joint(
withBodyA: bodyA, bodyB: bodyB,
anchor: anchorPoint
)
// Spring joint
let spring = SKPhysicsJointSpring.joint(
withBodyA: bodyA, bodyB: bodyB,
anchorA: pointA, anchorB: pointB
)
spring.frequency = 1.0 // Oscillations per second
spring.damping = 0.5 // 0 = no damping
// Sliding joint (linear constraint)
let slide = SKPhysicsJointSliding.joint(
withBodyA: bodyA, bodyB: bodyB,
anchor: point, axis: CGVector(dx: 1, dy: 0)
)
// Limit joint (distance constraint)
let limit = SKPhysicsJointLimit.joint(
withBodyA: bodyA, bodyB: bodyB,
anchorA: pointA, anchorB: pointB
)
// Add joint to world
scene.physicsWorld.add(joint)
// Remove: scene.physicsWorld.remove(joint)
// Gravity (directional)
let gravity = SKFieldNode.linearGravityField(withVector: vector_float3(0, -9.8, 0))
// Radial gravity (toward/away from point)
let radial = SKFieldNode.radialGravityField()
radial.strength = 5.0
// Electric field (charge-dependent)
let electric = SKFieldNode.electricField()
// Noise field (turbulence)
let noise = SKFieldNode.noiseField(withSmoothness: 0.5, animationSpeed: 1.0)
// Vortex
let vortex = SKFieldNode.vortexField()
// Drag
let drag = SKFieldNode.dragField()
// All fields share:
field.region = SKRegion(radius: 100) // Area of effect
field.strength = 1.0 // Intensity
field.falloff = 0.0 // Distance falloff
field.minimumRadius = 10 // Inner dead zone
field.isEnabled = true
field.categoryBitMask = 0xFFFFFFFF // Which bodies affected
SKAction.move(to: point, duration: 1.0)
SKAction.move(by: CGVector(dx: 100, dy: 0), duration: 0.5)
SKAction.moveTo(x: 200, duration: 1.0)
SKAction.moveTo(y: 300, duration: 1.0)
SKAction.moveBy(x: 50, y: 0, duration: 0.5)
SKAction.follow(path, asOffset: true, orientToPath: true, duration: 2.0)
SKAction.rotate(byAngle: .pi, duration: 1.0) // Relative
SKAction.rotate(toAngle: .pi / 2, duration: 0.5) // Absolute
SKAction.rotate(toAngle: angle, duration: 0.5, shortestUnitArc: true)
SKAction.scale(to: 2.0, duration: 0.5)
SKAction.scale(by: 1.5, duration: 0.3)
SKAction.scaleX(to: 2.0, y: 1.0, duration: 0.5)
SKAction.resize(toWidth: 100, height: 50, duration: 0.5)
SKAction.fadeIn(withDuration: 0.5)
SKAction.fadeOut(withDuration: 0.5)
SKAction.fadeAlpha(to: 0.5, duration: 0.3)
SKAction.fadeAlpha(by: -0.2, duration: 0.3)
SKAction.sequence([action1, action2, action3]) // Sequential
SKAction.group([action1, action2]) // Parallel
SKAction.repeat(action, count: 5) // Finite repeat
SKAction.repeatForever(action) // Infinite
action.reversed() // Reverse
SKAction.wait(forDuration: 1.0) // Delay
SKAction.wait(forDuration: 1.0, withRange: 0.5) // Random delay
SKAction.setTexture(texture)
SKAction.setTexture(texture, resize: true)
SKAction.animate(with: [tex1, tex2, tex3], timePerFrame: 0.1)
SKAction.animate(with: textures, timePerFrame: 0.1, resize: false, restore: true)
SKAction.colorize(with: .red, colorBlendFactor: 1.0, duration: 0.5)
SKAction.colorize(withColorBlendFactor: 0, duration: 0.5)
SKAction.playSoundFileNamed("explosion.wav", waitForCompletion: false)
SKAction.removeFromParent()
SKAction.run(block)
SKAction.run(block, queue: .main)
SKAction.customAction(withDuration: 1.0) { node, elapsed in
// Custom per-frame logic
}
SKAction.applyForce(CGVector(dx: 0, dy: 100), duration: 0.5)
SKAction.applyImpulse(CGVector(dx: 50, dy: 0), duration: 1.0/60.0) // ~1 frame
SKAction.applyTorque(0.5, duration: 1.0)
SKAction.changeCharge(to: 1.0, duration: 0.5)
SKAction.changeMass(to: 2.0, duration: 0.5)
action.timingMode = .linear // Constant speed
action.timingMode = .easeIn // Slow → fast
action.timingMode = .easeOut // Fast → slow
action.timingMode = .easeInEaseOut // Slow → fast → slow
action.speed = 2.0 // 2x speed
// From image
let tex = SKTexture(imageNamed: "player")
// From atlas
let atlas = SKTextureAtlas(named: "Characters")
let tex = atlas.textureNamed("player_run_1")
// Subrectangle (for manual sprite sheets)
let sub = SKTexture(rect: CGRect(x: 0, y: 0, width: 0.25, height: 0.5), in: sheetTexture)
// From CGImage
let tex = SKTexture(cgImage: cgImage)
// Filtering
tex.filteringMode = .nearest // Pixel art (no smoothing)
tex.filteringMode = .linear // Smooth scaling (default)
// Preload
SKTexture.preload([tex1, tex2]) { /* Ready */ }
// Create in Xcode: Assets.xcassets → New Sprite Atlas
// Or .atlas folder in project bundle
let atlas = SKTextureAtlas(named: "Characters")
let textureNames = atlas.textureNames // All texture names in atlas
// Preload entire atlas
atlas.preload { /* Atlas ready */ }
// Preload multiple atlases
SKTextureAtlas.preloadTextureAtlases([atlas1, atlas2]) { /* All ready */ }
// Animation from atlas
let frames = (1...8).map { atlas.textureNamed("run_\($0)") }
let animate = SKAction.animate(with: frames, timePerFrame: 0.1)
// Orient toward another node
let orient = SKConstraint.orient(to: targetNode, offset: SKRange(constantValue: 0))
// Orient toward a point
let orient = SKConstraint.orient(to: point, offset: SKRange(constantValue: 0))
// Position constraint (keep X in range)
let xRange = SKConstraint.positionX(SKRange(lowerLimit: 0, upperLimit: 400))
// Position constraint (keep Y in range)
let yRange = SKConstraint.positionY(SKRange(lowerLimit: 50, upperLimit: 750))
// Distance constraint (stay within range of node)
let dist = SKConstraint.distance(SKRange(lowerLimit: 50, upperLimit: 200), to: targetNode)
// Rotation constraint
let rot = SKConstraint.zRotation(SKRange(lowerLimit: -.pi/4, upperLimit: .pi/4))
// Apply constraints (processed in order)
node.constraints = [orient, xRange, yRange]
// Toggle
node.constraints?.first?.isEnabled = false
SKRange(constantValue: 100) // Exactly 100
SKRange(lowerLimit: 50, upperLimit: 200) // 50...200
SKRange(lowerLimit: 0) // >= 0
SKRange(upperLimit: 500) // <= 500
SKRange(value: 100, variance: 20) // 80...120
let skView = SKView(frame: view.bounds)
// Debug overlays
skView.showsFPS = true
skView.showsNodeCount = true
skView.showsDrawCount = true
skView.showsPhysics = true
skView.showsFields = true
skView.showsQuadCount = true
// Performance
skView.ignoresSiblingOrder = true // Enables batching optimizations
skView.shouldCullNonVisibleNodes = true // Auto-hide offscreen (manual is faster)
skView.isAsynchronous = true // Default: renders asynchronously
skView.allowsTransparency = false // Opaque is faster
// Frame rate
skView.preferredFramesPerSecond = 60 // Or 120 for ProMotion
// Present scene
skView.presentScene(scene)
skView.presentScene(scene, transition: .fade(withDuration: 0.5))
| Mode | Aspect Ratio | Content | Best For |
|---|---|---|---|
.aspectFill | Preserved | Fills view, crops edges | Most games |
.aspectFit | Preserved | Fits in view, letterboxes | Exact layout needed |
.resizeFill | Distorted | Stretches to fill | Almost never |
.fill | Varies | Scene resizes to match view | Adaptive scenes |
SKTransition.fade(withDuration: 0.5)
SKTransition.fade(with: .black, duration: 0.5)
SKTransition.crossFade(withDuration: 0.5)
SKTransition.flipHorizontal(withDuration: 0.5)
SKTransition.flipVertical(withDuration: 0.5)
SKTransition.reveal(with: .left, duration: 0.5)
SKTransition.moveIn(with: .right, duration: 0.5)
SKTransition.push(with: .up, duration: 0.5)
SKTransition.doorway(withDuration: 0.5)
SKTransition.doorsOpenHorizontal(withDuration: 0.5)
SKTransition.doorsOpenVertical(withDuration: 0.5)
SKTransition.doorsCloseHorizontal(withDuration: 0.5)
SKTransition.doorsCloseVertical(withDuration: 0.5)
// Custom with CIFilter:
SKTransition(ciFilter: filter, duration: 0.5)
let emitter = SKEmitterNode(fileNamed: "Spark")!
// Emission control
emitter.particleBirthRate = 100 // Particles per second
emitter.numParticlesToEmit = 0 // 0 = infinite
emitter.particleLifetime = 2.0 // Seconds
emitter.particleLifetimeRange = 0.5 // ± random
// Position
emitter.particlePosition = .zero
emitter.particlePositionRange = CGVector(dx: 10, dy: 10)
// Movement
emitter.emissionAngle = .pi / 2 // Direction (radians)
emitter.emissionAngleRange = .pi / 4 // Spread
emitter.particleSpeed = 100 // Points per second
emitter.particleSpeedRange = 50 // ± random
emitter.xAcceleration = 0
emitter.yAcceleration = -100 // Gravity-like
// Appearance
emitter.particleTexture = SKTexture(imageNamed: "spark")
emitter.particleSize = CGSize(width: 8, height: 8)
emitter.particleColor = .white
emitter.particleColorAlphaSpeed = -0.5 // Fade out
emitter.particleBlendMode = .add // Additive for fire/glow
emitter.particleAlpha = 1.0
emitter.particleAlphaSpeed = -0.5
// Scale
emitter.particleScale = 1.0
emitter.particleScaleRange = 0.5
emitter.particleScaleSpeed = -0.3 // Shrink over time
// Rotation
emitter.particleRotation = 0
emitter.particleRotationSpeed = 2.0
// Target node (for trails)
emitter.targetNode = scene // Particles stay in world space
// Render order
emitter.particleRenderOrder = .dontCare // .oldestFirst, .oldestLast, .dontCare
// Physics field interaction
emitter.fieldBitMask = 0x1
| Effect | Key Settings |
|---|---|
| Fire | blendMode: .add, fast alphaSpeed, orange→red color, upward speed |
| Smoke | blendMode: .alpha, slow speed, gray color, scale up over time |
| Sparks | blendMode: .add, high speed + range, short lifetime, small size |
| Rain | Downward emissionAngle, narrow range, long lifetime, thin texture |
| Snow | Slow downward speed, wide position range, slight x acceleration |
| Trail | Set targetNode to scene, narrow emission angle, medium lifetime |
import MetalKit
let device = MTLCreateSystemDefaultDevice()!
let renderer = SKRenderer(device: device)
renderer.scene = gameScene
renderer.ignoresSiblingOrder = true
// In Metal render loop:
func draw(in view: MTKView) {
guard let commandBuffer = commandQueue.makeCommandBuffer(),
let rpd = view.currentRenderPassDescriptor else { return }
renderer.update(atTime: CACurrentMediaTime())
renderer.render(
withViewport: CGRect(origin: .zero, size: view.drawableSize),
commandBuffer: commandBuffer,
renderPassDescriptor: rpd
)
commandBuffer.present(view.currentDrawable!)
commandBuffer.commit()
}
// Fragment shader for per-pixel effects
let shader = SKShader(source: """
void main() {
vec4 color = texture2D(u_texture, v_tex_coord);
// Desaturate
float gray = dot(color.rgb, vec3(0.299, 0.587, 0.114));
gl_FragColor = vec4(vec3(gray), color.a) * v_color_mix.a;
}
""")
sprite.shader = shader
// With uniforms
let shader = SKShader(source: """
void main() {
vec4 color = texture2D(u_texture, v_tex_coord);
color.rgb *= u_intensity;
gl_FragColor = color;
}
""")
shader.uniforms = [
SKUniform(name: "u_intensity", float: 0.8)
]
// Built-in uniforms:
// u_texture — sprite texture
// u_time — elapsed time
// u_path_length — shape node path length
// v_tex_coord — texture coordinate
// v_color_mix — color/alpha mix
// SKAttribute for per-node values
import SpriteKit
import SwiftUI
// Basic embedding
struct GameView: View {
var body: some View {
SpriteView(scene: makeScene())
.ignoresSafeArea()
}
func makeScene() -> SKScene {
let scene = GameScene(size: CGSize(width: 1024, height: 768))
scene.scaleMode = .aspectFill
return scene
}
}
// With options
SpriteView(
scene: scene,
transition: .fade(withDuration: 0.5), // Scene transition
isPaused: false, // Pause control
preferredFramesPerSecond: 60, // Frame rate
options: [.ignoresSiblingOrder, .shouldCullNonVisibleNodes],
debugOptions: [.showsFPS, .showsNodeCount] // Debug overlays
)
| Option | Purpose |
|---|---|
.ignoresSiblingOrder | Enable draw order batching optimization |
.shouldCullNonVisibleNodes | Auto-hide offscreen nodes |
.allowsTransparency | Allow transparent background (slower) |
| Option | Shows |
|---|---|
.showsFPS | Frames per second |
.showsNodeCount | Total visible nodes |
.showsDrawCount | Draw calls per frame |
.showsPhysics | Physics body outlines |
.showsFields | Physics field regions |
.showsQuadCount | Quad subdivisions |
// Observable model shared between SwiftUI and scene
@Observable
class GameState {
var score = 0
var isPaused = false
var lives = 3
}
// Scene reads/writes the shared model
class GameScene: SKScene {
var gameState: GameState?
override func update(_ currentTime: TimeInterval) {
guard let state = gameState, !state.isPaused else { return }
// Game logic updates state.score, state.lives, etc.
}
}
// SwiftUI view owns the model
struct GameContainerView: View {
@State private var gameState = GameState()
@State private var scene: GameScene = {
let s = GameScene(size: CGSize(width: 1024, height: 768))
s.scaleMode = .aspectFill
return s
}()
var body: some View {
VStack {
Text("Score: \(gameState.score)")
SpriteView(scene: scene, isPaused: gameState.isPaused)
.ignoresSafeArea()
}
.onAppear { scene.gameState = gameState }
}
}
Key pattern : Use @Observable model as bridge. Scene mutates it; SwiftUI observes changes. Avoid recreating scenes in view body — use @State to persist the scene instance.
WWDC : 2014-608, 2016-610, 2017-609
Docs : /spritekit/skspritenode, /spritekit/skphysicsbody, /spritekit/skaction, /spritekit/skemitternode, /spritekit/skrenderer
Skills : axiom-spritekit, axiom-spritekit-diag
Weekly Installs
115
Repository
GitHub Stars
678
First Seen
Feb 5, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
opencode109
gemini-cli107
codex105
github-copilot104
kimi-cli103
amp102
Trigger.dev 成本节约分析技能:优化任务运行配置,降低云计算开支
504 周安装
数据新鲜度检查工具 - 监控数据更新状态,确保数据可用性 | Astronomer Agents
495 周安装
AntV Infographic 信息图创建器 - 数据可视化与视觉设计工具
494 周安装
高级机器学习工程师技能:生产级AI/ML系统部署、MLOps与LLM集成实战指南
490 周安装
图表设计器 - 数据可视化专家 | 推荐图表类型,生成ECharts/Chart.js配置
508 周安装
Tailwind CSS v4 最佳实践:Vite插件、@theme指令、OKLCH颜色格式详解
492 周安装
| Explosion | High birth rate, short numParticlesToEmit, high speed range |