我一直在使用SKLightNodeSpriteKit(iOS8中的新功能)进行实验,即使是在一个非常简单的测试用例中,我的性能也非常糟糕.例如,在纯色的单个光源上,我在第3代iPad上获得13.2 FPS.如果我添加第二个光源,它会下降到一个非常糟糕的7.7 FPS.SKSpriteNode
WWDC会话视频SpriteKit中的新功能提到如果你在同一个精灵上有多个灯光,你可能会得到低于60 FPS,但我甚至不能得到60 FPS.这是相关部分.
这是我在swift中的测试场景(它从一个光源开始,可以通过点击添加更多):
class GameScene: SKScene {
override func didMoveToView(view: SKView) {
let center = CGPointMake(size.width / 2.0, size.height / 2.0)
let background = SKSpriteNode(color: SKColor.lightGrayColor(), size: size)
background.position = center
background.lightingBitMask = 1
addChild(background)
let light = SKLightNode()
light.position = center
light.falloff = 1.0
light.lightColor = SKColor(hue: 0.62 , saturation: 0.89, brightness: 1.0, alpha: 0.4) …Run Code Online (Sandbox Code Playgroud) 我想尝试这些新的 Xcode 功能,但我不知道该怎么做。
我首先创建一个新的SpriteKit action file. 然后动作编辑器打开,在屏幕中间显示一个文本:No Preview Scene Selected。
那么如何“选择场景”。从哪儿开始?
我正在尝试学习如何制作一个GameManager类型的类,并为我的每个GameScenes制作单独的类......可能是错误的做法,但为了这个问题,请接受这个作为做事的方式.
我的GameManager看起来像这样,有一个对每个场景的引用,这是静态的:
import SpriteKit
class GM {
static let scene2 = SecondScene()
static let scene3 = ThirdScene()
static let home = SKScene(fileNamed: "GameScene")
}
Run Code Online (Sandbox Code Playgroud)
如何以编程方式创建SKScene,没有大小信息,因为它们位于SKScene的子类中,并且不知道视图大小是什么,我不希望他们需要担心这个:
我这样做,但获得了EXC_BAD_Access convenience override init()
class SecondScene: SKScene {
override init(size: CGSize){
super.init(size: size)
}
convenience override init(){
self.init()
self.backgroundColor = SKColor.red
self.anchorPoint = CGPoint(x: 0.5, y: 0.5)
}
}
Run Code Online (Sandbox Code Playgroud) 我在做什么,我认为,这是一个非常简单的计时器栏,屏幕上没有别的东西.然而,这在iPad Pro上无法保持60fps ......我做的事情显然是重而错吗?
let previewTime: TimeInterval = 3.0
func timerBar(){
let tbW: CGFloat = frame.size.width / 2
let tbH: CGFloat = 64
let tbSize = CGSize(width: tbW, height: tbH)
let timerBox = SKSpriteNode(color: SKColor.black, size: tbSize)
timerBox.anchorPoint = CGPoint(x: 0.0, y: 0.0)
timerBox.position = CGPoint(x: frame.size.width / 2 - (tbW / 2), y: 500)
timerBox.zPosition = 8
addChild(timerBox)
let timerBoxBar = SKSpriteNode(color: .white, size: tbSize)
timerBoxBar.anchorPoint = CGPoint(x: 0.0, y: 0.0)
timerBoxBar.position.x = 0.0
timerBoxBar.position.y = 0.0
timerBoxBar.xScale = 0.0 …Run Code Online (Sandbox Code Playgroud) 我正在做一些开关.在我的MenuScene类中,有一些布尔值是静态变量,布尔值,用于表示这些开关的状态.
这些是否可以作为引用类型进行寻址,因此我可以确定其他对象能够通过对它们的唯一引用来更改其状态?
梦想,在我梦幻般的伪代码中,我希望改变会iAmOn影响状态myButtonABC_state
class MenuScene {
static var myButtonABC_state: Bool = false
static var myButtonXYZ_state: Bool = false
override onDidMoveToView {
let buttonABC = Button(withState: MenuScene.myButtonABC_state)
let buttonXYZ = Button(withState: MenuScene.myButtonXYZ_state)
}
}
Run Code Online (Sandbox Code Playgroud)
在按钮类中
class Button {
var iAmOn: Bool = false
init(withState state: Bool){
iAmOn = state
}
override onTouchesBegun(... etc...){
if iAmOn { iAMOn = false }
else { iAmOn = true}
}
}
Run Code Online (Sandbox Code Playgroud) 在SpriteKit的SKSpriteNodes和其他可见节点中,响应这些节点中的触摸通常以某种方式完成,如下所示:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first {...
// do something with the touch location that's now in touch
// etc
Run Code Online (Sandbox Code Playgroud)
这一切都很棒,但如果我想确保获得其中任何和所有触摸的触摸信息(让我们想象一下......)SKSpriteNode怎么办?
= touches.first将条件限制为仅响应此SKSpriteNode内的第一次触摸.如何在这个SKSpriteNode中完成所有触摸,这样每个触摸都可以使用它的位置,或者其他任何东西都可以携带和携带.
我知道.愚蠢的问题.我就是那个人.我根本看不出这是怎么做到的.
为什么从未调用完成?
我非常抱歉这是一个代码转储...因为我不知道为什么每个部分都有效,除了调用完成.
除了完成之外,没有调用完成的SKAction运行.就是这个:
curtain.run(fadeMoveWipeAndReveal, completion: {onDone(), print("I'm done!!!!")})
Run Code Online (Sandbox Code Playgroud)
在以下课程中:
import SpriteKit
class WipeCurtain: SKSpriteNode {
var wipeCurtainBase: SKSpriteNode?
var returnable = SKNode()
var moveRight = SKAction()
var node = SKNode()
init( color: SKColor,
size: CGSize,
time: TimeInterval,
reveal: @escaping () -> (),
onDone: @escaping () -> ())
{
super.init(texture: nil, color: color, size: size)
wipeCurtainBase = SKSpriteNode(color: color, size: size)
let show = SKAction.run(reveal)
let fadeIn = createFadeIn(duration: time)
let moveRight = createMoveRight(duration: time)
let wipeRight = createWipeRight(duration: time)
let …Run Code Online (Sandbox Code Playgroud) SKKeyframeSequence上的Apple文档提供了用于创建渐变的简短示例代码:
let colorSequence = SKKeyframeSequence(keyframeValues: [SKColor.green,
SKColor.yellow,
SKColor.red,
SKColor.blue],
times: [0, 0.25, 0.5, 1])
colorSequence.interpolationMode = .linear
stride(from: 0, to: 1, by: 0.001).forEach {
let color = colorSequence.sample(atTime: CGFloat($0)) as! SKColor
}
Run Code Online (Sandbox Code Playgroud)
当与某种绘图系统结合使用时,据说输出:
如何从演示代码中的颜色序列采样中得出?
ps我没有任何线索如何使用SpriteKit对象绘制它,因此没有尝试代码.我不是要求代码,只是如何使用这个'数组'颜色来创建一个可以在SpriteKit中用作纹理的渐变的答案.
如何设置 2d 视图的 SceneKit SCNCamera属性,例如:
- top view
- bottom view
- front view
- back view
- left side view
- right side view
Run Code Online (Sandbox Code Playgroud) 我找不到如何根据英雄的速度和身高不断地动态地在3台摄像机(我称它们为中,上,下)之间动态融合的方法。
当跟随英雄时,中间的vCam是主要/基础的,我想根据英雄的高度按比例地混合上下vCam。
播放器/英雄可以在不同高度之间快速移动,因此应轻松地对混合进行加权。这是Cinemachine混合的自然组成部分。和工程。但是,就我目前对Cinemachine混合的了解而言,这就像是一个开关,而不是基于高度的恒定动态混合。
sprite-kit ×7
swift ×6
ios ×4
skaction ×3
boolean ×1
camera ×1
cinemachine ×1
closures ×1
completion ×1
dynamic ×1
gradient ×1
ios8 ×1
performance ×1
reference ×1
scenekit ×1
scncamera ×1
scnscene ×1
sklightnode ×1
skscene ×1
skspritenode ×1
subclass ×1
touch ×1
touchesbegan ×1
types ×1
xcode ×1