Ios5 如何在数组中保存x和y值的接触位置

Ios5 如何在数组中保存x和y值的接触位置,ios5,cocos2d-iphone,cocos2d-iphone-3,Ios5,Cocos2d Iphone,Cocos2d Iphone 3,我正在使用cocos2d 3.x和Xcode 5.1.1。我像糖果压榨一样玩游戏,在这里我将精灵加载到5*5矩阵中,我已经得到了触摸精灵的位置,现在我需要保存并使用x,y值在一个数组中,如(0,0),(3,0),(2,2)有几种方法来存储坐标,很难说哪种方式更适合,因为我不知道你说“保存”的确切意思 选项#1 显然可以选择将它们存储在CGPointstruct中,但是struct设计用于存储分数坐标,但它也可以处理整数值 不能将CGPoint直接插入任何集合类型,如NSArray、NSSet或N

我正在使用cocos2d 3.x和Xcode 5.1.1。我像糖果压榨一样玩游戏,在这里我将精灵加载到5*5矩阵中,我已经得到了触摸精灵的位置,现在我需要保存并使用x,y值在一个数组中,如(0,0),(3,0),(2,2)

有几种方法来存储坐标,很难说哪种方式更适合,因为我不知道你说“保存”的确切意思

选项#1 显然可以选择将它们存储在
CGPoint
struct中,但是struct设计用于存储分数坐标,但它也可以处理整数值

不能将
CGPoint
直接插入任何集合类型,如
NSArray
NSSet
NSSDictionary
,但可以将其存储在固定大小的C数组中,如:

CGPoint _fiveCoordinates[4];
选项2 这是一个快速而丑陋的解决方案,我个人不喜欢它——但在某些情况下是有用的(与选项4相比)。也可以将其存储在任何集合类型中,并且您可以在之后提取坐标以供进一步使用,例如:

NSArray *_components = [_coordinates componentsSeparatedByString:@" "];
NSInteger x = [[_components firstObject] integerValue];
NSInteger y = [[_components lastObject] integerValue];
如果您将值存储在一个简单的
NSArray
中,如

NSArray *_coordinates = [NSArray arrayWithObjects:@(x), @(y)];
提取过程与上述想法类似

选项3 一个简单的字典可以完美地存储它们,如果你需要提取值,它就像

NSInteger x = [[_coordinates valueForKey:@"x"] integerValue];
NSInteger y = [[_coordinates valueForKey:@"y"] integerValue];
选项4 如果您喜欢使用索引路径,这是一种非常简单的存储索引的方法,因为
nsindepath
广泛使用,可以直接插入到任何集合类型中

提取坐标也是同样简单的方法:

NSInteger x = [_coordinates section];
NSInteger y = [_coordinates row];
选项#5A 另一个明显的方法是创建自己的类来存储这些坐标,例如:

NSArray *_components = [_coordinates componentsSeparatedByString:@" "];
NSInteger x = [[_components firstObject] integerValue];
NSInteger y = [[_components lastObject] integerValue];
H M 选项5B 如果您想获得纯可序列化对象,还可以使用
NSCoding
协议对其进行扩展,该对象可以与
NSArray
一起存档以进行永久存储,例如:

H
@接口坐标:NSObject{}
@财产(非TMIC)NSInteger x;
@不动产(非TMIC)NSInteger y;
@结束
M
@实施坐标
#布拉格标记-
-(id)initWithCoder:(NSCoder*)aDecoder{
if(self=[super init]){
_x=[[aDecoder decodeObjectForKey:@“x”]整数值];
_y=[[aDecoder decodeObjectForKey:@“y”]整数值];
}
回归自我;
}
-(void)编码器与编码器:(NSCoder*)一个编码器{
[aCoder encodeObject:@(x)forKey:@“x”];
[aCoder encodeObject:@(_y)forKey:@“y”];
}
@结束
…或类似的东西,或者你可以将它们结合在一起,因为你觉得哪种方式最方便是在你个人看来

//SET
NSMutableArray* points = [[NSMutableArray alloc] initWithCapacity:10];
CGPoint pointToBeStored = CGPointMake(0,0);
[points addObject:[NSValue valueWithCGPoint:pointToBeStored]];

//GET
NSValue *value = [points objectAtIndex:index];
CGPoint storedPoint = [value CGPointValue];
NSIndexPath *_coordinates = [NSIndexPath indexPathForRow:y inSection:x];
NSInteger x = [_coordinates section];
NSInteger y = [_coordinates row];
@interface MyCoordinates : NSObject { }

@property (nonatomic) NSinteger x;
@property (nonatomic) NSinteger y;

@end
@implementation MyCoordinates

@end
@interface MyCoordinates : NSObject <NSCoding> { }

@property (nonatmic) NSInteger x;
@property (nonatmic) NSInteger y;

@end
@implementation MyCoordinates

#pragma mark - <NSCoding>

- (id)initWithCoder:(NSCoder *)aDecoder {
    if (self = [super init]) {
        _x = [[aDecoder decodeObjectForKey:@"x"] integerValue];
        _y = [[aDecoder decodeObjectForKey:@"y"] IntegerValue];
    }
    return self;
}

- (void)encodeWithCoder:(NSCoder *)aCoder {
    [aCoder encodeObject:@(_x) forKey:@"x"];
    [aCoder encodeObject:@(_y) forKey:@"y"];
}

@end
//SET
NSMutableArray* points = [[NSMutableArray alloc] initWithCapacity:10];
CGPoint pointToBeStored = CGPointMake(0,0);
[points addObject:[NSValue valueWithCGPoint:pointToBeStored]];

//GET
NSValue *value = [points objectAtIndex:index];
CGPoint storedPoint = [value CGPointValue];