Can you save the position of a node in an sks file

烂漫一生 提交于 2019-12-11 08:08:52

问题


I'm trying to create a SKScene file where we create empty nodes at the places where our players and other objects will be placed. The problem is that the positions all have to be set in code. Is it possible to set the position in the sks file instead? I did notice there is a Position label in the node inspector, but that doesn't appear to have any effect.


回答1:


Write data with NSUserDefaults:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

NSDictionary *nodeDict = [[NSDictionary alloc] initWithObjectsAndKeys:
                          [NSNumber numberWithFloat:myNode.position.x], @"posX",
                          [NSNumber numberWithFloat:myNode.position.y], @"posY",
                          nil];

[defaults setObject: nodeDict forKey:@"nodeDict"];

[defaults synchronize];

Read data with NSUserDefaults:

NSDictionary * nodeDict = [[NSUserDefaults standardUserDefaults] objectForKey:@"nodeDict"];

float posX = [[nodeDict objectForKey:@"posX"] floatValue];
float posY = [[nodeDict objectForKey:@"posY"] floatValue];

Obviously you can write a lot more data than the example above. If you have several nodes, create a dictionary for each one and store them all in an array. Write the array to NSUserDefaults.

Keep in mind that you can only store objects in a dictionary and not primitives like int, float, etc... That is why you have to convert them into NSNumber.



来源:https://stackoverflow.com/questions/25423979/can-you-save-the-position-of-a-node-in-an-sks-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!