【发布时间】:2015-12-18 12:40:45
【问题描述】:
我现在已针对以下问题提交了一个错误。任何有好的人 解决方法?
我尝试将SKTexture 保存到文件中,然后再次加载它,但我没有成功。以下代码 sn -p 可以复制到 Xcode 启动项目中的 GameScene.m 中。
我在generateTexture 中使用textureFromNode,这似乎是我的问题的根本原因。如果我使用精灵的纹理,代码可以工作,并且可以看到两个宇宙飞船。
此代码在 iOS 8 中工作,但在 Xcode7 和 iOS 9 中停止工作。我只是想在提交错误报告之前验证这是一个错误。我担心我对NSKeyedArchiver 做错了什么。
它发生在模拟器和设备上。
#import "GameScene.h"
@implementation GameScene
// Generates a texture
- (SKTexture *)generateTexture
{
SKScene *scene = [[SKScene alloc] initWithSize:CGSizeMake(100, 100)];
SKShapeNode *shapeNode = [SKShapeNode shapeNodeWithRectOfSize:CGSizeMake(50, 50)];
shapeNode.position = CGPointMake(50, 50);
shapeNode.strokeColor = SKColor.redColor;
shapeNode.lineWidth = 10;
[scene addChild:shapeNode];
SKTexture *texture = [self.view textureFromNode:scene];
//SKTexture *texture = [SKSpriteNode spriteNodeWithImageNamed:@"Spaceship"].texture; // This works!
return texture;
}
// Just generate a path
- (NSString *)fullDocumentsPath
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *yourFileName = [documentsDirectory stringByAppendingPathComponent:@"fileName"];
return yourFileName;
}
- (void)didMoveToView:(SKView *)view
{
self.scaleMode = SKSceneScaleModeResizeFill;
// Verify that the generateTexture method indeed produces a valid texture.
SKSpriteNode *s1 = [SKSpriteNode spriteNodeWithTexture:[self generateTexture]];
s1.position = CGPointMake(100, 100);
[self addChild:s1];
// Start with saving the texture.
NSString *fullName = [self fullDocumentsPath];
NSError *error;
NSFileManager *fileMgr = [NSFileManager defaultManager];
if ([fileMgr fileExistsAtPath:fullName])
{
[fileMgr removeItemAtPath:fullName error:&error];
assert(error == nil);
}
NSDictionary *dict1 = [NSDictionary dictionaryWithObject:[self generateTexture] forKey:@"object"];
bool ok = [NSKeyedArchiver archiveRootObject:dict1 toFile:fullName];
assert(ok);
// Read back the texture and place it in a sprite. This sprite is not shown. Why?
NSData *data = [NSData dataWithContentsOfFile:fullName];
NSDictionary *dict2 = [NSKeyedUnarchiver unarchiveObjectWithData:data];
SKTexture *loadedTexture = [dict2 objectForKey:@"object"];
SKSpriteNode *s2= [SKSpriteNode spriteNodeWithTexture:loadedTexture];
NSLog(@"t(%f, %f)", loadedTexture.size.width, loadedTexture.size.height); // Size of sprite & texture is zero. Why?
s2.position = CGPointMake(200, 100);
[self addChild:s2];
}
@end
鱼东更新:
这可能是一个更相关的例子,但假设场景由 4 层组成,有很多精灵。当游戏结束时,我想存储比赛结束场景的缩略图。图像将用作按钮上的纹理。按下该按钮将开始播放比赛的重播电影。会有很多带有旧游戏图像的按钮,所以我需要将每个图像存储在文件中。
-(SKTexture*)generateTexture
{
SKScene *scene = [[SKScene alloc] initWithSize:CGSizeMake(100, 100)];
SKSpriteNode *ship = [SKSpriteNode spriteNodeWithImageNamed:@"Spaceship"];
ship.position = CGPointMake(50, 50);
[scene addChild:ship];
SKTexture *texture = [self.view textureFromNode:scene];
NSLog(@"texture: %@", texture);
return texture;
}
解决方案/解决方法:
受 Russells 代码的启发,我做了以下工作。有效!
CGImageRef cgImg = texture.CGImage;
SKTexture *newText = [SKTexture textureWithCGImage:cgImg];
【问题讨论】:
-
如果我错了,请纠正我:您想要结束场景(可能是场景的一部分)的“快照”,并将其用作按钮的纹理。所以你会考虑拍摄真实的快照而不是保存场景纹理。
-
您要存储的缩略图是全屏内容还是屏幕的一部分?
-
抱歉,我无法提供反馈。问题是比赛并不总是在屏幕上播放。极端情况下,比赛可以由两个 AI 玩家进行,完全在 CPU 中。屏幕上看不到比赛,但我还是想拍个快照。在更常见的情况下,我想在快照模式下制作场景的特殊变体,例如更大的字体。这只是对上述问题的回答,我也会评论你的回答。
-
在 XCode 11.7 iPhone 8 模拟器 (iOS 13.7) 上测试。纹理 CGImage 是一个空的透明图像。纹理本身包含正确的图像。不知道是模拟器bug还是其他。
标签: ios objective-c sprite-kit ios9 sktexture