在 SpriteKit 中,是否可以通过编程方式将元素添加到场景中,以便在打开 .sks 时显示它们?
In SpriteKit, is it possible to add elements programmatically to the scene, so when .sks is opened, they display?
在 SpriteKit 中,是否可以通过编程方式将元素添加到场景中,以便在打开 .sks 时显示它们?目前我有我的 GameScene
(扩展 SKScene
)class,它使用 UnarchiveFromFile 方法模式从 .sks 文件打开场景编辑器中已经添加的元素。此方法如下所示:
@implementation SKScene (Unarchive)
+ (instancetype)unarchiveFromFile:(NSString *)file {
/* Retrieve scene file path from the application bundle */
NSString *nodePath = [[NSBundle mainBundle] pathForResource:file ofType:@"sks"];
/* Unarchive the file to an SKScene object */
NSData *data = [NSData dataWithContentsOfFile:nodePath
options:NSDataReadingMappedIfSafe
error:nil];
NSKeyedUnarchiver *arch = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
[arch setClass:self forClassName:@"SKScene"];
SKScene *scene = [arch decodeObjectForKey:NSKeyedArchiveRootObjectKey];
[arch finishDecoding];
return scene;
}
@end
我怎样才能做与 unarchiveFromFile 相反的事情,即。从代码开始并将精灵添加到 .sks 文件。之后, unarchiveFromFile 将被触发,其余的将按现在的方式进行。有什么建议吗?谢谢
您不想在代码中更新您的 .sks 文件,即使您设法弄清楚了如何做。我想你想要做的是在 didMoveTo(view:)
中向场景添加元素
https://developer.apple.com/documentation/spritekit/skscene/1519607-didmove
didMove(to:) Called immediately after a scene is presented by a view.
Discussion This method is intended to be overridden in a subclass. You
can use this method to implement any custom behavior for your scene
when it is about to be presented by a view. For example, you might use0
this method to create the scene’s contents.
下面是添加边框的示例:
override func didMove(to view: SKView) {
/* Setup your scene here */
// drawBorder()
let border = drawSKShapeNode([CGPoint.zero,
CGPoint(x: 0, y: frame.height),
CGPoint(x: frame.width, y: frame.height),
CGPoint(x: frame.width, y: 0)],
withWidth: 10,
inColour: SKColor.yellow,
called: "border")
border.zPosition = 200
addChild(border)
}
// Helper function to draw an SkShapeNode
func drawSKShapeNode(_ withPoints: [CGPoint], withWidth width: CGFloat, inColour colour: SKColor, called name: String) -> SKShapeNode {
var points = withPoints
let shape = SKShapeNode(points: &points, count: points.count)
shape.lineWidth = width
shape.strokeColor = colour
shape.name = name
return shape
}
我解决了我的实际问题! - 只需在场景中放置一个图表并将其写入代码即可。它是一个导航图,并且工作正常。然而,程序化添加不起作用。
在 SpriteKit 中,是否可以通过编程方式将元素添加到场景中,以便在打开 .sks 时显示它们?目前我有我的 GameScene
(扩展 SKScene
)class,它使用 UnarchiveFromFile 方法模式从 .sks 文件打开场景编辑器中已经添加的元素。此方法如下所示:
@implementation SKScene (Unarchive)
+ (instancetype)unarchiveFromFile:(NSString *)file {
/* Retrieve scene file path from the application bundle */
NSString *nodePath = [[NSBundle mainBundle] pathForResource:file ofType:@"sks"];
/* Unarchive the file to an SKScene object */
NSData *data = [NSData dataWithContentsOfFile:nodePath
options:NSDataReadingMappedIfSafe
error:nil];
NSKeyedUnarchiver *arch = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
[arch setClass:self forClassName:@"SKScene"];
SKScene *scene = [arch decodeObjectForKey:NSKeyedArchiveRootObjectKey];
[arch finishDecoding];
return scene;
}
@end
我怎样才能做与 unarchiveFromFile 相反的事情,即。从代码开始并将精灵添加到 .sks 文件。之后, unarchiveFromFile 将被触发,其余的将按现在的方式进行。有什么建议吗?谢谢
您不想在代码中更新您的 .sks 文件,即使您设法弄清楚了如何做。我想你想要做的是在 didMoveTo(view:)
https://developer.apple.com/documentation/spritekit/skscene/1519607-didmove
didMove(to:) Called immediately after a scene is presented by a view.
Discussion This method is intended to be overridden in a subclass. You can use this method to implement any custom behavior for your scene when it is about to be presented by a view. For example, you might use0 this method to create the scene’s contents.
下面是添加边框的示例:
override func didMove(to view: SKView) {
/* Setup your scene here */
// drawBorder()
let border = drawSKShapeNode([CGPoint.zero,
CGPoint(x: 0, y: frame.height),
CGPoint(x: frame.width, y: frame.height),
CGPoint(x: frame.width, y: 0)],
withWidth: 10,
inColour: SKColor.yellow,
called: "border")
border.zPosition = 200
addChild(border)
}
// Helper function to draw an SkShapeNode
func drawSKShapeNode(_ withPoints: [CGPoint], withWidth width: CGFloat, inColour colour: SKColor, called name: String) -> SKShapeNode {
var points = withPoints
let shape = SKShapeNode(points: &points, count: points.count)
shape.lineWidth = width
shape.strokeColor = colour
shape.name = name
return shape
}
我解决了我的实际问题! - 只需在场景中放置一个图表并将其写入代码即可。它是一个导航图,并且工作正常。然而,程序化添加不起作用。