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)
how to save the touched position of x and y values in a array
103 Views Asked by Prabakaran At
2
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
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
orNSSDictionary
, but you can store them in e.g. a fix-sized C-array, like:option #2
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.:
if you'd store the values in a simple
NSArray
likethe extraction procedure would be the similar to the idea above.
option #3
a simple dictionary can store them flawlessly, if you need to extract the values, it is like e.g.
option #4
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:
option #5A
another obvious way would be that creating an own class to store those coordinates, like e.g.:
.h
.m
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 theNSArray
for permanent storing, like:.h
.m
...or something similar, or you can combine them together as you'd feel which the most convenient way is in your personal view.