how to save the touched position of x and y values in a array

103 Views Asked by At

I'm working with the cocos2d 3.x and Xcode 5.1.1.i'm doing the game like a candy crush,here i load the sprites to 5*5 matrix,and i already get the position for the touched sprite,now i need to save and use that x,y value in a array like (0,0),(3,0),(2,2)

2

There are 2 best solutions below

0
On BEST ANSWER

there are a few of ways to store coordinates, it is hard to tell which way would fit better for, regarding I don't know what you mean exactly when you say save...

options #1

CGPoint _coords = CGPointMake(x, y);

obvious choice to store them in a CGPoint struct, however the struct is designed to store fraction coordinates, but it can handle integer values as well.

you cannot insert a CGPoint directly into any collection type, like e.g. NSArray, NSSet or NSSDictionary, but you can store them in e.g. a fix-sized C-array, like:

CGPoint _fiveCoordinates[4];

option #2

NSString *_coordinates = [NSString stringWithFormat:@"%d %d", x, y];

that is a quick and ugly solution, I personally don't like it – however in certain cases is useful (vs. option #4!). it is also possible to store it any collection type, and you can extract the coordinates after, for further usage, like e.g.:

NSArray *_components = [_coordinates componentsSeparatedByString:@" "];
NSInteger x = [[_components firstObject] integerValue];
NSInteger y = [[_components lastObject] integerValue];

if you'd store the values in a simple NSArray like

NSArray *_coordinates = [NSArray arrayWithObjects:@(x), @(y)];

the extraction procedure would be the similar to the idea above.

option #3

NSDictionary *_coordinates = [NSDictionary dictionaryWithObjectsAndKeys:@(x), @"x", @(y), @"y"];

a simple dictionary can store them flawlessly, if you need to extract the values, it is like e.g.

NSInteger x = [[_coordinates valueForKey:@"x"] integerValue];
NSInteger y = [[_coordinates valueForKey:@"y"] integerValue];

option #4

NSIndexPath *_coordinates = [NSIndexPath indexPathForRow:y inSection:x];

if you like to work with index paths, that is a very straightforward way to store indices, because the NSIndexPath widely used and can be inserted directly into any collection type.

extracting the coordinates would be the same easy way:

NSInteger x = [_coordinates section];
NSInteger y = [_coordinates row];

option #5A

another obvious way would be that creating an own class to store those coordinates, like e.g.:

.h

@interface MyCoordinates : NSObject { }

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

@end

.m

@implementation MyCoordinates

@end

option #5B

and you can also extend it comforming the NSCoding protocol, if you want to get a pure serialisable object, which can be archived with the NSArray for permanent storing, like:

.h

@interface MyCoordinates : NSObject <NSCoding> { }

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

@end

.m

@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

...or something similar, or you can combine them together as you'd feel which the most convenient way is in your personal view.

0
On
//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];