@property (SK_NONATOMIC_IOSONLY, getter = isPaused) BOOL paused;
Run Code Online (Sandbox Code Playgroud)
我发现这行代码可以添加到我的项目中,我将如何暂停整个游戏?
例如:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
for (UITouch *touch in touches)
{
SKSpriteNode *pause = (SKSpriteNode*)[self childNodeWithName:@"pause"];
CGPoint location = [touch locationInNode:self];
// NSLog(@"** TOUCH LOCATION ** \nx: %f / y: %f", location.x, location.y);
if([pause containsPoint:location])
{
NSLog(@"PAUSE GAME HERE SOMEHOW");
}
}
Run Code Online (Sandbox Code Playgroud)
}
如您所见,我已设置按钮.当我选择它时,我将如何暂停整个场景?然后当有人点击恢复按钮时恢复它.
好的,我有一些建议打电话
self.scene.view.paused = YES;
Run Code Online (Sandbox Code Playgroud)
除了这是问题,在我的app委托中
- (void)applicationWillResignActive:(UIApplication *)application{
SKView *view = (SKView *)self.window.rootViewController.view;
view.paused = YES;}
Run Code Online (Sandbox Code Playgroud)
和
- (void)applicationDidBecomeActive:(UIApplication *)application{
SKView *view = (SKView *)self.window.rootViewController.view;
view.paused = NO; …Run Code Online (Sandbox Code Playgroud) 有谁知道如何"快照"一个完整的SKView或SKScene一个NSImage?
我们已经能够使用textureFromNodeAPI SKTexture从节点及其所有子节点创建.但到目前为止,我们无法找到一种方法将图像数据提取为一个NSImage.我们正在构建一个混合Cocoa/ SpriteKit应用程序,我们需要提取场景的小缩略图.
再次,这似乎可以在iOS(获得a UIImage)使用drawViewHierarchyInRect:
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, scale);
[self drawViewHierarchyInRect:self.bounds afterScreenUpdates:YES];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Run Code Online (Sandbox Code Playgroud)
但如何做到这一点上Cocoa有NSImage?救命.
当我完成我的SKScene时,有没有办法从我的SKScene课程中解雇SKScene?
如果没有回到我的ViewController,我提出我的SKScene [skView presentScene:theScene];有没有办法重新启动场景或从我的SKView中删除?
该SKScene类参考和SKView类参考都没有帮助.
更新:
以下代码从我的SKView中删除了我的场景,[yourSKView presentScene:nil];但是当我回到我的视图控制器中时,场景仍然在后台运行.我总是可以在游戏结束时暂停它并将其发送回我的视图控制器(菜单),但我想知道是否还有另一种方法,然后暂停它就像完全删除它一样?
-(void)endTheGame {
[highscoreLabel removeFromSuperview];
NSLog(@"Game Over");
//would like to end here before calling the below method in my view controller
[self.delegate mySceneDidFinish:self];
}
Run Code Online (Sandbox Code Playgroud) 我有一个问题,我的SKView的背景颜色(灰色)在场景出现之前被短暂显示.
我通过Storyboard编辑器和我的控制器(self.skView.backgroundColor = [SKColor blueColor])手动尝试设置它,但它仍然是灰色的.这不是可以覆盖的属性吗?
更新#1
这里有一些关于发生了什么的解释:
viewDidLoad被调用并skView在屏幕上显示灰色背景(skView.scene尚未设置).视图控制器
- (void)viewDidLoad
{
[self.activityIndicator startAnimating];
[self authenticatePlayer];
self.skView.backgroundColor = [SKColor blueColor];
// During this time the grey screen is visible as the assets are being loaded and take ~1 second
// self.skView.scene is NULL here so I cannot set the backgroundColor...
[GamePlayScene loadSceneAssetsWithCompletionHandler:^{
[self.activityIndicator stopAnimating];
self.activityIndicator.hidden = YES;
CGSize viewSize = self.skView.bounds.size;
self.gamePlayScene = [[GamePlayScene alloc] initWithSize:viewSize];
self.adView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
[self.skView presentScene:self.gamePlayScene]; …Run Code Online (Sandbox Code Playgroud) 目标:我想呈现一个新场景:
[self.scene.view presentScene:level2 transition:reveal];
Run Code Online (Sandbox Code Playgroud)
并结束当前的背景音乐以启动新的背景音乐(2级的新背景音乐).
问题:在呈现新场景时,1.场景(level1)的背景音乐一直在播放,即使在离开迷你游戏时也不会停止,因为整个游戏由几个迷你游戏组成.
播放的音乐是SKAction:
@implementation WBMAnimalMiniGameLvL1
-(id)initWithSize:(CGSize)size {
if (self = [super initWithSize:size])
{
/* Setup your scene here */
self.temporaryScore = 0;
self.animalSKSprites = [[WBMAnimalsMiniGameModel alloc] init];
self.backgroundImage = [SKSpriteNode spriteNodeWithImageNamed:@"farmBackground1024x768.png"];
self.backgroundImage.position = CGPointMake(CGRectGetMidX(self.frame),
CGRectGetMidY(self.frame));
self.temporaryStartingPointIndex = -1;
[self addChild:self.backgroundImage];
self.playBackgroundSound = [SKAction playSoundFileNamed:@"farm.mp3" waitForCompletion:NO];
//SKAction *repeat = [SKAction repeatActionForever:playSound];
[self runAction:self.playBackgroundSound withKey:@"play"];
[self drawAllAnimalsOntoScreen];
}
return self;
}
Run Code Online (Sandbox Code Playgroud)
这是向下一级别的过渡发生的:
-(void)transitionToNextLevel
{
NSLog(@"transitionToNextLevel");
SKTransition *reveal = [SKTransition moveInWithDirection:SKTransitionDirectionDown duration:0.5];
//SKView *skView = (SKView *)self.view;
SKScene *level2 …Run Code Online (Sandbox Code Playgroud) 我使用以下代码暂停我的SKScene:
self.paused = YES;
Run Code Online (Sandbox Code Playgroud)
然而,根据此答案由安德烈Gordeev,一方面也可以使用该行暂停的场景:
self.view.paused = YES;
Run Code Online (Sandbox Code Playgroud)
我想了解调用其中一个(或两个)暂停一个场景会有什么不同.
我尝试了所建议的但是输出是一个白色的空白截图.这让我想到我没有在视图中添加任何内容.这是我在视图中添加图形的方法.addChild方法附带SpriteKit,它接收SKSpriteNodes:
addChild(background)
addChild(rate)
addChild(scoreLabel)
addChild(share)
addChild(playAgain)
addChild(highScoreLabel)
addChild(scoreBackground)
addChild(highScoreBackground)
Run Code Online (Sandbox Code Playgroud)
以下是截取屏幕截图的方法:
UIGraphicsBeginImageContext(self.view!.bounds.size)
self.view!.layer.renderInContext(UIGraphicsGetCurrentContext())
let screenshot = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
UIImageWriteToSavedPhotosAlbum(screenshot, nil, nil, nil)
Run Code Online (Sandbox Code Playgroud)
任何的意见都将会有帮助
我使用dispatch_once NSObject来创建数据指针.因此,当主视图控制器出现时,所有游戏资产指针都会生成.为了玩游戏,用户在UIViewController上点击对应于特定级别的UIButton.我叫它LevelSelectionController.当游戏结束时,用户将点击标签(SKLabel).并且将删除所有操作和节点.
[self removeAllActions];
[self removeAllChildren];
[self removeFromParent];
Run Code Online (Sandbox Code Playgroud)
此外,特定级别的SKScene子类将用户返回到LevelSelectionController的任务委托给呈现游戏SKView的viewcontroller ,如下所示.
- (void)closeScene {
SKView *spriteView = [[SKView alloc] init];
[spriteView presentScene:nil];
[self.navigationController popViewControllerAnimated:YES];
}
Run Code Online (Sandbox Code Playgroud)
我唯一的问题是当用户离开游戏场景(SKScene)时内存仍然很高.游戏需要大量资产.因此,当游戏开始时,内存使用量将跳至200 MB.当用户返回到原始级别选择视图控制器时,根据活动监视器,游戏模拟器仍然消耗200 MB .当用户进入不同级别时,内存使用量将再跳过10 MB.那么一旦用户离开SKScene,我怎样才能释放上一场比赛的内存?
我正在使用ARC.Xcode版本是5.1.开发目标是iOS 7.1.
谢谢您的帮助.
- 编辑1 -
我太愚蠢.我知道问题是什么.当我关闭场景时,我正在创建一个新的SKView,然后我将其设置为nil以获取当前场景.有用.但这不应该是这样做的方式.相反,我需要在呈现之前将当前SKView设置为变量.当我关闭场景时,我需要将该变量设置为nil.嗯......我没在想.
- 编辑2 - 当前场景以零显示时几乎没有变化.从removeFromSuperview中删除它并没有太大作用.
使用Sprite Kit我试图SKPhysicsBody根据给定的角度设置移动,所以例如如果你想让精灵向右移动你会指定1.571弧度.要将指定的角度转换为速度,我使用下面的方法将弧度转换为a CGVector.我从内存中实现的ORIGINAL版本具有将所有角度偏移90度的奇怪效果.(即如果使用0度,精灵向右移动(就像你指定90度时一样)
我通过交换dx和dy分配修复了新版本.我的问题是为什么会发生这种情况,我在原版中是否有错(似乎有其他人在网上这样做)或者是否存在基于所使用的特定坐标系的某些原因.
// ORIGINAL
- (CGVector)convertAngleToVector:(CGFloat)radians {
CGVector vector;
vector.dx = cos(radians) * 10;
vector.dy = sin(radians) * 10;
NSLog(@"DX: %0.2f DY: %0.2f", vector.dx, vector.dy);
return vector;
}
// NEW, SWAPPED DX & DY
- (CGVector)convertAngleToVector:(CGFloat)radians {
CGVector vector;
vector.dy = cos(radians) * 10;
vector.dx = sin(radians) * 10;
NSLog(@"DX: %0.2f DY: %0.2f", vector.dx, vector.dy);
return vector;
}
Run Code Online (Sandbox Code Playgroud)
注意:同样在Sprite Kit顺时针旋转是负的,到目前为止convertAngleToVector顺时针旋转正(即1.571弧度是正确的,它应该留在哪里)我可以做cos(radians*-1),sin(radians*-1)但可能有一些潜在的原因,这是基于我交换dx和dy.

我的最新游戏是iPhone制作的,这是由Games by Tutorials帮助制作的.
注意:来自SpriteKit的方法 - 正确的多任务方式不起作用.
所以,在我的ViewController.m文件中,我正在定义私有变量SKView*_skView.
然后,我做这样的事情:
- (void)viewDidLayoutSubviews
{
[super viewDidLayoutSubviews];
if(!_skView)
{
_skView = [[SKView alloc] initWithFrame:self.view.bounds];
_skView.showsFPS = NO;
_skView.showsNodeCount = NO;
// Create and configure the scene.
SKScene * scene = [MainMenuScene sceneWithSize:_skView.bounds.size];
scene.scaleMode = SKSceneScaleModeAspectFill;
// Present the scene.
[_skView presentScene:scene];
[self.view addSubview:_skView];
}
}
Run Code Online (Sandbox Code Playgroud)
我已经定义了_skView,一切正常.
但是,当我打断游戏时,它会将其状态重置为初始状态,因此,例如,如果我正在玩,有人打电话给我,游戏会切换回主菜单.它不可能是这样的.
基于我上面提到的网站,我创建了这个:
- (void)applicationWillResignActive:(UIApplication *)application
{
SKView *view = (SKView *)self.window.rootViewController.view;
view.paused = YES;
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
SKView *view = (SKView *)self.window.rootViewController.view;
view.paused …Run Code Online (Sandbox Code Playgroud)