在 appDelegate 中调用 class 方法给出了时髦的符号
Calling a class method into appDelegate gives funky notation
所以我在 update() 中得到了这段代码 function/method 可以在几秒钟内跟踪游戏会话(这是为了成就目的)如果有人有更好的方法,我洗耳恭听:
-(void)update:(NSTimeInterval)currentTime
{
if (pauseTimer == NO)
{
millisecondsTimer++;
if(millisecondsTimer == 60) //Remember that SK runs at 60 FPS default which means 60 equals 1 second.
{
millisecondsTimer = 0; //Reset the counter to 0 to begin again.
_secondsTracker++; //Add 1 second to this property.
}
}
else
{
NSLog(@"Timer paused.");
}
}
我想在有人完全关闭游戏之前获取 _secondsTracker 并保存它的值(如双击主页按钮并向上滑动游戏 window 并离开)。我现在的工作方式是像这样使用 appDelegate:
AppDelegate.h
#import <UIKit/UIKit.h>
BOOL pauseTimer;
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@end
AppDelegate.m
#import "AppDelegate.h"
#import "GameScene.h" //For sessionTracker property.
#import "GameData.h" //To call upon gdTotalSessionTime property.
@interface AppDelegate ()
@end
@implementation AppDelegate
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
/*Override point for customization after application launch.*/
return YES;
}
-(void)applicationWillResignActive:(UIApplication *)application
{
/*Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.*/
/*Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.*/
pauseTimer = YES; //Will stop the secondsTimer accumulating value.
}
-(void)applicationDidEnterBackground:(UIApplication *)application
{
/*Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.*/
/*If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.*/
//Will add secondsTracker value to the total (gdTotalSessionTIme).
GameScene *gS = [[GameScene alloc] init];
[GameData sharedGameData].gdTotalSeconds = [GameData sharedGameData].gdTotalSeconds + [gS.secondsTracker]; //Supposed to add the secondsTracker value to the gdTotalSeconds value.
[[GameData sharedGameData] save]; //This saves all the data to a file in app.
NSLog(@"THIS SESSION TIME: %ld seconds", gS.secondsTracker);
}
-(void)applicationWillEnterForeground:(UIApplication *)application
{
/*Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.*/
}
-(void)applicationDidBecomeActive:(UIApplication *)application
{
/*Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.*/
pauseTimer = NO; //Will resume the secondsTimer value accumulation.
}
-(void)applicationWillTerminate:(UIApplication *)application
{
/*Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.*/
}
给我带来问题的代码行是:
[GameData sharedGameData].gdTotalSeconds = [GameData sharedGameData].gdTotalSeconds + gS.secondsTracker;
更具体地说 gS.secondsTracker
。 Xcode一直想换成这样:
*([GameData sharedGameData].gdTotalSeconds + gS.secondsTracker);
这样做,没有任何结果符合我的要求,而且我知道 GameData
class 工作正常并将其他数据保存到它创建的文件中。有人请告诉我这些 *() 是什么?
虽然我建议您可以提供一个单例访问器来获取 GameScene
的当前 运行 实例,但它 完全 不是单例,因为您将希望允许 GameScene
objects 正常创建。所需要的只是访问 "current" 实例,类似:
header
@interface GameScene : SKScene
...
+ (GameScene *)currentGameScene;
...
@end
实施
static GameScene *_instance = nil;
@interface GameScene
- (instancetype)init...
{
if (self) ...
_instance = self;
return self;
}
- (void)dealloc
{
if (self == _instance)
_instance = nil;
}
+ (GameScene *)currentGameScene
{
return _instance;
}
所以我在 update() 中得到了这段代码 function/method 可以在几秒钟内跟踪游戏会话(这是为了成就目的)如果有人有更好的方法,我洗耳恭听:
-(void)update:(NSTimeInterval)currentTime
{
if (pauseTimer == NO)
{
millisecondsTimer++;
if(millisecondsTimer == 60) //Remember that SK runs at 60 FPS default which means 60 equals 1 second.
{
millisecondsTimer = 0; //Reset the counter to 0 to begin again.
_secondsTracker++; //Add 1 second to this property.
}
}
else
{
NSLog(@"Timer paused.");
}
}
我想在有人完全关闭游戏之前获取 _secondsTracker 并保存它的值(如双击主页按钮并向上滑动游戏 window 并离开)。我现在的工作方式是像这样使用 appDelegate:
AppDelegate.h
#import <UIKit/UIKit.h>
BOOL pauseTimer;
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@end
AppDelegate.m
#import "AppDelegate.h"
#import "GameScene.h" //For sessionTracker property.
#import "GameData.h" //To call upon gdTotalSessionTime property.
@interface AppDelegate ()
@end
@implementation AppDelegate
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
/*Override point for customization after application launch.*/
return YES;
}
-(void)applicationWillResignActive:(UIApplication *)application
{
/*Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.*/
/*Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.*/
pauseTimer = YES; //Will stop the secondsTimer accumulating value.
}
-(void)applicationDidEnterBackground:(UIApplication *)application
{
/*Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.*/
/*If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.*/
//Will add secondsTracker value to the total (gdTotalSessionTIme).
GameScene *gS = [[GameScene alloc] init];
[GameData sharedGameData].gdTotalSeconds = [GameData sharedGameData].gdTotalSeconds + [gS.secondsTracker]; //Supposed to add the secondsTracker value to the gdTotalSeconds value.
[[GameData sharedGameData] save]; //This saves all the data to a file in app.
NSLog(@"THIS SESSION TIME: %ld seconds", gS.secondsTracker);
}
-(void)applicationWillEnterForeground:(UIApplication *)application
{
/*Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.*/
}
-(void)applicationDidBecomeActive:(UIApplication *)application
{
/*Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.*/
pauseTimer = NO; //Will resume the secondsTimer value accumulation.
}
-(void)applicationWillTerminate:(UIApplication *)application
{
/*Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.*/
}
给我带来问题的代码行是:
[GameData sharedGameData].gdTotalSeconds = [GameData sharedGameData].gdTotalSeconds + gS.secondsTracker;
更具体地说 gS.secondsTracker
。 Xcode一直想换成这样:
*([GameData sharedGameData].gdTotalSeconds + gS.secondsTracker);
这样做,没有任何结果符合我的要求,而且我知道 GameData
class 工作正常并将其他数据保存到它创建的文件中。有人请告诉我这些 *() 是什么?
虽然我建议您可以提供一个单例访问器来获取 GameScene
的当前 运行 实例,但它 完全 不是单例,因为您将希望允许 GameScene
objects 正常创建。所需要的只是访问 "current" 实例,类似:
header
@interface GameScene : SKScene
...
+ (GameScene *)currentGameScene;
...
@end
实施
static GameScene *_instance = nil;
@interface GameScene
- (instancetype)init...
{
if (self) ...
_instance = self;
return self;
}
- (void)dealloc
{
if (self == _instance)
_instance = nil;
}
+ (GameScene *)currentGameScene
{
return _instance;
}